Skip to content

Commit

Permalink
🐛 - fix: fix a bug where the field selection in the DataGrid componen…
Browse files Browse the repository at this point in the history
…t was not updated when fields prop was updated
  • Loading branch information
svenvandescheur committed Jun 4, 2024
1 parent 68c8b81 commit ba70140
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 24 additions & 1 deletion src/components/data/datagrid/datagrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,24 @@ export const DataGridCaption: React.FC<DataGridCaptionProps> = ({
onSelectAll,
}) => {
const [selectFieldsModalState, setSelectFieldsModalState] = useState(false);
const [selectFieldsActiveState, setSelectFieldsActiveState] = useState<
Record<string, boolean>
>({});
const intl = useIntl();

// Create map mapping `field.name` to active state.
useEffect(() => {
setSelectFieldsActiveState(
fields.reduce(
(acc, field) => ({
...acc,
[field.name]: field.active !== false,
}),
{},
),
);
}, [fields]);

const allSelected =
selectedRows?.every((a) => renderableRows.includes(a)) &&
renderableRows.every((a) => selectedRows.includes(a));
Expand Down Expand Up @@ -595,9 +611,16 @@ export const DataGridCaption: React.FC<DataGridCaptionProps> = ({
options: fields.map((f) => ({
label: field2Caption(f.name),
value: f.name,
selected: f.active !== false,
selected: Boolean(selectFieldsActiveState[f.name]),
})),
type: "checkbox",
onChange: (e: React.ChangeEvent<HTMLInputElement>) => {
const name = e.target.value;
setSelectFieldsActiveState({
...selectFieldsActiveState,
[name]: !selectFieldsActiveState[name],
});
},
},
]}
labelSubmit={ucFirst(_labelSaveFieldSelection)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/form/checkbox/checkboxgroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const CheckboxGroup: React.FC<CheckboxGroupProps> = ({

return (
<Checkbox
defaultChecked={option.selected}
checked={option.selected}
id={checkboxId}
key={option.value || option.label || index}
name={name}
Expand Down

0 comments on commit ba70140

Please sign in to comment.