From fbec7d38d0f22dadc8a715f74d2c0fa1e255cd7e Mon Sep 17 00:00:00 2001 From: hanyuxinting Date: Wed, 21 May 2025 16:57:08 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat(Textarea):=20=E5=A2=9E=E5=8A=A0=20ref?= =?UTF-8?q?=20=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/packages/input/doc.en-US.md | 11 + src/packages/input/doc.md | 11 + src/packages/input/doc.taro.md | 11 + src/packages/input/doc.zh-TW.md | 11 + src/packages/textarea/doc.en-US.md | 11 + src/packages/textarea/doc.md | 11 + src/packages/textarea/doc.taro.md | 11 + src/packages/textarea/doc.zh-TW.md | 11 + src/packages/textarea/textarea.taro.tsx | 37 ++- src/packages/textarea/textarea.tsx | 260 ++++++++++-------- .../doc/docs/react/migrate-from-v2.en-US.md | 1 + .../doc/docs/react/migrate-from-v2.md | 1 + .../doc/docs/taro/migrate-from-v2.en-US.md | 1 + .../doc/docs/taro/migrate-from-v2.md | 1 + 14 files changed, 258 insertions(+), 131 deletions(-) diff --git a/src/packages/input/doc.en-US.md b/src/packages/input/doc.en-US.md index 3184cb85b5..b619d0b76d 100644 --- a/src/packages/input/doc.en-US.md +++ b/src/packages/input/doc.en-US.md @@ -148,6 +148,17 @@ import { Input } from '@nutui/nutui-react' | onClear | Triggered when the clear button is clicked | `(value: string) => void` | `-` | | onClick | Triggered when the input container is clicked | `(value: MouseEvent) => void` | `-` | +### Ref + +You can get Ref of Input. + +| Event | Description | Arguments | +| --- | --- | --- | +| clear | clear the value of input | `-` | +| focus | focus the input | `-` | +| blur | blur the input | `-` | +| get | get the input ref | `-` | + ## Theming ### CSS Variables diff --git a/src/packages/input/doc.md b/src/packages/input/doc.md index 4afade339a..4648149b74 100644 --- a/src/packages/input/doc.md +++ b/src/packages/input/doc.md @@ -151,6 +151,17 @@ import { Input } from '@nutui/nutui-react' 此外还支持以下原生属性:`onCompositionStart` `onCompositionEnd` +### Ref + +通过 ref 可以获取到 Input 实例并调用实例方法。 + +| 方法名 | 说明 | 参数 | +| --- | --- | --- | +| clear | 清除容器中的数据 | `-` | +| focus | 使容器获取焦点 | `-` | +| blur | 使容器失去焦点 | `-` | +| get | 获取当前容器 | `-` | + ## 主题定制 ### 样式变量 diff --git a/src/packages/input/doc.taro.md b/src/packages/input/doc.taro.md index dd30bafc90..4896dbd987 100644 --- a/src/packages/input/doc.taro.md +++ b/src/packages/input/doc.taro.md @@ -151,6 +151,17 @@ import { Input } from '@nutui/nutui-react-taro' 此外还支持 Taro 中的 [Input 属性](https://docs.taro.zone/docs/components/forms/input/) +### Ref + +通过 ref 可以获取到 Input 实例并调用实例方法。 + +| 方法名 | 说明 | 参数 | +| --- | --- | --- | +| clear | 清除容器中的数据 | `-` | +| focus | 使容器获取焦点 | `-` | +| blur | 使容器失去焦点 | `-` | +| get | 获取当前容器 | `-` | + ## 主题定制 ### 样式变量 diff --git a/src/packages/input/doc.zh-TW.md b/src/packages/input/doc.zh-TW.md index f430a7406f..16e85a7dca 100644 --- a/src/packages/input/doc.zh-TW.md +++ b/src/packages/input/doc.zh-TW.md @@ -148,6 +148,17 @@ import { Input } from '@nutui/nutui-react' | onClear | 点击清空按钮时触发 | `(value: string) => void` | `-` | | onClick | 点击 input 容器触发 | `(value: MouseEvent) => void` | `-` | +### Ref + +通過 ref 可以獲取到 Input 實例並調用實例方法。 + +| 方法名 | 說明 | 參數 | +| --- | --- | --- | +| clear | 清除容器中的數據 | `-` | +| focus | 使容器獲取焦點 | `-` | +| blur | 使容器失去焦點 | `-` | +| get | 獲取當前容器 | `-` | + ## 主题定制 ### 样式变量 diff --git a/src/packages/textarea/doc.en-US.md b/src/packages/textarea/doc.en-US.md index 135a6061a4..aa8c8a7c4a 100644 --- a/src/packages/textarea/doc.en-US.md +++ b/src/packages/textarea/doc.en-US.md @@ -95,6 +95,17 @@ import { TextArea } from '@nutui/nutui-react' | onFocus | Triggered when focusing | `(event: FocusEvent) => void` | `-` | | onBlur | Triggered when out of focus | `(event: FocusEvent) => void` | `-` | +### Ref + +You can get Ref of Textarea. + +| Event | Description | Arguments | +| --- | --- | --- | +| clear | clear the value of textarea | `-` | +| focus | focus the textarea | `-` | +| blur | blur the textarea | `-` | +| get | get the textarea ref | `-` | + ## Theming ### CSS Variables diff --git a/src/packages/textarea/doc.md b/src/packages/textarea/doc.md index a0d6979f2f..184321a204 100644 --- a/src/packages/textarea/doc.md +++ b/src/packages/textarea/doc.md @@ -95,6 +95,17 @@ import { TextArea } from '@nutui/nutui-react' | onFocus | 聚焦时触发 | `(event: FocusEvent) => void` | `-` | | onBlur | 失焦时触发 | `(event: FocusEvent) => void` | `-` | +### Ref + +通过 ref 可以获取到 Textarea 实例并调用实例方法。 + +| 方法名 | 说明 | 参数 | +| --- | --- | --- | +| clear | 清除容器中的数据 | `-` | +| focus | 使容器获取焦点 | `-` | +| blur | 使容器失去焦点 | `-` | +| get | 获取当前容器 | `-` | + ## 主题定制 ### 样式变量 diff --git a/src/packages/textarea/doc.taro.md b/src/packages/textarea/doc.taro.md index e8d62792f2..8a6d7eb37a 100644 --- a/src/packages/textarea/doc.taro.md +++ b/src/packages/textarea/doc.taro.md @@ -95,6 +95,17 @@ import { TextArea } from '@nutui/nutui-react-taro' | onFocus | 聚焦时触发 | `(event) => void` | `-` | | onBlur | 失焦时触发 | `(event) => void` | `-` | +### Ref + +通过 ref 可以获取到 Textarea 实例并调用实例方法。 + +| 方法名 | 说明 | 参数 | +| --- | --- | --- | +| clear | 清除容器中的数据 | `-` | +| focus | 使容器获取焦点 | `-` | +| blur | 使容器失去焦点 | `-` | +| get | 获取当前容器 | `-` | + ## 主题定制 ### 样式变量 diff --git a/src/packages/textarea/doc.zh-TW.md b/src/packages/textarea/doc.zh-TW.md index b3c633436d..c3d9e2d727 100644 --- a/src/packages/textarea/doc.zh-TW.md +++ b/src/packages/textarea/doc.zh-TW.md @@ -94,6 +94,17 @@ import { TextArea } from '@nutui/nutui-react' | onFocus | 聚焦時觸發 | `(event) => void` | `-` | | onBlur | 失焦時觸發 | `(event) => void` | `-` | +### Ref + +通過 ref 可以獲取到 Textarea 實例並調用實例方法。 + +| 方法名 | 說明 | 參數 | +| --- | --- | --- | +| clear | 清除容器中的數據 | `-` | +| focus | 使容器獲取焦點 | `-` | +| blur | 使容器失去焦點 | `-` | +| get | 獲取當前容器 | `-` | + ## 主題定制 ### 樣式變量 diff --git a/src/packages/textarea/textarea.taro.tsx b/src/packages/textarea/textarea.taro.tsx index 9ad57c399b..7a89cfeac7 100644 --- a/src/packages/textarea/textarea.taro.tsx +++ b/src/packages/textarea/textarea.taro.tsx @@ -1,4 +1,4 @@ -import React, { FunctionComponent, useRef } from 'react' +import React, { forwardRef, useRef, useImperativeHandle } from 'react' import classNames from 'classnames' import Taro from '@tarojs/taro' import { BaseEventOrig, Textarea, View } from '@tarojs/components' @@ -19,9 +19,7 @@ const defaultProps = { plain: false, status: 'default', } as TaroTextAreaProps -export const TextArea: FunctionComponent> = ( - props -) => { +export const TextArea = forwardRef((props: Partial, ref) => { const { locale } = useConfig() const { className, @@ -54,20 +52,21 @@ export const TextArea: FunctionComponent> = ( return value } - const [inputValue, setInputValue] = usePropsValue({ + const [innerValue, setInnerValue] = usePropsValue({ value, defaultValue, finalValue: format(defaultValue), onChange, }) + const textareaRef = useRef(null) const handleChange = (event: BaseEventOrig) => { const text = event?.detail?.value if (text) { const value = compositionRef.current ? text : format(text) - setInputValue(value) + setInnerValue(value) } else { - setInputValue('') + setInnerValue('') } } @@ -83,6 +82,23 @@ export const TextArea: FunctionComponent> = ( onBlur?.(event) } + useImperativeHandle(ref, () => { + return { + clear: () => { + setInnerValue('') + }, + focus: () => { + textareaRef.current?.focus() + }, + blur: () => { + textareaRef.current?.blur() + }, + get nativeElement() { + return textareaRef.current + }, + } + }) + return ( <> > = ( >