-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathDropdownField.res
More file actions
185 lines (174 loc) · 5.9 KB
/
Copy pathDropdownField.res
File metadata and controls
185 lines (174 loc) · 5.9 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
type optionType = {
value: string,
label?: string,
displayValue?: string,
}
let updateArrayOfStringToOptionsTypeArrayWithUpperCaseLabel = arrayOfString =>
arrayOfString->Array.map(item => {
value: item,
label: item->String.toUpperCase,
})
let updateArrayOfStringToOptionsTypeArray = arrayOfString =>
arrayOfString->Array.map(item => {
value: item,
})
let defaultValue = {
value: "",
}
open RecoilAtoms
@react.component
let make = (
~appearance: CardThemeType.appearance,
~value,
~setValue,
~isDisplayValueVisible=false,
~displayValue=?,
~setDisplayValue=?,
~fieldName,
~options: array<optionType>,
~disabled=false,
~className="",
~width="w-full",
) => {
let {themeObj, localeString, config} = Recoil.useRecoilValueFromAtom(configAtom)
let {readOnly} = Recoil.useRecoilValueFromAtom(optionAtom)
let loggerState = Recoil.useRecoilValueFromAtom(loggerAtom)
let dropdownRef = React.useRef(Nullable.null)
let (inputFocused, setInputFocused) = React.useState(_ => false)
let {parentURL, iframeId} = Recoil.useRecoilValueFromAtom(keys)
let isSpacedInnerLayout = config.appearance.innerLayout === Spaced
let elementType = PaymentTypeContext.usePaymentType()->CardThemeType.getPaymentModeToString
let handleFocus = _ => {
setInputFocused(_ => true)
Utils.handleOnFocusPostMessage(~iframeId, ~elementType, ~targetOrigin=parentURL)
}
let handleChange = ev => {
let target = ev->ReactEvent.Form.target
let value = target["value"]
// Log the dropdown change using fieldName
if fieldName->String.length > 0 {
LoggerUtils.logInputChangeInfo(fieldName, loggerState)
}
setValue(_ => value)
if isDisplayValueVisible {
let findDisplayValue =
options
->Array.find(ele => ele.value === value)
->Option.getOr(defaultValue)
switch setDisplayValue {
| Some(setDisplayValue) =>
setDisplayValue(_ => findDisplayValue.displayValue->Option.getOr(value))
| None => ()
}
}
}
let disbaledBG = React.useMemo(() => {
themeObj.colorBackground
}, [themeObj])
React.useEffect0(() => {
if value === "" || !(options->Array.map(val => val.value)->Array.includes(value)) {
setValue(_ =>
(
options
->Array.get(0)
->Option.getOr(defaultValue)
).value
)
}
None
})
let focusClass = if inputFocused || value->String.length > 0 {
`mb-7 pb-1 pt-2 ${themeObj.fontSizeXs} transition-all ease-in duration-75`
} else {
"transition-all ease-in duration-75"
}
let floatinglabelClass = inputFocused ? "Label--floating" : "Label--resting"
let inputClassStyles = isSpacedInnerLayout ? "Input" : "Input-Compressed"
let cursorClass = !disabled ? "cursor-pointer" : "cursor-not-allowed"
<RenderIf condition={options->Array.length > 0}>
<div className={`flex flex-col ${width}`}>
<RenderIf
condition={fieldName->String.length > 0 &&
appearance.labels == Above &&
isSpacedInnerLayout}>
<div
className={`Label `}
style={
fontWeight: themeObj.fontWeightNormal,
fontSize: themeObj.fontSizeLg,
marginBottom: "5px",
opacity: "0.6",
}
ariaHidden=true>
{React.string(fieldName)}
</div>
</RenderIf>
<div className="relative">
<RenderIf condition={isDisplayValueVisible && displayValue->Option.isSome}>
<div
className="absolute top-[2px] left-[2px] right-0 bottom-[2px] pointer-events-none rounded-sm z-20 whitespace-nowrap"
style={
background: disabled ? disbaledBG : themeObj.colorBackground,
opacity: disabled ? "35%" : "",
padding: themeObj.spacingUnit,
width: "calc(100% - 22px)",
}
ariaHidden=true>
{React.string(displayValue->Option.getOr(""))}
</div>
</RenderIf>
<select
ref={dropdownRef->ReactDOM.Ref.domRef}
style={
background: disabled ? disbaledBG : themeObj.colorBackground,
opacity: disabled ? "35%" : "",
padding: themeObj.spacingUnit,
paddingRight: "22px",
width: "100%",
}
name=""
value
disabled={readOnly || disabled}
onChange=handleChange
onFocus=handleFocus
className={`${inputClassStyles} ${className} w-full appearance-none outline-none overflow-hidden whitespace-nowrap text-ellipsis ${cursorClass}`}
ariaLabel={`${fieldName} option tab`}>
{options
->Array.mapWithIndex((item, index) => {
<option key={Int.toString(index)} value=item.value>
{React.string(item.label->Option.getOr(item.value))}
</option>
})
->React.array}
</select>
<RenderIf condition={config.appearance.labels == Floating}>
<div
className={`Label ${floatinglabelClass} absolute bottom-0 ml-3 ${focusClass} pointer-events-none`}
style={
marginBottom: {
inputFocused || value->String.length > 0 ? "" : themeObj.spacingUnit
},
fontSize: {
inputFocused || value->String.length > 0 ? themeObj.fontSizeXs : ""
},
opacity: "0.6",
}
ariaHidden=true>
{React.string(fieldName)}
</div>
</RenderIf>
<div
className="self-center absolute pointer-events-none"
style={
opacity: disabled ? "35%" : "",
color: themeObj.colorText,
left: localeString.localeDirection == "rtl" ? "1%" : "97%",
top: "42%",
marginLeft: localeString.localeDirection == "rtl" ? "1rem" : "-1rem",
}>
<Icon size=10 name={"arrow-down"} />
</div>
</div>
</div>
</RenderIf>
}