Skip to content

Commit

Permalink
🔀 Show filter chips with true or false in Sieve component
Browse files Browse the repository at this point in the history
🩹 show chips with true or false
  • Loading branch information
AmandaElvkull authored Jun 23, 2023
2 parents cdf1af2 + f24a30f commit 91fbabe
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@equinor/amplify-components",
"version": "3.8.11",
"version": "3.8.12",
"description": "Frontend Typescript components for the Amplify team",
"main": "dist/index.js",
"types": "dist/types/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions src/components/Inputs/Sieve/Sieve.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default {
control: 'array',
},
onUpdate: { action: 'Ran on update' },
showChips: { actions: 'boolean' },
},
args: {
searchPlaceholder: 'Write to search for...',
Expand Down Expand Up @@ -57,6 +58,7 @@ export default {
],
},
],
showChips: true,
},
};

Expand Down
58 changes: 58 additions & 0 deletions src/components/Inputs/Sieve/Sieve.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,61 @@ test('Users can remove all filters', async () => {
sortValue: props.sortOptions?.at(0),
});
});

test('Do not show filter chips when showChips is set to false', async () => {
const props = fakeProps();
const user = userEvent.setup();
render(<Sieve {...props} showChips={false} />);

const filterByButton = screen.getByRole('button', {
name: /filter by/i,
});

await user.click(filterByButton);

for (const option of props?.filterOptions ?? []) {
expect(screen.getByText(option.label)).toBeInTheDocument();
}

const randomFilterGroup = faker.datatype.number({
min: 0,
max: (props.filterOptions?.length ?? 0) - 1,
});

await user.click(
screen.getByRole('menuitem', {
name: props.filterOptions?.[randomFilterGroup].label,
})
);

for (const option of props.filterOptions?.[randomFilterGroup].options ?? []) {
expect(screen.getByText(option.label)).toBeInTheDocument();
}

const randomIndex = faker.datatype.number({
min: 0,
max: (props.filterOptions?.[randomFilterGroup].options.length ?? 0) - 1,
});
await user.click(
screen.getByText(
props.filterOptions?.[randomFilterGroup]?.options[randomIndex]?.label ??
'not-found'
)
);

expect(props.onUpdate).toHaveBeenCalledWith({
filterValues: [
props.filterOptions?.[randomFilterGroup].options[randomIndex],
],
sortValue: props.sortOptions?.at(0),
});

await user.click(filterByButton);

const filterChips = screen.queryByText(
props.filterOptions?.[randomFilterGroup]?.options[randomIndex]?.label ??
'not-found'
);

expect(filterChips).not.toBeInTheDocument();
});
30 changes: 17 additions & 13 deletions src/components/Inputs/Sieve/Sieve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ export interface SieveProps {
sortOptions?: Option[];
filterOptions?: FilterOption[];
onUpdate: (value: SieveValue) => void;
showChips?: boolean;
}

const Sieve: FC<SieveProps> = ({
searchPlaceholder,
sortOptions,
filterOptions,
onUpdate,
showChips = true,
}) => {
const sieveValue = useRef<SieveValue>({
searchValue: undefined,
Expand Down Expand Up @@ -128,19 +130,21 @@ const Sieve: FC<SieveProps> = ({
/>
)}
</Container>
<Container>
{filterValues?.map((filter) => (
<FilterChip
key={`filter-chip-${filter.value}`}
onDelete={() => handleRemoveFilter(filter)}
>
{filter.label}
</FilterChip>
))}
{filterValues.length > 1 && (
<FilterChip onDelete={handleRemoveAll}>Remove all</FilterChip>
)}
</Container>
{showChips && (
<Container>
{filterValues?.map((filter) => (
<FilterChip
key={`filter-chip-${filter.value}`}
onDelete={() => handleRemoveFilter(filter)}
>
{filter.label}
</FilterChip>
))}
{filterValues.length > 1 && (
<FilterChip onDelete={handleRemoveAll}>Remove all</FilterChip>
)}
</Container>
)}
</Wrapper>
);
};
Expand Down

0 comments on commit 91fbabe

Please sign in to comment.