Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clean filter value button #99

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Update ESLint configuration and sort imports (#93)
- Update Safari layout for favorites (#97)
- Update to Grafana 10.2.1 (#98)
- Add clean filter value button (#99)

## 2.1.0 (2023-11-08)

Expand Down
10 changes: 10 additions & 0 deletions src/components/Table/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ export const Filter = <TableData extends object>({ column, alwaysVisible }: Prop
onChange={onChangeFilterValue}
className={styles.filterInput}
data-testid={TestIds.table.fieldFilterValue}
addonAfter={
columnFilterValue ? (
<Button
variant="secondary"
icon="times"
onClick={() => column.setFilterValue('')}
data-testid={TestIds.table.buttonCleanFilterValue}
/>
) : null
}
/>
);

Expand Down
51 changes: 51 additions & 0 deletions src/components/Table/Table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,57 @@ describe('Table', () => {
expect(selectors.cell(true, 'device2', 0)).not.toBeInTheDocument();
});

it('Should clean value filter', async () => {
render(
getComponent({
showHeader: true,
columns: [
{
id: 'value',
header: 'cell header',
accessorKey: 'value',
enableColumnFilter: true,
cell: ({ getValue, row }: any) => {
const value = getValue() as string;
return <span data-testid={InTestIds.cell(value, row.depth)}>{value}</span>;
},
},
],
data: [{ value: 'device1' }, { value: 'device2' }],
})
);

/**
* Check Filter presence
*/
expect(selectors.buttonFilter()).toBeInTheDocument();
expect(selectors.fieldFilterValue(true)).not.toBeInTheDocument();

/**
* Open Filter
*/
fireEvent.click(selectors.buttonFilter());

/**
* Check Filter Content Presence
*/
expect(selectors.fieldFilterValue()).toBeInTheDocument();

/**
* Apply filter
*/
await act(() => fireEvent.change(selectors.fieldFilterValue(), { target: { value: 'device1' } }));

expect(selectors.buttonCleanFilterValue()).toBeInTheDocument();

/**
* Clean filter value
*/
await act(async () => fireEvent.click(selectors.buttonCleanFilterValue()));

expect(selectors.fieldFilterValue()).toHaveValue('');
});

it('Should always show value filter', async () => {
render(
getComponent({
Expand Down
1 change: 1 addition & 0 deletions src/constants/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const TestIds = {
favoritesControl: 'data-testid table favorites-control',
favoritesFilter: 'data-testid table favorites-filter',
fieldFilterValue: 'data-testid table field-filter-value',
buttonCleanFilterValue: 'data-testid table button-clean-filter-value',
header: 'data-testid table header',
label: 'data-testid table label',
},
Expand Down
Loading