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

feat: preset value support callback function #654

Merged
merged 8 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions docs/examples/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export default () => {
label: 'Hello World!',
value: moment(),
},
{
label: 'Now',
value: moment,
Wxh16144 marked this conversation as resolved.
Show resolved Hide resolved
Wxh16144 marked this conversation as resolved.
Show resolved Hide resolved
}
],
};

Expand Down
10 changes: 10 additions & 0 deletions docs/examples/range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ export default () => {
defaultValue={[moment('1990-09-03'), moment('1989-11-28')]}
clearIcon={<span>X</span>}
suffixIcon={<span>O</span>}
presets={[
{
label: 'Last week',
value: [moment().subtract(1, 'week'), moment()],
},
{
label: 'Last 3 days',
value: () => [moment().subtract(3, 'days'), moment().add(3, 'days')],
},
]}
/>
<RangePicker<Moment>
{...sharedProps}
Expand Down
12 changes: 10 additions & 2 deletions src/PresetPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ export default function PresetPanel<T>(props: PresetPanelProps<T>) {
<li
key={index}
onClick={() => {
onClick(value);
onClick?.(
typeof value === 'function'
? (value as any)()
: value
);
}}
onMouseEnter={() => {
onHover?.(value);
onHover?.(
typeof value === 'function'
? (value as any)()
: value
);
Wxh16144 marked this conversation as resolved.
Show resolved Hide resolved
}}
onMouseLeave={() => {
onHover?.(null);
Expand Down
2 changes: 1 addition & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export type CustomFormat<DateType> = (value: DateType) => string;

export interface PresetDate<T> {
label: React.ReactNode;
value: T;
value: T | (() => T);
}

// https://stackoverflow.com/a/39495173; need TypeScript >= 4.5
Expand Down
35 changes: 35 additions & 0 deletions tests/picker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1030,4 +1030,39 @@ describe('Picker.Basic', () => {

expect(onChange.mock.calls[0][0].format('YYYY-MM-DD')).toEqual('2000-09-03');
});

it('presets support callback', () => {
Wxh16144 marked this conversation as resolved.
Show resolved Hide resolved
const onChange = jest.fn();
const mockPresetValue = jest.fn().mockImplementationOnce(() => moment('2000-09-03'));

render(
<MomentPicker
onChange={onChange}
open
presets={[
{
label: 'Bamboo',
value: mockPresetValue,
},
]}
/>,
);

const firstPreset = document.querySelector('.rc-picker-presets li');
expect(firstPreset.textContent).toBe('Bamboo');

fireEvent.click(firstPreset);

expect(mockPresetValue).toHaveBeenCalled();
expect(onChange.mock.calls[0][0].format('YYYY-MM-DD')).toEqual('2000-09-03');

mockPresetValue.mockImplementationOnce(() => moment('2023-05-01 12:34:56'));

fireEvent.click(firstPreset);

expect(mockPresetValue).toBeCalledTimes(2);
expect(onChange).toBeCalledTimes(2);

expect(onChange.mock.calls[1][0].format('YYYY-MM-DD HH:mm:ss')).toEqual('2023-05-01 12:34:56');
});
});