Reset textarea height after clear (#10911)

This commit is contained in:
DS Boyce
2026-06-16 09:25:34 -07:00
committed by GitHub
parent 9dc73d2ec7
commit f075187e57
2 changed files with 32 additions and 1 deletions
+22 -1
View File
@@ -1,4 +1,4 @@
import {useMemo, useRef, useState} from 'react'
import {useImperativeHandle, useMemo, useRef, useState} from 'react'
import {
TextInput,
type TextInputContentSizeChangeEvent,
@@ -10,8 +10,18 @@ import {atoms as a, extractPadding, useAlf, web} from '#/alf'
import {normalizeTextStyles} from '#/alf/typography'
import {IS_ANDROID, IS_IOS, IS_WEB} from '#/env'
export type AutosizedTextareaHeightApi = {
/**
* Reset the input back to its minimum height. Needed on Android, where the
* height is driven by state that only updates via `onContentSizeChange` -
* that event doesn't fire when the value is cleared programmatically.
*/
resetHeight: () => void
}
export type AutosizedTextareaProps = Omit<TextInputProps, 'multiline'> & {
ref?: React.Ref<TextInput>
heightApiRef?: React.Ref<AutosizedTextareaHeightApi>
label: string
minRows?: number
maxRows?: number
@@ -20,6 +30,7 @@ export type AutosizedTextareaProps = Omit<TextInputProps, 'multiline'> & {
export function AutosizedTextarea({
ref,
heightApiRef,
label,
minRows = 1,
maxRows,
@@ -107,6 +118,16 @@ export function AutosizedTextarea({
* directly drive the `height`.
*/
const [nativeHeight, setNativeHeight] = useState(minInputHeight)
useImperativeHandle(
heightApiRef,
() => ({
resetHeight: () => {
setNativeHeight(minInputHeight)
onUpdateHeight?.(minInputHeight)
},
}),
[minInputHeight, onUpdateHeight],
)
const onContentSizeChange = (e: TextInputContentSizeChangeEvent) => {
const contentSize = Math.ceil(e.nativeEvent.contentSize.height)
// ios reports the content size without padding