generated from react-component/footer
-
-
Notifications
You must be signed in to change notification settings - Fork 335
fix: trigger onChange when manually clearing input via select all + delete #947
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
Open
alii13
wants to merge
9
commits into
react-component:master
Choose a base branch
from
alii13:fix/manual-clear-input
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+329
−6
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d5bba8d
fix: trigger onChange when manually clearing input via select all + d…
alii13 88c80f2
chore: add relevant comment
alii13 fa46b0c
refactor: extract handleManualClear to reduce code duplication
alii13 a3d4b3c
refactor: rename onSingleChange to handleSingleChange for clarity
alii13 acb8fdb
fix: review comment
alii13 0172a1a
test: add coverage for formatted input manual clear
alii13 91955bd
fix: implement manual clear via onChange detection instead of keyDown
alii13 669467f
test: fix misleading test names for RangePicker manual clear
alii13 81b32b6
fix: test
alii13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,304 @@ | ||
| import { fireEvent, render } from '@testing-library/react'; | ||
| import React from 'react'; | ||
| import { Picker, RangePicker } from '../src'; | ||
| import dayGenerateConfig from '../src/generate/dayjs'; | ||
| import enUS from '../src/locale/en_US'; | ||
| import { getDay, openPicker, waitFakeTimer } from './util/commonUtil'; | ||
|
|
||
| describe('Picker.ManualClear', () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers().setSystemTime(getDay('1990-09-03 00:00:00').valueOf()); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllTimers(); | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| describe('Single Picker', () => { | ||
| it('should trigger onChange when manually clearing input', async () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <Picker | ||
| generateConfig={dayGenerateConfig} | ||
| value={getDay('2023-08-01')} | ||
| onChange={onChange} | ||
| locale={enUS} | ||
| allowClear | ||
| />, | ||
| ); | ||
|
|
||
| const input = container.querySelector('input') as HTMLInputElement; | ||
|
|
||
| openPicker(container); | ||
| fireEvent.change(input, { target: { value: '' } }); | ||
|
|
||
| await waitFakeTimer(); | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith(null, null); | ||
| }); | ||
|
|
||
| it('should NOT clear when allowClear is disabled - reset to previous value', async () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <Picker | ||
| generateConfig={dayGenerateConfig} | ||
| value={getDay('2023-08-01')} | ||
| onChange={onChange} | ||
| locale={enUS} | ||
| allowClear={false} | ||
| />, | ||
| ); | ||
|
|
||
| const input = container.querySelector('input') as HTMLInputElement; | ||
|
|
||
| expect(input.value).toBe('2023-08-01'); | ||
|
|
||
| openPicker(container); | ||
| fireEvent.change(input, { target: { value: '' } }); | ||
| fireEvent.blur(input); | ||
|
|
||
| await waitFakeTimer(); | ||
|
|
||
| expect(onChange).not.toHaveBeenCalled(); | ||
| expect(input.value).toBe('2023-08-01'); | ||
| }); | ||
|
|
||
| it('should reset invalid partial input on blur without triggering onChange', async () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <Picker | ||
| generateConfig={dayGenerateConfig} | ||
| value={getDay('2023-08-01')} | ||
| onChange={onChange} | ||
| locale={enUS} | ||
| format="YYYY-MM-DD" | ||
| />, | ||
| ); | ||
| const input = container.querySelector('input') as HTMLInputElement; | ||
| openPicker(container); | ||
| fireEvent.change(input, { target: { value: '2023-08' } }); | ||
| const initialOnChangeCallCount = onChange.mock.calls.length; | ||
| fireEvent.blur(input); | ||
| await waitFakeTimer(); | ||
| expect(onChange.mock.calls.length).toBe(initialOnChangeCallCount); | ||
| expect(input.value).toBe('2023-08-01'); | ||
| }); | ||
|
|
||
| it('should work with different picker modes', async () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <Picker | ||
| generateConfig={dayGenerateConfig} | ||
| value={getDay('2023-08-01')} | ||
| onChange={onChange} | ||
| locale={enUS} | ||
| picker="month" | ||
| allowClear | ||
| />, | ||
| ); | ||
|
|
||
| const input = container.querySelector('input') as HTMLInputElement; | ||
|
|
||
| openPicker(container); | ||
| fireEvent.change(input, { target: { value: '' } }); | ||
|
|
||
| await waitFakeTimer(); | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith(null, null); | ||
| }); | ||
|
|
||
| it('should clear input value when manually clearing', async () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <Picker | ||
| generateConfig={dayGenerateConfig} | ||
| value={getDay('2023-08-01')} | ||
| onChange={onChange} | ||
| locale={enUS} | ||
| allowClear | ||
| />, | ||
| ); | ||
|
|
||
| const input = container.querySelector('input') as HTMLInputElement; | ||
|
|
||
| expect(input.value).toBe('2023-08-01'); | ||
|
|
||
| openPicker(container); | ||
| fireEvent.change(input, { target: { value: '' } }); | ||
|
|
||
| await waitFakeTimer(); | ||
|
|
||
| expect(input.value).toBe(''); | ||
| }); | ||
|
|
||
| it('should clear formatted input with mask format', async () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <Picker | ||
| generateConfig={dayGenerateConfig} | ||
| value={getDay('2023-08-01')} | ||
| onChange={onChange} | ||
| locale={enUS} | ||
| format={{ type: 'mask', format: 'YYYY-MM-DD' }} | ||
| allowClear | ||
| />, | ||
| ); | ||
|
|
||
| const input = container.querySelector('input') as HTMLInputElement; | ||
|
|
||
| openPicker(container); | ||
| fireEvent.change(input, { target: { value: '' } }); | ||
|
|
||
| await waitFakeTimer(); | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith(null, null); | ||
| expect(input.value).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Range Picker', () => { | ||
| it('should clear start input value when manually clearing', async () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <RangePicker | ||
| generateConfig={dayGenerateConfig} | ||
| value={[getDay('2023-08-01'), getDay('2023-08-15')]} | ||
| onChange={onChange} | ||
| locale={enUS} | ||
| needConfirm={false} | ||
| allowClear | ||
| />, | ||
| ); | ||
|
|
||
| const startInput = container.querySelectorAll('input')[0] as HTMLInputElement; | ||
|
|
||
| openPicker(container, 0); | ||
| fireEvent.change(startInput, { target: { value: '' } }); | ||
| fireEvent.blur(startInput); | ||
|
|
||
| await waitFakeTimer(); | ||
|
|
||
| expect(startInput.value).toBe(''); | ||
| }); | ||
|
|
||
| it('should clear end input value when manually clearing', async () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <RangePicker | ||
| generateConfig={dayGenerateConfig} | ||
| value={[getDay('2023-08-01'), getDay('2023-08-15')]} | ||
| onChange={onChange} | ||
| locale={enUS} | ||
| needConfirm={false} | ||
| allowClear | ||
| />, | ||
| ); | ||
|
|
||
| const endInput = container.querySelectorAll('input')[1] as HTMLInputElement; | ||
|
|
||
| openPicker(container, 1); | ||
| fireEvent.change(endInput, { target: { value: '' } }); | ||
| fireEvent.blur(endInput); | ||
|
|
||
| await waitFakeTimer(); | ||
|
|
||
| expect(endInput.value).toBe(''); | ||
alii13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| it('should clear both input values when manually clearing', async () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <RangePicker | ||
| generateConfig={dayGenerateConfig} | ||
| value={[getDay('2023-08-01'), getDay('2023-08-15')]} | ||
| onChange={onChange} | ||
| locale={enUS} | ||
| needConfirm={false} | ||
| allowClear | ||
| />, | ||
| ); | ||
|
|
||
| const startInput = container.querySelectorAll('input')[0] as HTMLInputElement; | ||
| const endInput = container.querySelectorAll('input')[1] as HTMLInputElement; | ||
|
|
||
| openPicker(container, 0); | ||
| fireEvent.change(startInput, { target: { value: '' } }); | ||
| fireEvent.blur(startInput); | ||
| await waitFakeTimer(); | ||
|
|
||
| openPicker(container, 1); | ||
| fireEvent.change(endInput, { target: { value: '' } }); | ||
| fireEvent.blur(endInput); | ||
| await waitFakeTimer(); | ||
|
|
||
| expect(startInput.value).toBe(''); | ||
| expect(endInput.value).toBe(''); | ||
alii13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| it('should clear input values when manually clearing', async () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <RangePicker | ||
| generateConfig={dayGenerateConfig} | ||
| value={[getDay('2023-08-01'), getDay('2023-08-15')]} | ||
| onChange={onChange} | ||
| locale={enUS} | ||
| allowClear | ||
| />, | ||
| ); | ||
|
|
||
| const startInput = container.querySelectorAll('input')[0] as HTMLInputElement; | ||
|
|
||
| expect(startInput.value).toBe('2023-08-01'); | ||
|
|
||
| openPicker(container, 0); | ||
| fireEvent.change(startInput, { target: { value: '' } }); | ||
|
|
||
| await waitFakeTimer(); | ||
|
|
||
| expect(startInput.value).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Comparison with clear button', () => { | ||
| it('manual clear should behave the same as clear button for Picker', async () => { | ||
| const onChangeManual = jest.fn(); | ||
| const onChangeClear = jest.fn(); | ||
|
|
||
| const { container: container1 } = render( | ||
| <Picker | ||
| generateConfig={dayGenerateConfig} | ||
| value={getDay('2023-08-01')} | ||
| onChange={onChangeManual} | ||
| locale={enUS} | ||
| allowClear | ||
| />, | ||
| ); | ||
|
|
||
| const input1 = container1.querySelector('input') as HTMLInputElement; | ||
| openPicker(container1); | ||
| fireEvent.change(input1, { target: { value: '' } }); | ||
| await waitFakeTimer(); | ||
|
|
||
| const { container: container2 } = render( | ||
| <Picker | ||
| generateConfig={dayGenerateConfig} | ||
| value={getDay('2023-08-01')} | ||
| onChange={onChangeClear} | ||
| locale={enUS} | ||
| allowClear | ||
| />, | ||
| ); | ||
|
|
||
| const clearBtn = container2.querySelector('.rc-picker-clear'); | ||
| fireEvent.mouseDown(clearBtn); | ||
| fireEvent.mouseUp(clearBtn); | ||
| fireEvent.click(clearBtn); | ||
| await waitFakeTimer(); | ||
|
|
||
| expect(onChangeManual).toHaveBeenCalledWith(null, null); | ||
| expect(onChangeClear).toHaveBeenCalledWith(null, null); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.