I am using material-table to render my data and I am dynamically building lookup
I want to add another lookup column to my table if a certain value is selected from the first lookup
const [state, setState] = useState<TableState>({
columns: [
{
title: 'Name',
field: 'name',
editComponent: (tableData) => {
return <Input autoFocus={true} onChange={(e) => tableData.onChange(e.target.value)} />;
}
},
{
title: 'Type', field: 'type_id', initialEditValue: 1, editable: 'onAdd',
lookup: columnTypes,
}
],
data: []
});
then I would want to add another item to make the tablestate look like this if they chose alignment from the first lookup
const [state, setState] = useState<TableState>({
columns: [
{
title: 'Name',
field: 'name',
editComponent: (tableData) => {
return <Input autoFocus={true} onChange={(e) => tableData.onChange(e.target.value)} />;
}
},
{
title: 'Type', field: 'type_id', initialEditValue: 1, editable: 'onAdd',
lookup: columnTypes,
},
{
title: '', field: 'alignment_col', initialEditValue: 1, editable: 'onAdd',
lookup: alignmentColumns,
}
],
data: []
});
this is what my material-table looks like. Seems like there should be some kin of onRowSelected for the lookup for a function to run so I can change my state to add the 3rd column if a certain value is selected. I have not found a solution on how to do this.
<MaterialTable
title="Column Definitions"
columns={state.columns}
data={state.data}
onRowClick={handleClick}
editable={{
onRowAdd: (newData) =>
(async function () {
const success = await editorStore.createColumnDefinition(
{
linkId: linkId,
columnName: newData.name,
columnType: (state.columns[1].lookup as { [key: number]: string })[newData.type_id],
alignmentColumn: (state.columns[2].lookup as { [key: number]: string})[newData.alignment_col]
}
);
if (success) {
changeToastValues('success', `Successfully added column '${newData.name}'`);
handleOpen();
}
})(),
onRowDelete: (oldData) =>
(async function () {
const success = await editorStore.deleteColumnsDefinition(linkId, oldData.id);
if (success) {
changeToastValues('success', `Successfully deleted column '${oldData.name}'`);
setState((prevState) => {
const data = [...prevState.data];
data.splice(data.indexOf(oldData), 1);
return { ...prevState, data };
});
}
})(),
onRowUpdate: (newData, oldData) =>
(async function () {
const resData = await editorStore.editColumnDefinition(newData.id, newData.name);
if (resData) {
changeToastValues('success', `Successfully edited column '${newData.name}'`);
setState((prevState) => {
const data = [...prevState.data];
data[data.indexOf(oldData as Row)] = resData as Row;
return { ...prevState, data };
});
}
})()
}}
/>
when alignment is selected I want to add another lookup and it should look like this.
I am struggling on how to get a function to run on selection of a lookup in order to reset the state.

