Skip to content

Commit 71490e4

Browse files
committed
refactor: 优化textarea.tsx和typography.tsx中的代码格式和结构
1 parent 1812eeb commit 71490e4

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

apps/ui/components/ui/textarea.tsx

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
"use client";
1+
"use client"
22

3-
import { useEffect, useState } from "react";
3+
import { useEffect, useState } from "react"
44
import {
5-
composeRenderProps,
65
TextArea as TextAreaPrimitive,
76
TextField as TextFieldPrimitive,
87
type TextFieldProps as TextFieldPrimitiveProps,
98
type ValidationResult,
10-
} from "react-aria-components";
11-
import { tv } from "tailwind-variants";
9+
composeRenderProps,
10+
} from "react-aria-components"
11+
import { tv } from "tailwind-variants"
1212

13-
import { Description, FieldError, Label } from "./field";
14-
import { composeTailwindRenderProps, focusStyles } from "./primitive";
13+
import { Description, FieldError, Label } from "./field"
14+
import { composeTailwindRenderProps, focusStyles } from "./primitive"
1515

1616
const textareaStyles = tv({
1717
extend: focusStyles,
18-
base:
19-
"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",
20-
});
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",
19+
})
2120

2221
interface TextareaProps extends TextFieldPrimitiveProps {
23-
autoSize?: boolean;
24-
label?: string;
25-
placeholder?: string;
26-
description?: string;
27-
errorMessage?: string | ((validation: ValidationResult) => string);
28-
className?: string;
29-
ref?: React.Ref<HTMLDivElement>;
30-
showCharacterCount?: boolean;
31-
maxLength?: number;
32-
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
3332
}
3433

3534
const Textarea = ({
@@ -44,29 +43,24 @@ const Textarea = ({
4443
height,
4544
...props
4645
}: TextareaProps) => {
47-
const [currentLength, setCurrentLength] = useState(
48-
props.value?.toString().length || 0,
49-
);
46+
const [currentLength, setCurrentLength] = useState(props.value?.toString().length || 0)
5047

5148
useEffect(() => {
52-
setCurrentLength(props.value?.toString().length || 0);
53-
}, [props.value]);
49+
setCurrentLength(props.value?.toString().length || 0)
50+
}, [props.value])
5451

5552
const getCountText = () => {
5653
if (maxLength) {
57-
return `${currentLength}/${maxLength}`;
54+
return `${currentLength}/${maxLength}`
5855
}
59-
return `${currentLength}字`;
60-
};
56+
return `${currentLength}字`
57+
}
6158

6259
return (
6360
<TextFieldPrimitive
6461
ref={ref}
6562
{...props}
66-
className={composeTailwindRenderProps(
67-
className,
68-
"group flex flex-col gap-y-1.5",
69-
)}
63+
className={composeTailwindRenderProps(className, "group flex flex-col gap-y-1.5")}
7064
>
7165
{label && <Label isRequired={props.isRequired}>{label}</Label>}
7266
<div className="relative">
@@ -77,7 +71,8 @@ const Textarea = ({
7771
textareaStyles({
7872
...renderProps,
7973
className,
80-
}))}
74+
}),
75+
)}
8176
style={{
8277
height: height ?? undefined,
8378
wordWrap: "break-word",
@@ -86,16 +81,16 @@ const Textarea = ({
8681
}}
8782
/>
8883
{showCharacterCount && currentLength > 0 && (
89-
<div className="absolute bottom-3 right-3 text-xs text-muted-fg select-none pointer-events-none">
84+
<div className="pointer-events-none absolute right-3 bottom-3 select-none text-muted-fg text-xs">
9085
{getCountText()}
9186
</div>
9287
)}
9388
</div>
9489
{description && <Description>{description}</Description>}
9590
<FieldError>{errorMessage}</FieldError>
9691
</TextFieldPrimitive>
97-
);
98-
};
92+
)
93+
}
9994

100-
export type { TextareaProps };
101-
export { Textarea };
95+
export type { TextareaProps }
96+
export { Textarea }

apps/ui/components/ui/typography.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ type TypographyProps = {
2727
as?: "p" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "span" | "div"
2828
}
2929

30-
const Typography = ({ className, variant, as: Tag = "div", children, ...props }: TypographyProps) => {
30+
const Typography = ({
31+
className,
32+
variant,
33+
as: Tag = "div",
34+
children,
35+
...props
36+
}: TypographyProps) => {
3137
return (
3238
<Tag className={typographyStyles({ variant, className })} {...props}>
3339
{children}

0 commit comments

Comments
 (0)