-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(filter): L3-4114 - create dumb filter component (#418)
Co-authored-by: Zachary Rose <[email protected]> Co-authored-by: Scott Dickerson <[email protected]>
- Loading branch information
1 parent
c816a5d
commit 0014771
Showing
32 changed files
with
1,278 additions
and
113 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Meta } from '@storybook/react'; | ||
import Filter from './Filter'; | ||
import FilterHeader from './FilterHeader'; | ||
import FilterInput from './FilterInput'; | ||
import { useState } from 'react'; | ||
|
||
// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction | ||
const meta = { | ||
title: 'Components/Filter', | ||
component: Filter, | ||
} satisfies Meta<typeof Filter>; | ||
|
||
export default meta; | ||
|
||
type FilterDimension = { label: string; disabled?: boolean | undefined; checked: boolean }; | ||
|
||
type FilterType = { | ||
label: string; | ||
id: string; | ||
filterDimensions: FilterDimension[]; | ||
}; | ||
|
||
type PropTypes = { | ||
filter: FilterType; | ||
viewAllLimit: number; | ||
isViewingAll: boolean; | ||
}; | ||
|
||
const filter: FilterType = { | ||
label: 'Artists & Makers', | ||
id: 'makers', | ||
filterDimensions: [ | ||
{ label: 'Jimmy', checked: true }, | ||
{ label: 'Bob', checked: false }, | ||
{ label: 'Alan', checked: false }, | ||
{ label: 'Nick', checked: false }, | ||
{ label: 'Joe', checked: false }, | ||
{ label: 'Fred', checked: false }, | ||
{ label: 'Rob', checked: false }, | ||
{ label: 'Roy', checked: false }, | ||
{ label: 'disabled', disabled: true, checked: false }, | ||
], | ||
}; | ||
|
||
const FilterComponent = (props: PropTypes) => { | ||
const { filter: intialFilters, viewAllLimit, isViewingAll } = props; | ||
const [filter, setFilter] = useState(intialFilters); | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => { | ||
const { checked, name } = e.target as HTMLInputElement; | ||
const updatedFilterDimensions = filter.filterDimensions.map((dimension) => { | ||
if (dimension.label === name) { | ||
return { | ||
...dimension, | ||
checked, | ||
}; | ||
} | ||
return dimension; | ||
}); | ||
|
||
const updatedFilter = { | ||
...filter, | ||
filterDimensions: updatedFilterDimensions, | ||
}; | ||
|
||
setFilter(updatedFilter); | ||
}; | ||
|
||
return ( | ||
<Filter key={filter.label} name={filter.label} viewAllLimit={viewAllLimit} isViewingAll={isViewingAll}> | ||
<FilterHeader heading={filter.label} /> | ||
{filter.filterDimensions.map((value: FilterDimension) => ( | ||
<FilterInput | ||
id={value.label} | ||
key={value.label} | ||
labelText={value.label} | ||
onChange={handleChange} | ||
type="checkbox" | ||
disabled={value?.disabled} | ||
name={value.label} | ||
checked={value.checked} | ||
/> | ||
))} | ||
</Filter> | ||
); | ||
}; | ||
|
||
export const Playground = (props: PropTypes) => { | ||
return <FilterComponent {...props} />; | ||
}; | ||
|
||
// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args | ||
Playground.args = { | ||
filter, | ||
viewAllLimit: 10, | ||
isViewingAll: false, | ||
}; | ||
|
||
Playground.argTypes = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Filter from './Filter'; | ||
import { runCommonTests } from '../../utils/testUtils'; | ||
import { render, screen } from '@testing-library/react'; | ||
import FilterHeader from './FilterHeader'; | ||
import FilterInput from './FilterInput'; | ||
|
||
describe('Filter', () => { | ||
runCommonTests(Filter, 'Filter'); | ||
|
||
it('renders the different input types of filter values', () => { | ||
const handleChange = vi.fn(); | ||
|
||
render( | ||
<Filter name="test"> | ||
<FilterInput | ||
type="checkbox" | ||
onChange={handleChange} | ||
id="test[test1]" | ||
labelText="Filter 1" | ||
name="test[test1]" | ||
checked={true} | ||
/> | ||
<FilterInput | ||
type="radio" | ||
onChange={handleChange} | ||
id="test[test2]" | ||
labelText="Filter 2" | ||
name="test[test2]" | ||
checked={true} | ||
/> | ||
</Filter>, | ||
); | ||
|
||
expect(screen.getAllByText('Filter 1')[0]).toBeInTheDocument(); | ||
expect(screen.getAllByText('Filter 2')[0]).toBeInTheDocument(); | ||
|
||
const checkbox = screen.getByRole('checkbox'); | ||
const radio = screen.getByRole('radio'); | ||
|
||
expect(checkbox).toHaveAttribute('type', 'checkbox'); | ||
expect(radio).toHaveAttribute('type', 'radio'); | ||
}); | ||
|
||
it('renders a filter header', () => { | ||
render( | ||
<Filter name="test"> | ||
<FilterHeader heading="Filter Header 1" /> | ||
</Filter>, | ||
); | ||
|
||
expect(screen.getByText('Filter Header 1')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should disable filters when disabled prop is passed', () => { | ||
const handleChange = vi.fn(); | ||
|
||
render( | ||
<Filter name="test"> | ||
<FilterInput | ||
type="checkbox" | ||
onChange={handleChange} | ||
id="test[test1]" | ||
labelText="Filter 1" | ||
name="test[test1]" | ||
checked={true} | ||
disabled={true} | ||
/> | ||
</Filter>, | ||
); | ||
|
||
const checkbox = screen.getByRole('checkbox'); | ||
expect(checkbox).toBeDisabled(); | ||
}); | ||
}); |
Oops, something went wrong.