-
-
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
feat: improve focus behavior #681
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> | ||
); | ||
}; |
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ import useFrame from './hooks/useFrame'; | |
export type { ValueType }; | ||
|
||
export interface InputNumberRef extends HTMLInputElement { | ||
focus: (options?: InputFocusOptions) => void; | ||
blur: () => void; | ||
nativeElement: HTMLElement; | ||
} | ||
|
||
|
@@ -660,6 +662,21 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r | |
|
||
React.useImperativeHandle(ref, () => | ||
proxyObject(inputFocusRef.current, { | ||
focus, | ||
blur: () => { | ||
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.
|
||
inputFocusRef.current?.blur(); | ||
}, | ||
setSelectionRange: ( | ||
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. 这边你忘记删了哈~ 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. 从这往下都是不需要的 |
||
start: number, | ||
end: number, | ||
direction?: 'forward' | 'backward' | 'none', | ||
) => { | ||
inputFocusRef.current?.setSelectionRange(start, end, direction); | ||
}, | ||
select: () => { | ||
inputFocusRef.current?.select(); | ||
}, | ||
input: inputFocusRef.current, | ||
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. 建议优化 focus 和 blur 方法的空值检查 根据之前的讨论,建议仅为 focus 和 blur 方法添加空值检查,以提高代码的健壮性。其他方法可以让用户通过 nativeElement 自行处理。 建议按如下方式修改: - focus,
+ focus: (option?: InputFocusOptions) => {
+ if (!inputFocusRef.current) return;
+ triggerFocus(inputFocusRef.current, option);
+ },
blur: () => {
+ if (!inputFocusRef.current) return;
inputFocusRef.current?.blur();
},
setSelectionRange: (
start: number,
end: number,
direction?: 'forward' | 'backward' | 'none',
) => {
inputFocusRef.current?.setSelectionRange(start, end, direction);
},
select: () => {
inputFocusRef.current?.select();
},
|
||
nativeElement: holderRef.current.nativeElement || inputNumberDomRef.current, | ||
}), | ||
); | ||
|
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);
}); |
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
建议改进按钮组的样式和结构
当前按钮组的布局比较简单,建议使用更规范的样式和结构: