UNPKG

@trail-ui/react

Version:
83 lines (80 loc) 2.53 kB
import { useInput } from "./chunk-LSMK7S5U.mjs"; // src/input/textarea.tsx import { XCircleFilledIcon } from "@trail-ui/icons"; import { clsx } from "@trail-ui/shared-utils"; import { input } from "@trail-ui/theme"; import { forwardRef, useMemo } from "react"; import { mergeProps } from "react-aria"; import { TextAreaContext, useContextProps } from "react-aria-components"; import TextareaAutosize from "react-textarea-autosize"; import { jsx, jsxs } from "react/jsx-runtime"; function TextArea(props, ref) { [props, ref] = useContextProps(props, ref, TextAreaContext); const { classNames, className, variant, fullWidth, isMultiline = true, style, minRows = 3, maxRows = 8, cacheMeasurements = false, disableAutosize = false, onHeightChange, ...otherProps } = props; const { getInputWrapperProps, getInputProps, getClearButtonProps, domRef } = useInput({ ...otherProps, ref }); const isClearable = !!props.onClear; const slots = useMemo( () => input({ variant, fullWidth, isClearable, isMultiline }), [variant, fullWidth, isClearable, isMultiline] ); const baseStyles = clsx(classNames == null ? void 0 : classNames.base, className); const clearButton = useMemo( () => isClearable ? /* @__PURE__ */ jsx( "span", { ...getClearButtonProps(), className: slots.clearButton({ class: classNames == null ? void 0 : classNames.clearButton }), children: /* @__PURE__ */ jsx(XCircleFilledIcon, {}) } ) : null, [classNames == null ? void 0 : classNames.clearButton, getClearButtonProps, isClearable, slots] ); const inputProps = getInputProps(); const content = disableAutosize ? /* @__PURE__ */ jsx( "textarea", { ...inputProps, className: slots.input({ class: classNames == null ? void 0 : classNames.input }), style: mergeProps(inputProps.style, style != null ? style : {}), ref: domRef } ) : /* @__PURE__ */ jsx( TextareaAutosize, { ...inputProps, className: slots.input({ class: classNames == null ? void 0 : classNames.input }), cacheMeasurements, maxRows, minRows, style: mergeProps(inputProps.style, style != null ? style : {}), onHeightChange, ref: domRef } ); return /* @__PURE__ */ jsxs("div", { className: slots.base({ class: baseStyles }), ...getInputWrapperProps(), children: [ content, clearButton ] }); } var _TextArea = forwardRef(TextArea); export { _TextArea };