二货机器人
4 years ago
committed by
GitHub
7 changed files with 306 additions and 10 deletions
@ -0,0 +1,58 @@ |
|||
import React from 'react'; |
|||
import { mount } from 'enzyme'; |
|||
import { spyElementPrototypes } from 'rc-util/lib/test/domHook'; |
|||
import Input from '..'; |
|||
|
|||
const { TextArea } = Input; |
|||
|
|||
describe('Input.Focus', () => { |
|||
let inputSpy: ReturnType<typeof spyElementPrototypes>; |
|||
let textareaSpy: 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, |
|||
}); |
|||
textareaSpy = spyElementPrototypes(HTMLTextAreaElement, { |
|||
focus, |
|||
setSelectionRange, |
|||
}); |
|||
}); |
|||
|
|||
afterEach(() => { |
|||
inputSpy.mockRestore(); |
|||
textareaSpy.mockRestore(); |
|||
}); |
|||
|
|||
it('start', () => { |
|||
const ref = React.createRef<Input>(); |
|||
mount(<Input ref={ref} defaultValue="light" />); |
|||
ref.current!.focus({ cursor: 'start' }); |
|||
|
|||
expect(focus).toHaveBeenCalled(); |
|||
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 0, 0); |
|||
}); |
|||
|
|||
it('end', () => { |
|||
const ref = React.createRef<Input>(); |
|||
mount(<Input ref={ref} defaultValue="light" />); |
|||
ref.current!.focus({ cursor: 'end' }); |
|||
|
|||
expect(focus).toHaveBeenCalled(); |
|||
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 5, 5); |
|||
}); |
|||
|
|||
it('all', () => { |
|||
const ref = React.createRef<any>(); |
|||
mount(<TextArea ref={ref} defaultValue="light" />); |
|||
ref.current!.focus({ cursor: 'all' }); |
|||
|
|||
expect(focus).toHaveBeenCalled(); |
|||
expect(setSelectionRange).toHaveBeenCalledWith(expect.anything(), 0, 5); |
|||
}); |
|||
}); |
@ -0,0 +1,84 @@ |
|||
--- |
|||
order: 21 |
|||
title: |
|||
zh-CN: 聚焦 |
|||
en-US: Focus |
|||
--- |
|||
|
|||
## zh-CN |
|||
|
|||
聚焦额外配置属性。 |
|||
|
|||
## en-US |
|||
|
|||
Focus with additional option. |
|||
|
|||
```tsx |
|||
import { Input, Space, Button, Switch } from 'antd'; |
|||
|
|||
const Demo = () => { |
|||
const inputRef = React.useRef<any>(null); |
|||
const [input, setInput] = React.useState(true); |
|||
|
|||
const sharedProps = { |
|||
style: { width: '100%' }, |
|||
defaultValue: 'Ant Design love you!', |
|||
ref: inputRef, |
|||
}; |
|||
|
|||
return ( |
|||
<Space direction="vertical" style={{ width: '100%' }}> |
|||
<Space> |
|||
<Button |
|||
onClick={() => { |
|||
inputRef.current!.focus({ |
|||
cursor: 'start', |
|||
}); |
|||
}} |
|||
> |
|||
Focus at first |
|||
</Button> |
|||
<Button |
|||
onClick={() => { |
|||
inputRef.current!.focus({ |
|||
cursor: 'end', |
|||
}); |
|||
}} |
|||
> |
|||
Focus at last |
|||
</Button> |
|||
<Button |
|||
onClick={() => { |
|||
inputRef.current!.focus({ |
|||
cursor: 'all', |
|||
}); |
|||
}} |
|||
> |
|||
Focus to select all |
|||
</Button> |
|||
<Button |
|||
onClick={() => { |
|||
inputRef.current!.focus({ |
|||
preventScroll: true, |
|||
}); |
|||
}} |
|||
> |
|||
Focus prevent scroll |
|||
</Button> |
|||
<Switch |
|||
checked={input} |
|||
checkedChildren="Input" |
|||
unCheckedChildren="TextArea" |
|||
onChange={() => { |
|||
setInput(!input); |
|||
}} |
|||
/> |
|||
</Space> |
|||
|
|||
{input ? <Input {...sharedProps} /> : <Input.TextArea {...sharedProps} />} |
|||
</Space> |
|||
); |
|||
}; |
|||
|
|||
ReactDOM.render(<Demo />, mountNode); |
|||
``` |
Loading…
Reference in new issue