mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-28 05:05:48 +08:00
feat: add setSelectionRange for the ref of Input and TextArea (#27584)
Co-authored-by: lvpansen <pansen.lv@atzuche.com>
This commit is contained in:
parent
e4fb3d04cb
commit
8f18328aad
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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', () => {
|
||||
|
@ -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', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user