-
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: improve focus behavior * feat: add type definition * test: add test case * refactor: remove some api * chore: remove useless code
- Loading branch information
1 parent
5b46007
commit c418866
Showing
5 changed files
with
119 additions
and
0 deletions.
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(); | ||
}); | ||
}); |