Skip to content

Commit 941df60

Browse files
authored
Update textarea.tsx
1 parent 35e828b commit 941df60

File tree

1 file changed

+72
-72
lines changed

1 file changed

+72
-72
lines changed

apps/ui/components/ui/textarea.tsx

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,95 @@
11
"use client"
22

3-
import { useEffect, useState } from "react"
43
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,
109
} from "react-aria-components"
1110
import { tv } from "tailwind-variants"
11+
import { useState, useEffect } from "react"
1212

1313
import { Description, FieldError, Label } from "./field"
1414
import { composeTailwindRenderProps, focusStyles } from "./primitive"
1515

1616
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",
1919
})
2020

2121
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
3232
}
3333

3434
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)
4747

48-
useEffect(() => {
49-
setCurrentLength(props.value?.toString().length || 0)
50-
}, [props.value])
48+
useEffect(() => {
49+
setCurrentLength(props.value?.toString().length || 0)
50+
}, [props.value])
5151

52-
const getCountText = () => {
53-
if (maxLength) {
54-
return `${currentLength}/${maxLength}`
52+
const getCountText = () => {
53+
if (maxLength) {
54+
return `${currentLength}/${maxLength}`
55+
}
56+
return `${currentLength}字`
5557
}
56-
return `${currentLength}字`
57-
}
5858

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+
)
9393
}
9494

9595
export type { TextareaProps }

0 commit comments

Comments
 (0)