From 7558cb8fcaa6548ed282e591b2f097c2f6c33613 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 7 Nov 2025 13:20:45 +0800 Subject: [PATCH 1/3] chore(deps-dev): bump eslint-plugin-jest from 28.14.0 to 29.0.1 (#695) Bumps [eslint-plugin-jest](https://github.com/jest-community/eslint-plugin-jest) from 28.14.0 to 29.0.1. - [Release notes](https://github.com/jest-community/eslint-plugin-jest/releases) - [Changelog](https://github.com/jest-community/eslint-plugin-jest/blob/main/CHANGELOG.md) - [Commits](https://github.com/jest-community/eslint-plugin-jest/compare/v28.14.0...v29.0.1) --- updated-dependencies: - dependency-name: eslint-plugin-jest dependency-version: 29.0.1 dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 75ad891d..8831fc81 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "cross-env": "^7.0.2", "dumi": "^2.1.3", "eslint": "^8.54.0", - "eslint-plugin-jest": "^28.2.0", + "eslint-plugin-jest": "^29.0.1", "eslint-plugin-unicorn": "^56.0.1", "father": "^4.1.3", "glob": "^11.0.3", From e213fe24dd03fc58147fdce2a1ca6a637b0b7ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Mon, 10 Nov 2025 16:59:38 +0800 Subject: [PATCH 2/3] feat: triggerFocus support (#709) --- src/Dom/focus.ts | 40 ++++++++++++++++++++++++++++++++++++++++ tests/focus.test.ts | 27 ++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/Dom/focus.ts b/src/Dom/focus.ts index a6654841..6b182463 100644 --- a/src/Dom/focus.ts +++ b/src/Dom/focus.ts @@ -97,3 +97,43 @@ export function limitTabRange(node: HTMLElement, e: KeyboardEvent) { } } } + +export interface InputFocusOptions extends FocusOptions { + cursor?: 'start' | 'end' | 'all'; +} + +// Used for `rc-input` `rc-textarea` `rc-input-number` +/** + * Focus element and set cursor position for input/textarea elements. + */ +export function triggerFocus( + element?: HTMLElement, + option?: InputFocusOptions, +) { + if (!element) return; + + element.focus(option); + + // Selection content + const { cursor } = option || {}; + if ( + cursor && + (element instanceof HTMLInputElement || + element instanceof HTMLTextAreaElement) + ) { + const len = element.value.length; + + switch (cursor) { + case 'start': + element.setSelectionRange(0, 0); + break; + + case 'end': + element.setSelectionRange(len, len); + break; + + default: + element.setSelectionRange(0, len); + } + } +} diff --git a/tests/focus.test.ts b/tests/focus.test.ts index 795e2e71..1c835bf9 100644 --- a/tests/focus.test.ts +++ b/tests/focus.test.ts @@ -1,6 +1,6 @@ /* eslint-disable class-methods-use-this */ import { spyElementPrototype } from '../src/test/domHook'; -import { getFocusNodeList } from '../src/Dom/focus'; +import { getFocusNodeList, triggerFocus } from '../src/Dom/focus'; describe('focus', () => { beforeAll(() => { @@ -31,4 +31,29 @@ describe('focus', () => { const tabFocusList = getFocusNodeList(div, true); expect(tabFocusList).toHaveLength(5); }); + + it('triggerFocus should set cursor position for textarea', () => { + const textarea = document.createElement('textarea'); + textarea.value = 'test content'; + + const focusSpy = jest.spyOn(textarea, 'focus'); + const setSelectionRangeSpy = jest.spyOn(textarea, 'setSelectionRange'); + + // Test cursor: 'start' + triggerFocus(textarea, { cursor: 'start' }); + expect(setSelectionRangeSpy).toHaveBeenCalledWith(0, 0); + + // Test cursor: 'end' + triggerFocus(textarea, { cursor: 'end' }); + expect(setSelectionRangeSpy).toHaveBeenCalledWith(12, 12); // 'test content'.length = 12 + + // Test cursor: 'all' + triggerFocus(textarea, { cursor: 'all' }); + expect(setSelectionRangeSpy).toHaveBeenCalledWith(0, 12); // select all text + + expect(focusSpy).toHaveBeenCalledTimes(3); + + focusSpy.mockRestore(); + setSelectionRangeSpy.mockRestore(); + }); }); From 4d24a06fa9ae01d57bf4a08513323b17cd5f1f33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Mon, 10 Nov 2025 17:03:16 +0800 Subject: [PATCH 3/3] chore: bump version to 1.4.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8831fc81..3cf7898f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rc-component/util", - "version": "1.3.1", + "version": "1.4.0", "description": "Common Utils For React Component", "keywords": [ "react",