forked from ckb-devrel/ccc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHexArrayInput.tsx
99 lines (90 loc) · 2.29 KB
/
HexArrayInput.tsx
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"use client";
import { TextInput } from "@/src/components/Input";
import { Button } from "@/src/components/Button";
interface HexInputProps {
value: string;
onChange: (value: string) => void;
onRemove?: () => void;
label?: string;
placeholder?: string;
}
interface HexArrayInputProps {
value: string[];
onChange: (value: string[]) => void;
label?: string;
}
export const HexInput: React.FC<HexInputProps> = ({
value,
label = "Hex Value",
placeholder = "Enter hex value (with 0x prefix)",
onChange,
onRemove,
}) => {
return (
<div className="flex w-full flex-col gap-2 rounded border p-2">
<TextInput
label={label}
placeholder={placeholder}
state={[
value,
(newValue) => {
// Ensure hex format
const hexValue = newValue.startsWith("0x")
? newValue
: `0x${newValue}`;
onChange(hexValue);
},
]}
className="w-full"
/>
{onRemove && (
<Button
onClick={onRemove}
className="self-start rounded bg-red-500 px-2 py-1 text-white"
>
Remove
</Button>
)}
</div>
);
};
export const HexArrayInput: React.FC<HexArrayInputProps> = ({
value = [],
onChange,
label = "Hex Values",
}) => {
const addHexValue = () => {
onChange([...value, "0x"]);
};
const removeHexValue = (index: number) => {
const newValues = [...value];
newValues.splice(index, 1);
onChange(newValues);
};
const updateHexValue = (index: number, hexValue: string) => {
const newValues = [...value];
newValues[index] = hexValue;
onChange(newValues);
};
return (
<div className="flex flex-col gap-2">
<label className="font-semibold">{label}</label>
{value.map((hexValue, index) => (
<HexInput
key={index}
value={hexValue}
label={`Hex Value ${index + 1}`}
placeholder={`Enter hex value (with 0x prefix)`}
onChange={(updatedValue) => updateHexValue(index, updatedValue)}
onRemove={() => removeHexValue(index)}
/>
))}
<Button
onClick={addHexValue}
className="self-start rounded bg-green-500 px-2 py-1 text-white"
>
Add Hex Value
</Button>
</div>
);
};