From 7c3b84e41486cab176452a3803724d4dfc6a3825 Mon Sep 17 00:00:00 2001 From: appleshell Date: Fri, 6 Nov 2020 11:48:50 +0800 Subject: [PATCH] feat: add setSelectionRange for the ref of Input and TextArea (#27584) Co-authored-by: lvpansen --- components/input/Input.tsx | 4 ++++ components/input/__tests__/index.test.js | 9 +++++++++ components/input/__tests__/textarea.test.js | 9 +++++++++ 3 files changed, 22 insertions(+) diff --git a/components/input/Input.tsx b/components/input/Input.tsx index e48edb29d065..e8b1655f5253 100644 --- a/components/input/Input.tsx +++ b/components/input/Input.tsx @@ -179,6 +179,10 @@ class Input extends React.Component { this.input.blur(); } + setSelectionRange(start: number, end: number, direction?: 'forward' | 'backward' | 'none') { + this.input.setSelectionRange(start, end, direction); + } + select() { this.input.select(); } diff --git a/components/input/__tests__/index.test.js b/components/input/__tests__/index.test.js index 0268a9627b67..cd12f9c4abc7 100644 --- a/components/input/__tests__/index.test.js +++ b/components/input/__tests__/index.test.js @@ -74,6 +74,15 @@ describe('Input', () => { wrapper.unmount(); }); }); + + it('set mouse cursor position', () => { + const defaultValue = '11111'; + const valLength = defaultValue.length; + const wrapper = mount(); + wrapper.instance().setSelectionRange(valLength, valLength); + expect(wrapper.instance().input.selectionStart).toEqual(5); + expect(wrapper.instance().input.selectionEnd).toEqual(5); + }); }); describe('prefix and suffix', () => { diff --git a/components/input/__tests__/textarea.test.js b/components/input/__tests__/textarea.test.js index 01a5277c6651..3065373e8b1c 100644 --- a/components/input/__tests__/textarea.test.js +++ b/components/input/__tests__/textarea.test.js @@ -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(