-
-
Notifications
You must be signed in to change notification settings - Fork 175
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
Add Validator callback and provide prevValue in Parser callback #644
Open
bombillazo
wants to merge
13
commits into
react-component:master
Choose a base branch
from
bombillazo:master
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.
+114
−15
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
121606e
add diff function to compare inputs, have stricter number inputs
bombillazo 37f1ed2
store prev value and pass to parser/formatter callbacks
bombillazo 3650677
format value on every change
bombillazo c39b28d
add validtor prop callback to validate input before updating
bombillazo 7ac04d4
Merge branch 'react-component:master' into master
bombillazo 0c116c6
Merge branch 'react-component:master' into master
bombillazo 032a5e9
Merge branch 'react-component:master' into master
bombillazo c25fd57
chore: update file spacing
bombillazo bc42675
Merge branch 'master' into master
bombillazo 296327d
Merge branch 'master' into master
bombillazo 81e3375
chore: fix prevValue to be empty string if not defined
bombillazo fecd638
chore: add validator test
bombillazo afe99b4
Merge branch 'react-component:master' into master
bombillazo 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
There are no files selected for viewing
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,84 @@ | ||
import KeyCode from 'rc-util/lib/KeyCode'; | ||
import InputNumber from '../src'; | ||
import { fireEvent, render } from './util/wrapper'; | ||
|
||
describe('InputNumber.validator', () => { | ||
it('validator on direct input', () => { | ||
const onChange = jest.fn(); | ||
const { container } = render( | ||
<InputNumber | ||
defaultValue={0} | ||
validator={(num) => { | ||
return /^[0-9]*$/.test(num); | ||
}} | ||
onChange={onChange} | ||
/>, | ||
); | ||
const input = container.querySelector('input'); | ||
fireEvent.focus(input); | ||
|
||
fireEvent.change(input, { target: { value: 'a' } }); | ||
expect(input.value).toEqual('0'); | ||
fireEvent.change(input, { target: { value: '5' } }); | ||
expect(input.value).toEqual('5'); | ||
expect(onChange).toHaveBeenCalledWith(5); | ||
fireEvent.change(input, { target: { value: '10e' } }); | ||
expect(input.value).toEqual('5'); | ||
fireEvent.change(input, { target: { value: '_' } }); | ||
expect(input.value).toEqual('5'); | ||
fireEvent.change(input, { target: { value: '10' } }); | ||
expect(input.value).toEqual('10'); | ||
expect(onChange).toHaveBeenCalledWith(10); | ||
}); | ||
|
||
it('validator and formatter', () => { | ||
const onChange = jest.fn(); | ||
const { container } = render( | ||
<InputNumber | ||
defaultValue={1} | ||
formatter={(num) => `$ ${num} boeing 737`} | ||
validator={(num) => { | ||
return /^[0-9]*$/.test(num); | ||
}} | ||
onChange={onChange} | ||
/>, | ||
); | ||
const input = container.querySelector('input'); | ||
fireEvent.focus(input); | ||
|
||
expect(input.value).toEqual('$ 1 boeing 737'); | ||
fireEvent.change(input, { target: { value: '5' } }); | ||
expect(input.value).toEqual('$ 5 boeing 737'); | ||
|
||
fireEvent.keyDown(input, { | ||
which: KeyCode.UP, | ||
key: 'ArrowUp', | ||
code: 'ArrowUp', | ||
keyCode: KeyCode.UP, | ||
}); | ||
|
||
expect(input.value).toEqual('$ 6 boeing 737'); | ||
expect(onChange).toHaveBeenLastCalledWith(6); | ||
|
||
fireEvent.change(input, { target: { value: '#' } }); | ||
expect(input.value).toEqual('$ 6 boeing 737'); | ||
|
||
fireEvent.keyDown(input, { | ||
which: KeyCode.DOWN, | ||
key: 'ArrowDown', | ||
code: 'ArrowDown', | ||
keyCode: KeyCode.DOWN, | ||
}); | ||
|
||
expect(input.value).toEqual('$ 5 boeing 737'); | ||
expect(onChange).toHaveBeenLastCalledWith(5); | ||
|
||
fireEvent.mouseDown(container.querySelector('.rc-input-number-handler-up'), { | ||
which: KeyCode.DOWN, | ||
}); | ||
expect(input.value).toEqual('$ 6 boeing 737'); | ||
expect(onChange).toHaveBeenLastCalledWith(6); | ||
fireEvent.change(input, { target: { value: 'a' } }); | ||
expect(input.value).toEqual('$ 6 boeing 737'); | ||
}); | ||
bombillazo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
注意:
parser
和formatter
方法签名的更改可能引入破坏性变更在
InputNumberProps
接口中为parser
和formatter
方法添加新的参数info
,可能会导致现有使用这些属性的代码出现 TypeScript 类型错误,因为现有实现可能未预期新的info
参数。建议将新的info
参数设为可选,以保持向后兼容性。应用以下代码修改:
📝 Committable suggestion