Skip to content

Commit

Permalink
refactor prefilled flag
Browse files Browse the repository at this point in the history
  • Loading branch information
balanza committed Aug 7, 2024
1 parent c859461 commit 8d486a1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion assets/js/common/ComposedFilter/ComposedFilter.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const WithDateFilter = {
key: 'filter2',
type: 'date',
title: 'Date',
prefill: true,
prefilled: true,
options: [['My birthday', () => new Date(1986, 0, 24)]],
},
],
Expand Down
14 changes: 10 additions & 4 deletions assets/js/common/DateFilter/DateFilter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const getSelectedOption = (options, value) => {
* // options={['1h ago', '24h ago', '7d ago', '30d ago']}
* // title="Date"
* // value="24h ago"
* // prefill
* // prefilled
* // onChange={(value) => console.log(value)}
* // />
*
Expand All @@ -76,15 +76,21 @@ const getSelectedOption = (options, value) => {
* Options will be displayed sorted by date in descending order.
* @param {string} props.title - The title of the date filter, to be shown as placeholder when no value is selected.
* @param {string} props.value - The selected id of the selected option. It accepted either a string or an array with the id as the first element.
* @param {boolean} props.prefill - Whether to include pre-configured options in the options list. Default is false.
* @param {boolean} props.prefilled - Whether to include pre-configured options in the options list. Default is true.
* @param {function} props.onChange - The callback function to be called when the value of the date filter changes. It will provide a couple with the selected id and the actual date.
*/
function DateFilter({ options = [], title, value, prefill, onChange }) {
function DateFilter({
options = [],
title,
value,
prefilled = true,
onChange,
}) {
const ref = useRef();
const [open, setOpen] = useState(false);

const parsedOptions = parseInputOptions(
prefill ? [...Object.entries(preconfiguredOptions), ...options] : options
prefilled ? [...Object.entries(preconfiguredOptions), ...options] : options
);

const selectedOption = getSelectedOption(parsedOptions, value);
Expand Down
7 changes: 3 additions & 4 deletions assets/js/common/DateFilter/DateFilter.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default {
title={args.title}
options={args.options}
value={value}
prefill={args.prefill}
prefilled={args.prefilled}
onChange={(newValue) => {
setValue(newValue);
action('onChange')(newValue);
Expand All @@ -50,7 +50,6 @@ export default {
export const Default = {
args: {
title: 'by date',
prefill: true,
},
};

Expand Down Expand Up @@ -86,15 +85,15 @@ export const WithOverriddenOptions = {
export const WithPickedOptionsOnly = {
args: {
...Default.args,
prefill: false,
prefilled: false,
options: ['1h ago', '30d ago'],
},
};

export const WithCustomRenderer = {
args: {
...Default.args,
prefill: false,
prefilled: false,
options: [['epoch', () => new Date(0), () => '⌛ Beginning of time']],
},
};
14 changes: 7 additions & 7 deletions assets/js/common/DateFilter/DateFilter.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('DateFilter component', () => {
it('should render with pre-configured options', async () => {
const user = userEvent.setup();

render(<DateFilter title="by date" prefill onChange={jest.fn()} />);
render(<DateFilter title="by date" prefilled onChange={jest.fn()} />);

const placeholder = 'Filter by date...';

Expand All @@ -27,7 +27,7 @@ describe('DateFilter component', () => {
const user = userEvent.setup();
const mockOnChange = jest.fn();

render(<DateFilter title="by date" prefill onChange={mockOnChange} />);
render(<DateFilter title="by date" prefilled onChange={mockOnChange} />);

await act(() => user.click(screen.getByText('Filter by date...')));

Expand All @@ -40,7 +40,7 @@ describe('DateFilter component', () => {
render(
<DateFilter
title="by date"
prefill
prefilled
value="24h ago"
onChange={jest.fn()}
/>
Expand All @@ -53,7 +53,7 @@ describe('DateFilter component', () => {
render(
<DateFilter
title="by date"
prefill
prefilled
value={['24h ago', new Date()]}
onChange={jest.fn()}
/>
Expand All @@ -71,7 +71,7 @@ describe('DateFilter component', () => {
options={[
['Custom', () => new Date(Date.now() - 42 * 24 * 60 * 60 * 1000)],
]}
prefill
prefilled
onChange={jest.fn()}
/>
);
Expand All @@ -95,7 +95,7 @@ describe('DateFilter component', () => {
<DateFilter
title="by date"
options={[option]}
prefill={false}
prefilled={false}
onChange={jest.fn()}
/>
);
Expand All @@ -114,7 +114,7 @@ describe('DateFilter component', () => {
<DateFilter
title="by date"
options={[['30d ago', () => anyDate, () => 'my overridden item']]}
prefill
prefilled
onChange={mockOnChange}
/>
);
Expand Down

0 comments on commit 8d486a1

Please sign in to comment.