Skip to content

Commit 5209b1a

Browse files
authored
docs(FilterBar): include selection in With Logic story (#6973)
1 parent 9600d36 commit 5209b1a

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

packages/main/src/components/FilterBar/FilterBar.stories.tsx

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { FlexBox } from '../FlexBox/index.js';
2727
import { Text } from '../Text/index.js';
2828
import { VariantManagement } from '../VariantManagement/index.js';
2929
import { VariantItem } from '../VariantManagement/VariantItem.js';
30+
import type { FilterBarPropTypes } from './index.js';
3031
import { FilterBar } from './index.js';
3132

3233
const meta = {
@@ -137,7 +138,8 @@ const initialState = {
137138
currency: 'USD',
138139
date: '',
139140
dateRange: '',
140-
search: ''
141+
search: '',
142+
selectedFiltersByLabel: ['Age', 'Countries', 'Currency', 'Date']
141143
};
142144

143145
function reducer(state, action) {
@@ -154,6 +156,16 @@ function reducer(state, action) {
154156
return { ...state, dateRange: action.payload };
155157
case 'SET_SEARCH':
156158
return { ...state, search: action.payload };
159+
case 'SHOW_FILTER': {
160+
const updatedFilters = new Set(state.selectedFiltersByLabel);
161+
updatedFilters.add(action.payload);
162+
return { ...state, selectedFiltersByLabel: Array.from(updatedFilters) };
163+
}
164+
case 'HIDE_FILTER': {
165+
const updatedFilters = new Set(state.selectedFiltersByLabel);
166+
updatedFilters.delete(action.payload);
167+
return { ...state, selectedFiltersByLabel: Array.from(updatedFilters) };
168+
}
157169
case 'DIALOG_RESTORE':
158170
return action.payload;
159171
default:
@@ -164,7 +176,7 @@ function reducer(state, action) {
164176
export const WithLogic: Story = {
165177
render: (args) => {
166178
const [state, dispatch] = useReducer(reducer, initialState);
167-
const { age, countries, currency, date, dateRange, search } = state;
179+
const { age, countries, currency, date, dateRange, search, selectedFiltersByLabel } = state;
168180
const prevDialogOpenState = useRef();
169181

170182
const handleSearch = (e) => {
@@ -206,18 +218,32 @@ export const WithLogic: Story = {
206218
dispatch({ type: 'DIALOG_RESTORE', payload: prevDialogOpenState.current });
207219
};
208220

221+
const handleFilterSelectionChange: FilterBarPropTypes['onFiltersDialogSelectionChange'] = (e) => {
222+
const { checked, element } = e.detail;
223+
if (checked) {
224+
dispatch({ type: 'SHOW_FILTER', payload: element.dataset.text });
225+
} else {
226+
dispatch({ type: 'HIDE_FILTER', payload: element.dataset.text });
227+
}
228+
};
229+
209230
return (
210231
<>
211232
<FilterBar
212233
showResetButton
213234
search={<Input onInput={handleSearch} />}
214235
onRestore={handleRestore}
215236
onFiltersDialogOpen={handleFiltersDialogOpen}
237+
onFiltersDialogSelectionChange={handleFilterSelectionChange}
216238
>
217239
<FilterGroupItem label="Age" active={!!age} required>
218240
<StepInput value={age} onChange={handleAgeChange} required />
219241
</FilterGroupItem>
220-
<FilterGroupItem label="Countries" active={Object.keys(countries).length > 0}>
242+
<FilterGroupItem
243+
label="Countries"
244+
active={Object.keys(countries).length > 0}
245+
visibleInFilterBar={selectedFiltersByLabel.includes('Countries')}
246+
>
221247
<MultiComboBox onSelectionChange={handleCountriesChange}>
222248
<MultiComboBoxItem text="Argentina" selected={countries.argentina} />
223249
<MultiComboBoxItem text="Bulgaria" selected={countries.bulgaria} />
@@ -228,7 +254,11 @@ export const WithLogic: Story = {
228254
<MultiComboBoxItem text="USA" selected={countries.usa} />
229255
</MultiComboBox>
230256
</FilterGroupItem>
231-
<FilterGroupItem label="Currency" active={!!currency}>
257+
<FilterGroupItem
258+
label="Currency"
259+
active={!!currency}
260+
visibleInFilterBar={selectedFiltersByLabel.includes('Currency')}
261+
>
232262
<Select onChange={handleCurrencyChange}>
233263
<Option additionalText="€" selected={currency === 'EUR'}>
234264
EUR
@@ -247,11 +277,15 @@ export const WithLogic: Story = {
247277
</Option>
248278
</Select>
249279
</FilterGroupItem>
250-
<FilterGroupItem label="Date" active={!!date}>
251-
<DatePicker value={date} onChange={handleDateChange} style={{ minWidth: 'auto' }} on />
280+
<FilterGroupItem label="Date" active={!!date} visibleInFilterBar={selectedFiltersByLabel.includes('Date')}>
281+
<DatePicker value={date} onChange={handleDateChange} style={{ minWidth: 'auto' }} />
252282
</FilterGroupItem>
253-
<FilterGroupItem label="Date Range" active={!!dateRange} visibleInFilterBar={false}>
254-
<DateRangePicker value={dateRange} onChange={handleDateRangeChange} style={{ minWidth: 'auto' }} on />
283+
<FilterGroupItem
284+
label="Date Range"
285+
active={!!dateRange}
286+
visibleInFilterBar={selectedFiltersByLabel.includes('Date Range')}
287+
>
288+
<DateRangePicker value={dateRange} onChange={handleDateRangeChange} style={{ minWidth: 'auto' }} />
255289
</FilterGroupItem>
256290
</FilterBar>
257291
<FlexBox direction={FlexBoxDirection.Column}>

0 commit comments

Comments
 (0)