-
-
Notifications
You must be signed in to change notification settings - Fork 174
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: improve focus behavior #681
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* eslint no-console:0 */ | ||
import InputNumber, { InputNumberRef } from 'rc-input-number'; | ||
import React from 'react'; | ||
import '../../assets/index.less'; | ||
|
||
export default () => { | ||
const inputRef = React.useRef<InputNumberRef>(null); | ||
|
||
return ( | ||
<div style={{ margin: 10 }}> | ||
<InputNumber aria-label="focus example" value={10} style={{ width: 100 }} ref={inputRef} /> | ||
<div style={{ marginTop: 10 }}> | ||
<button type="button" onClick={() => inputRef.current?.focus({ cursor: 'start' })}> | ||
focus at start | ||
</button> | ||
<button type="button" onClick={() => inputRef.current?.focus({ cursor: 'end' })}> | ||
focus at end | ||
</button> | ||
<button type="button" onClick={() => inputRef.current?.focus({ cursor: 'all' })}> | ||
focus to select all | ||
</button> | ||
<button type="button" onClick={() => inputRef.current?.focus({ preventScroll: true })}> | ||
focus prevent scroll | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
}; |
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 |
---|---|---|
|
@@ -45,4 +45,6 @@ nav: | |
|
||
<code src="./demo/wheel.tsx"></code> | ||
|
||
## focus | ||
|
||
<code src="./demo/focus.tsx"></code> |
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,66 @@ | ||
import { fireEvent, render } from '@testing-library/react'; | ||
import InputNumber, { InputNumberRef } from 'rc-input-number'; | ||
import { spyElementPrototypes } from 'rc-util/lib/test/domHook'; | ||
import React from 'react'; | ||
|
||
const getInputRef = () => { | ||
const ref = React.createRef<InputNumberRef>(); | ||
render(<InputNumber ref={ref} defaultValue={12345} />); | ||
return ref; | ||
}; | ||
|
||
describe('InputNumber.Focus', () => { | ||
let inputSpy: ReturnType<typeof spyElementPrototypes>; | ||
let focus: ReturnType<typeof jest.fn>; | ||
let setSelectionRange: ReturnType<typeof jest.fn>; | ||
|
||
beforeEach(() => { | ||
focus = jest.fn(); | ||
setSelectionRange = jest.fn(); | ||
inputSpy = spyElementPrototypes(HTMLInputElement, { | ||
focus, | ||
setSelectionRange, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
inputSpy.mockRestore(); | ||
}); | ||
|
||
it('start', () => { | ||
const input = getInputRef(); | ||
input.current?.focus({ cursor: 'start' }); | ||
|
||
expect(focus).toHaveBeenCalled(); | ||
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 0, 0); | ||
}); | ||
|
||
it('end', () => { | ||
const input = getInputRef(); | ||
input.current?.focus({ cursor: 'end' }); | ||
|
||
expect(focus).toHaveBeenCalled(); | ||
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 5, 5); | ||
}); | ||
|
||
it('all', () => { | ||
const input = getInputRef(); | ||
input.current?.focus({ cursor: 'all' }); | ||
|
||
expect(focus).toHaveBeenCalled(); | ||
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 0, 5); | ||
}); | ||
|
||
it('disabled should reset focus', () => { | ||
const { container, rerender } = render(<InputNumber prefixCls="rc-input-number" />); | ||
const input = container.querySelector('input')!; | ||
|
||
fireEvent.focus(input); | ||
expect(container.querySelector('.rc-input-number-focused')).toBeTruthy(); | ||
|
||
rerender(<InputNumber prefixCls="rc-input-number" disabled />); | ||
fireEvent.blur(input); | ||
|
||
expect(container.querySelector('.rc-input-number-focused')).toBeFalsy(); | ||
}); | ||
}); | ||
Comment on lines
+30
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 建议补充更多测试用例 现有的测试用例覆盖了基本的焦点行为,但还可以增加以下测试场景:
建议添加如下测试用例: it('blur', () => {
const input = getInputRef();
input.current?.focus();
input.current?.blur();
expect(blur).toHaveBeenCalled();
});
it('select', () => {
const input = getInputRef();
input.current?.select();
expect(focus).toHaveBeenCalled();
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 0, 5);
}); |
Oops, something went wrong.
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
建议改进按钮组的样式和结构
当前按钮组的布局比较简单,建议使用更规范的样式和结构: