feat: add setSelectionRange for the ref of Input and TextArea (#27584)

Co-authored-by: lvpansen <pansen.lv@atzuche.com>
This commit is contained in:
appleshell 2020-11-06 11:48:50 +08:00 committed by GitHub
parent e4fb3d04cb
commit 8f18328aad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 0 deletions

View File

@ -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();
}

View File

@ -58,6 +58,10 @@ class TextArea extends React.Component<TextAreaProps, TextAreaState> {
this.resizableTextArea.textArea.blur();
}
setSelectionRange(start: number, end: number, direction?: 'forward' | 'backward' | 'none') {
this.resizableTextArea.textArea.setSelectionRange(start, end, direction);
}
saveTextArea = (textarea: RcTextArea) => {
this.resizableTextArea = textarea?.resizableTextArea;
};

View File

@ -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', () => {

View File

@ -161,6 +161,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', () => {