1
+ "use client" ;
2
+
3
+ import { TextInput } from "@/src/components/Input" ;
4
+ import { Button } from "@/src/components/Button" ;
5
+
6
+
7
+ interface HexInputProps {
8
+ value : string ;
9
+ onChange : ( value : string ) => void ;
10
+ onRemove ?: ( ) => void ;
11
+ label ?: string ;
12
+ placeholder ?: string ;
13
+ }
14
+
15
+ interface HexArrayInputProps {
16
+ value : string [ ] ;
17
+ onChange : ( value : string [ ] ) => void ;
18
+ label ?: string ;
19
+ }
20
+
21
+ export const HexInput : React . FC < HexInputProps > = ( {
22
+ value,
23
+ label = "Hex Value" ,
24
+ placeholder = "Enter hex value (with 0x prefix)" ,
25
+ onChange,
26
+ onRemove,
27
+ } ) => {
28
+ return (
29
+ < div className = "flex w-full flex-col gap-2 rounded border p-2" >
30
+ < TextInput
31
+ label = { label }
32
+ placeholder = { placeholder }
33
+ state = { [
34
+ value ,
35
+ ( newValue ) => {
36
+ // Ensure hex format
37
+ const hexValue = newValue . startsWith ( "0x" )
38
+ ? newValue
39
+ : `0x${ newValue } ` ;
40
+ onChange ( hexValue ) ;
41
+ } ,
42
+ ] }
43
+ className = "w-full"
44
+ />
45
+ { onRemove && (
46
+ < Button
47
+ onClick = { onRemove }
48
+ className = "self-start rounded bg-red-500 px-2 py-1 text-white"
49
+ >
50
+ Remove
51
+ </ Button >
52
+ ) }
53
+ </ div >
54
+ ) ;
55
+ } ;
56
+
57
+ export const HexArrayInput : React . FC < HexArrayInputProps > = ( {
58
+ value = [ ] ,
59
+ onChange,
60
+ label = "Hex Values" ,
61
+ } ) => {
62
+ const addHexValue = ( ) => {
63
+ onChange ( [ ...value , "0x" ] ) ;
64
+ } ;
65
+
66
+ const removeHexValue = ( index : number ) => {
67
+ const newValues = [ ...value ] ;
68
+ newValues . splice ( index , 1 ) ;
69
+ onChange ( newValues ) ;
70
+ } ;
71
+
72
+ const updateHexValue = ( index : number , hexValue : string ) => {
73
+ const newValues = [ ...value ] ;
74
+ newValues [ index ] = hexValue ;
75
+ onChange ( newValues ) ;
76
+ } ;
77
+
78
+ return (
79
+ < div className = "flex flex-col gap-2" >
80
+ < label className = "font-semibold" > { label } </ label >
81
+ { value . map ( ( hexValue , index ) => (
82
+ < HexInput
83
+ key = { index }
84
+ value = { hexValue }
85
+ label = { `Hex Value ${ index + 1 } ` }
86
+ placeholder = { `Enter hex value (with 0x prefix)` }
87
+ onChange = { ( updatedValue ) => updateHexValue ( index , updatedValue ) }
88
+ onRemove = { ( ) => removeHexValue ( index ) }
89
+ />
90
+ ) ) }
91
+ < Button
92
+ onClick = { addHexValue }
93
+ className = "self-start rounded bg-green-500 px-2 py-1 text-white"
94
+ >
95
+ Add Hex Value
96
+ </ Button >
97
+ </ div >
98
+ ) ;
99
+ } ;
0 commit comments