UNPKG

@cn-ui/core

Version:

The @cn-ui/core is a collection of UI components and utilities for building modern web applications with SolidJS.

35 lines (30 loc) 778 B
export interface FocusOptions { preventScroll?: boolean; cursor?: "end" | "start" | "all"; } /** 聚焦在某个元素上 */ export function triggerFocus( element?: HTMLInputElement | HTMLTextAreaElement, option: FocusOptions = {}, ) { if (!element) { return; } element.focus(option); // Selection content const { cursor } = option; if (cursor) { 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); break; } } }