diff --git a/packages/common b/packages/common index 17b62abaed..ff5ce40ca2 160000 --- a/packages/common +++ b/packages/common @@ -1 +1 @@ -Subproject commit 17b62abaedb8f4d2bac8e2aad53bc17bc68d5fd9 +Subproject commit ff5ce40ca26310c598240747ed08f86d49bae2ba diff --git a/packages/components/textarea/Textarea.tsx b/packages/components/textarea/Textarea.tsx index a78dc72db0..ccde847f11 100644 --- a/packages/components/textarea/Textarea.tsx +++ b/packages/components/textarea/Textarea.tsx @@ -1,5 +1,6 @@ import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react'; import classNames from 'classnames'; +import { CloseCircleFilledIcon as TdCloseCircleFilledIcon } from 'tdesign-icons-react'; import calcTextareaHeight from '@tdesign/common-js/utils/calcTextareaHeight'; import { getCharacterLength, getUnicodeLength, limitUnicodeMaxLength } from '@tdesign/common-js/utils/helper'; @@ -9,6 +10,7 @@ import useConfig from '../hooks/useConfig'; import useControlled from '../hooks/useControlled'; import useDefaultProps from '../hooks/useDefaultProps'; import useEventCallback from '../hooks/useEventCallback'; +import useGlobalIcon from '../hooks/useGlobalIcon'; import useIsomorphicLayoutEffect from '../hooks/useLayoutEffect'; import { textareaDefaultProps } from './defaultProps'; @@ -49,6 +51,8 @@ const Textarea = forwardRef((originalProps, tips, allowInputOverMax, rows, + clearable, + onClear, ...otherProps } = props; const hasMaxcharacter = typeof maxcharacter !== 'undefined'; @@ -56,6 +60,7 @@ const Textarea = forwardRef((originalProps, const [value = '', setValue] = useControlled(props, 'value', props.onChange); const [isFocused, setIsFocused] = useState(false); + const [isHover, setIsHover] = useState(false); const [isOvermax, setIsOvermax] = useState(false); const [textareaStyle, setTextareaStyle] = useState>(DEFAULT_TEXTAREA_STYLE); const [composingValue, setComposingValue] = useState(''); @@ -72,6 +77,10 @@ const Textarea = forwardRef((originalProps, }, [value, allowInputOverMax, maxcharacter]); const { classPrefix } = useConfig(); + const { CloseCircleFilledIcon } = useGlobalIcon({ + CloseCircleFilledIcon: TdCloseCircleFilledIcon, + }); + const isReadonly = props.readOnly || props.readonly; const textareaPropsNames = Object.keys(otherProps).filter( (key) => !/^on[A-Z]/.test(key) && !OMIT_PROPS.includes(key), @@ -136,10 +145,32 @@ const Textarea = forwardRef((originalProps, } } setComposingValue(val); - setValue(val, { e }); + setValue(val, { e, trigger: 'input' }); } } + function handleClear(e: React.MouseEvent) { + setValue('', { e, trigger: 'clear' }); + onClear?.({ e }); + textareaRef.current?.focus(); + adjustTextareaHeight(); + } + + function handleClearMouseDown(e: React.MouseEvent) { + e.preventDefault(); + } + + function handleFocus(e: React.FocusEvent) { + if (disabled) return; + setIsFocused(true); + props.onFocus?.(e.currentTarget.value, { e }); + } + + function handleBlur(e: React.FocusEvent) { + setIsFocused(false); + props.onBlur?.(e.currentTarget.value, { e }); + } + function handleCompositionStart() { composingRef.current = true; } @@ -214,9 +245,18 @@ const Textarea = forwardRef((originalProps, hasMaxcharacter ? characterLength : currentLength, hasMaxcharacter ? maxcharacter : maxlength, ); + const showClear = Boolean(clearable && value && !disabled && !isReadonly && isHover); return ( -
+
setIsHover(true)} + onMouseLeave={() => setIsHover(false)} + > + + + +
+
+`; + exports[`csr snapshot test > csr test packages/components/textarea/_example/events.tsx 1`] = `
ssr test packages/components/tag-input/_example/the exports[`ssr snapshot test > ssr test packages/components/textarea/_example/base.tsx 1`] = `"
"`; +exports[`ssr snapshot test > ssr test packages/components/textarea/_example/clearable.tsx 1`] = `"
"`; + exports[`ssr snapshot test > ssr test packages/components/textarea/_example/events.tsx 1`] = `"
"`; exports[`ssr snapshot test > ssr test packages/components/textarea/_example/maxlength.tsx 1`] = `"
这里可以放一些提示文字
0/20
0/20
0/20
0/20
0/20
"`; diff --git a/test/snap/__snapshots__/ssr.test.jsx.snap b/test/snap/__snapshots__/ssr.test.jsx.snap index dd64f818d1..56819c3b10 100644 --- a/test/snap/__snapshots__/ssr.test.jsx.snap +++ b/test/snap/__snapshots__/ssr.test.jsx.snap @@ -1116,6 +1116,8 @@ exports[`ssr snapshot test > ssr test packages/components/tag-input/_example/the exports[`ssr snapshot test > ssr test packages/components/textarea/_example/base.tsx 1`] = `"
"`; +exports[`ssr snapshot test > ssr test packages/components/textarea/_example/clearable.tsx 1`] = `"
"`; + exports[`ssr snapshot test > ssr test packages/components/textarea/_example/events.tsx 1`] = `"
"`; exports[`ssr snapshot test > ssr test packages/components/textarea/_example/maxlength.tsx 1`] = `"
这里可以放一些提示文字
0/20
0/20
0/20
0/20
0/20
"`;