Skip to content

Commit

Permalink
fix(DataGrid): disable save button on customise column (#6026)
Browse files Browse the repository at this point in the history
* fix(DataGrid): disable save button on customise column

* fix(DataGrid): disable save button on customise column2

* fix: review comments

* fix: adding test case
  • Loading branch information
amal-k-joy committed Sep 23, 2024
1 parent fa31ebb commit 2b32f0f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
40 changes: 39 additions & 1 deletion packages/ibm-products/src/components/Datagrid/Datagrid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,10 @@ const RowSizeDropdown = ({ ...rest } = {}) => {
};

const CustomizingColumns = ({ ...rest } = {}) => {
const columns = React.useMemo(() => defaultHeader, []);
const columns = React.useMemo(
() => (rest.columns ? rest.columns : defaultHeader),
[rest.columns]
);
const [data] = useState(makeData(10));
const datagridState = useDatagrid(
{
Expand Down Expand Up @@ -2021,6 +2024,41 @@ describe(componentName, () => {
);
});
});
it('Customizing Columns disable save button when un-select all columns', async () => {
const columnsWithoutSticky = [
{
Header: 'Row Index',
accessor: (row, i) => i,
id: 'rowIndex', // id is required when accessor is a function.
},
{
Header: 'First Name',
accessor: 'firstName',
},
];
const columns = [...columnsWithoutSticky, ...defaultHeader.slice(2)];
render(
<CustomizingColumns
data-testid={dataTestId}
columns={columns}
></CustomizingColumns>
);

const customizeColumnsButton = screen.getByLabelText('Customize columns');
fireEvent.click(customizeColumnsButton);
screen.getByRole('heading', { name: /Customize columns/ });

const selectAllCheckBox = screen.getByRole('checkbox', {
name: 'Column name',
});
fireEvent.click(selectAllCheckBox);
expect(selectAllCheckBox.checked).toEqual(true);
expect(screen.getByRole('button', { name: 'Save' })).toBeEnabled();

fireEvent.click(selectAllCheckBox);
expect(selectAllCheckBox.checked).toEqual(false);
expect(screen.getByRole('button', { name: 'Save' })).toBeDisabled();
});

it('Customizing Columns', async () => {
render(<CustomizingColumns data-testid={dataTestId}></CustomizingColumns>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,7 @@ const CustomizeColumnsTearsheet = ({
});

setColumnObjects(finalDefinitions);
setDirty();
};

const setDirty = () => {
if (!isDirty) {
setIsDirty(true);
}
setIsDirty(selectedColumnsCount !== 0);
};

const getVisibleColumnsCount = useCallback(() => {
Expand Down Expand Up @@ -154,7 +148,7 @@ const CustomizeColumnsTearsheet = ({
searchText={searchText}
setColumnsObject={(cols) => {
setColumnObjects(cols);
setDirty();
setIsDirty(true);
}}
setSearchText={setSearchText}
findColumnPlaceholderLabel={findColumnPlaceholderLabel}
Expand All @@ -172,7 +166,7 @@ const CustomizeColumnsTearsheet = ({
onSelectColumn={onCheckboxCheck}
setColumnsObject={(cols) => {
setColumnObjects(cols);
setDirty();
setIsDirty(getVisibleColumnsCount() !== 0);
}}
selectAllLabel={selectAllLabel}
customizeTearsheetHeadingLabel={customizeTearsheetHeadingLabel}
Expand Down

0 comments on commit 2b32f0f

Please sign in to comment.