@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
1 lines • 5.35 kB
Source Map (JSON)
{"version":3,"file":"TextArea.mjs","names":[],"sources":["../../../src/base-ui/Input/TextArea.tsx"],"sourcesContent":["'use client';\n\nimport { Field } from '@base-ui/react/field';\nimport { cx, useThemeMode } from 'antd-style';\nimport { type CSSProperties, memo, useMemo, useRef, useState } from 'react';\nimport { useMergeRefs } from 'react-merge-refs';\n\nimport ClearButton from './ClearButton';\nimport { clearNativeValue } from './clearNativeValue';\nimport { isPressEnter } from './pressEnter';\nimport { rootVariants, styles } from './style';\nimport type { TextAreaProps } from './type';\n\nconst textLength = (value: unknown) =>\n value === undefined || value === null ? 0 : Array.from(String(value)).length;\n\nconst TextArea = memo<TextAreaProps>(\n ({\n ref,\n className,\n classNames,\n styles: customStyles,\n style,\n variant,\n shadow,\n autoSize,\n resize = false,\n disabled,\n readOnly,\n allowClear,\n onClear,\n onPressEnter,\n onKeyDown,\n onChange,\n showCount,\n maxLength,\n value,\n defaultValue,\n ...rest\n }) => {\n const { isDarkMode } = useThemeMode();\n const mergedVariant = variant || (isDarkMode ? 'filled' : 'outlined');\n const textareaRef = useRef<HTMLTextAreaElement>(null);\n const mergedRef = useMergeRefs([ref, textareaRef]);\n const [innerLength, setInnerLength] = useState(() => textLength(value ?? defaultValue));\n const length = value === undefined ? innerLength : textLength(value);\n const showClear = allowClear && length > 0 && !disabled && !readOnly;\n const over = maxLength !== undefined && length > maxLength;\n\n const cssVariables = useMemo<CSSProperties>(() => {\n if (typeof autoSize !== 'object') return {};\n return {\n '--textarea-max-height': autoSize.maxRows ? `calc(1.5em * ${autoSize.maxRows})` : undefined,\n '--textarea-min-rows': autoSize.minRows,\n } as CSSProperties;\n }, [autoSize]);\n\n return (\n <div\n data-disabled={disabled ? '' : undefined}\n style={{ ...cssVariables, ...style }}\n className={cx(\n rootVariants({ shadow, variant: mergedVariant }),\n styles.textarea,\n autoSize && styles.textareaAutoSize,\n resize && styles.textareaResize,\n showCount && styles.textareaWithCount,\n className,\n )}\n >\n <Field.Control\n className={cx(styles.input, classNames?.input)}\n disabled={disabled}\n style={customStyles?.input}\n render={\n <textarea\n defaultValue={defaultValue}\n maxLength={maxLength}\n readOnly={readOnly}\n ref={mergedRef}\n value={value}\n onChange={(event) => {\n setInnerLength(textLength(event.currentTarget.value));\n onChange?.(event);\n }}\n onKeyDown={(event) => {\n if (isPressEnter(event)) onPressEnter?.(event);\n onKeyDown?.(event);\n }}\n {...rest}\n />\n }\n />\n {showClear && (\n <ClearButton\n className={styles.textareaClear}\n onClear={() => {\n clearNativeValue(textareaRef.current);\n onClear?.();\n }}\n />\n )}\n {showCount && (\n <span className={styles.count} data-over={over ? '' : undefined}>\n {maxLength === undefined ? length : `${length} / ${maxLength}`}\n </span>\n )}\n </div>\n );\n },\n);\n\nTextArea.displayName = 'TextArea';\n\nexport default Object.assign(TextArea, { formBinding: { emptyValue: '' } as const });\n"],"mappings":";;;;;;;;;;;AAaA,MAAM,cAAc,UAClB,UAAU,KAAA,KAAa,UAAU,OAAO,IAAI,MAAM,KAAK,OAAO,KAAK,CAAC,CAAC,CAAC;AAExE,MAAM,WAAW,MACd,EACC,KACA,WACA,YACA,QAAQ,cACR,OACA,SACA,QACA,UACA,SAAS,OACT,UACA,UACA,YACA,SACA,cACA,WACA,UACA,WACA,WACA,OACA,cACA,GAAG,WACC;CACJ,MAAM,EAAE,eAAe,aAAa;CACpC,MAAM,gBAAgB,YAAY,aAAa,WAAW;CAC1D,MAAM,cAAc,OAA4B,IAAI;CACpD,MAAM,YAAY,aAAa,CAAC,KAAK,WAAW,CAAC;CACjD,MAAM,CAAC,aAAa,kBAAkB,eAAe,WAAW,SAAS,YAAY,CAAC;CACtF,MAAM,SAAS,UAAU,KAAA,IAAY,cAAc,WAAW,KAAK;CACnE,MAAM,YAAY,cAAc,SAAS,KAAK,CAAC,YAAY,CAAC;CAC5D,MAAM,OAAO,cAAc,KAAA,KAAa,SAAS;CAEjD,MAAM,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