|
1 | 1 | "use client" |
2 | 2 |
|
3 | | -import { useEffect, useState } from "react" |
4 | 3 | import { |
5 | | - TextArea as TextAreaPrimitive, |
6 | | - TextField as TextFieldPrimitive, |
7 | | - type TextFieldProps as TextFieldPrimitiveProps, |
8 | | - type ValidationResult, |
9 | | - composeRenderProps, |
| 4 | + TextArea as TextAreaPrimitive, |
| 5 | + TextField as TextFieldPrimitive, |
| 6 | + type TextFieldProps as TextFieldPrimitiveProps, |
| 7 | + type ValidationResult, |
| 8 | + composeRenderProps, |
10 | 9 | } from "react-aria-components" |
11 | 10 | import { tv } from "tailwind-variants" |
| 11 | +import { useState, useEffect } from "react" |
12 | 12 |
|
13 | 13 | import { Description, FieldError, Label } from "./field" |
14 | 14 | import { composeTailwindRenderProps, focusStyles } from "./primitive" |
15 | 15 |
|
16 | 16 | const textareaStyles = tv({ |
17 | | - extend: focusStyles, |
18 | | - base: "field-sizing-content overflow-wrap-anywhere max-h-96 min-h-16 w-full min-w-0 whitespace-pre-wrap break-words rounded-lg border border-input px-2.5 py-2 pb-6 text-base shadow-xs outline-hidden transition duration-200 disabled:opacity-50 sm:text-sm", |
| 17 | + extend: focusStyles, |
| 18 | + base: "bg-(--color-bg) field-sizing-content max-h-96 min-h-16 w-full min-w-0 rounded-lg border border-input px-2.5 py-2 pb-6 text-base shadow-xs outline-hidden transition duration-200 disabled:opacity-50 sm:text-sm break-words whitespace-pre-wrap overflow-wrap-anywhere", |
19 | 19 | }) |
20 | 20 |
|
21 | 21 | interface TextareaProps extends TextFieldPrimitiveProps { |
22 | | - autoSize?: boolean |
23 | | - label?: string |
24 | | - placeholder?: string |
25 | | - description?: string |
26 | | - errorMessage?: string | ((validation: ValidationResult) => string) |
27 | | - className?: string |
28 | | - ref?: React.Ref<HTMLDivElement> |
29 | | - showCharacterCount?: boolean |
30 | | - maxLength?: number |
31 | | - height?: string | number |
| 22 | + autoSize?: boolean |
| 23 | + label?: string |
| 24 | + placeholder?: string |
| 25 | + description?: string |
| 26 | + errorMessage?: string | ((validation: ValidationResult) => string) |
| 27 | + className?: string |
| 28 | + ref?: React.Ref<HTMLDivElement> |
| 29 | + showCharacterCount?: boolean |
| 30 | + maxLength?: number |
| 31 | + height?: string | number |
32 | 32 | } |
33 | 33 |
|
34 | 34 | const Textarea = ({ |
35 | | - className, |
36 | | - placeholder, |
37 | | - label, |
38 | | - description, |
39 | | - errorMessage, |
40 | | - ref, |
41 | | - showCharacterCount, |
42 | | - maxLength, |
43 | | - height, |
44 | | - ...props |
45 | | -}: TextareaProps) => { |
46 | | - const [currentLength, setCurrentLength] = useState(props.value?.toString().length || 0) |
| 35 | + className, |
| 36 | + placeholder, |
| 37 | + label, |
| 38 | + description, |
| 39 | + errorMessage, |
| 40 | + ref, |
| 41 | + showCharacterCount, |
| 42 | + maxLength, |
| 43 | + height, |
| 44 | + ...props |
| 45 | + }: TextareaProps) => { |
| 46 | + const [currentLength, setCurrentLength] = useState(props.value?.toString().length || 0) |
47 | 47 |
|
48 | | - useEffect(() => { |
49 | | - setCurrentLength(props.value?.toString().length || 0) |
50 | | - }, [props.value]) |
| 48 | + useEffect(() => { |
| 49 | + setCurrentLength(props.value?.toString().length || 0) |
| 50 | + }, [props.value]) |
51 | 51 |
|
52 | | - const getCountText = () => { |
53 | | - if (maxLength) { |
54 | | - return `${currentLength}/${maxLength}` |
| 52 | + const getCountText = () => { |
| 53 | + if (maxLength) { |
| 54 | + return `${currentLength}/${maxLength}` |
| 55 | + } |
| 56 | + return `${currentLength}字` |
55 | 57 | } |
56 | | - return `${currentLength}字` |
57 | | - } |
58 | 58 |
|
59 | | - return ( |
60 | | - <TextFieldPrimitive |
61 | | - ref={ref} |
62 | | - {...props} |
63 | | - className={composeTailwindRenderProps(className, "group flex flex-col gap-y-1.5")} |
64 | | - > |
65 | | - {label && <Label isRequired={props.isRequired}>{label}</Label>} |
66 | | - <div className="relative"> |
67 | | - <TextAreaPrimitive |
68 | | - placeholder={placeholder} |
69 | | - maxLength={maxLength} |
70 | | - className={composeRenderProps(className, (className, renderProps) => |
71 | | - textareaStyles({ |
72 | | - ...renderProps, |
73 | | - className, |
74 | | - }), |
75 | | - )} |
76 | | - style={{ |
77 | | - height: height ?? undefined, |
78 | | - wordWrap: "break-word", |
79 | | - overflowWrap: "break-word", |
80 | | - ...props.style, |
81 | | - }} |
82 | | - /> |
83 | | - {showCharacterCount && currentLength > 0 && ( |
84 | | - <div className="pointer-events-none absolute right-3 bottom-3 select-none text-muted-fg text-xs"> |
85 | | - {getCountText()} |
86 | | - </div> |
87 | | - )} |
88 | | - </div> |
89 | | - {description && <Description>{description}</Description>} |
90 | | - <FieldError>{errorMessage}</FieldError> |
91 | | - </TextFieldPrimitive> |
92 | | - ) |
| 59 | + return ( |
| 60 | + <TextFieldPrimitive |
| 61 | + ref={ref} |
| 62 | + {...props} |
| 63 | + className={composeTailwindRenderProps(className, "group flex flex-col gap-y-1.5")} |
| 64 | + > |
| 65 | + {label && <Label isRequired={props.isRequired}>{label}</Label>} |
| 66 | + <div className="relative"> |
| 67 | + <TextAreaPrimitive |
| 68 | + placeholder={placeholder} |
| 69 | + maxLength={maxLength} |
| 70 | + className={composeRenderProps(className, (className, renderProps) => |
| 71 | + textareaStyles({ |
| 72 | + ...renderProps, |
| 73 | + className, |
| 74 | + }), |
| 75 | + )} |
| 76 | + style={{ |
| 77 | + height: height ?? undefined, |
| 78 | + wordWrap: "break-word", |
| 79 | + overflowWrap: "break-word", |
| 80 | + ...props.style, |
| 81 | + }} |
| 82 | + /> |
| 83 | + {showCharacterCount && currentLength > 0 && ( |
| 84 | + <div className="absolute bottom-3 right-3 text-xs text-muted-fg select-none pointer-events-none"> |
| 85 | + {getCountText()} |
| 86 | + </div> |
| 87 | + )} |
| 88 | + </div> |
| 89 | + {description && <Description>{description}</Description>} |
| 90 | + <FieldError>{errorMessage}</FieldError> |
| 91 | + </TextFieldPrimitive> |
| 92 | + ) |
93 | 93 | } |
94 | 94 |
|
95 | 95 | export type { TextareaProps } |
|
0 commit comments