Skip to content

Commit

Permalink
feat: add setSelectionRange for the ref of Input and TextArea (#27584)
Browse files Browse the repository at this point in the history
Co-authored-by: lvpansen <pansen.lv@atzuche.com>
  • Loading branch information
2 people authored and yoyo837 committed Nov 19, 2020
1 parent 2ff971e commit 7c3b84e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components/input/Input.tsx
Expand Up @@ -179,6 +179,10 @@ class Input extends React.Component<InputProps, InputState> {
this.input.blur();
}

setSelectionRange(start: number, end: number, direction?: 'forward' | 'backward' | 'none') {
this.input.setSelectionRange(start, end, direction);
}

select() {
this.input.select();
}
Expand Down
9 changes: 9 additions & 0 deletions components/input/__tests__/index.test.js
Expand Up @@ -74,6 +74,15 @@ describe('Input', () => {
wrapper.unmount();
});
});

it('set mouse cursor position', () => {
const defaultValue = '11111';
const valLength = defaultValue.length;
const wrapper = mount(<Input autoFocus defaultValue={defaultValue} />);
wrapper.instance().setSelectionRange(valLength, valLength);
expect(wrapper.instance().input.selectionStart).toEqual(5);
expect(wrapper.instance().input.selectionEnd).toEqual(5);
});
});

describe('prefix and suffix', () => {
Expand Down
9 changes: 9 additions & 0 deletions components/input/__tests__/textarea.test.js
Expand Up @@ -172,6 +172,15 @@ describe('TextArea', () => {
expect(wrapper.find('textarea').hasClass('ant-input-lg')).toBe(true);
expect(wrapper.render()).toMatchSnapshot();
});

it('set mouse cursor position', () => {
const defaultValue = '11111';
const valLength = defaultValue.length;
const wrapper = mount(<TextArea autoFocus defaultValue={defaultValue} />);
wrapper.instance().setSelectionRange(valLength, valLength);
expect(wrapper.instance().resizableTextArea.textArea.selectionStart).toEqual(5);
expect(wrapper.instance().resizableTextArea.textArea.selectionEnd).toEqual(5);
});
});

describe('TextArea allowClear', () => {
Expand Down

0 comments on commit 7c3b84e

Please sign in to comment.