-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathText.component.tsx
More file actions
77 lines (72 loc) · 2.31 KB
/
Text.component.tsx
File metadata and controls
77 lines (72 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"use client";
import Txt from "@/components/common/Txt.component";
import {
ApplicationNode,
type ApplicationText,
} from "@/src/constants/application/type";
import { replacer } from "@/src/functions/replacer";
import { validator } from "@/src/functions/validator";
import { useLocalStorage } from "@/src/hooks/useLocalstorage.hook";
import { cn } from "@/src/utils/cn";
import { useId, useState } from "react";
interface ApplicationTextProps {
data: ApplicationNode;
}
const ApplicationText = ({ data }: ApplicationTextProps) => {
const textData = data as ApplicationText;
const id = useId();
const [value, setValue] = useLocalStorage(textData.name, "");
const [isError, setIsError] = useState(false);
return (
<div className="relative">
{textData.title && (
<label>
<Txt typography="h6">{`${textData.title}${
textData.require ? "*" : ""
}`}</Txt>
{textData.subtitle && <Txt className="whitespace-pre-line">{` ${textData.subtitle}`}</Txt>}
</label>
)}
<input
className={cn("my-2 border rounded-lg p-4 w-full", {
"border-error": isError,
})}
type="text"
id={id}
value={value}
onInput={(e) => {
const value = textData.replace
? replacer(e.currentTarget.value, textData.replace)
: e.currentTarget.value;
const isError = textData.validate
? !validator(value, textData.validate)
: false;
setIsError(isError);
setValue(value);
}}
maxLength={textData.maxLength ?? 1000}
minLength={textData.minLength ?? 0}
/>
{isError && textData.errorMessages ? (
<div className="absolute w-full translate-x-[100%]">
<div className="w-fit text-error -translate-x-[calc(100%+1rem)] -translate-y-12">
{textData.errorMessages}
</div>
</div>
) : (
""
)}
{textData.example?.map((example, index) => {
return (
<div
key={index}
className="flex items-center justify-center rounded-full border border-gray-300 bg-gray-200 text-sm p-2 pl-4 pr-4 my-2 text-gray-400 w-fit"
>
{example}
</div>
);
})}
</div>
);
};
export default ApplicationText;