-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathPaymentInputField.res
More file actions
175 lines (167 loc) · 5.13 KB
/
Copy pathPaymentInputField.res
File metadata and controls
175 lines (167 loc) · 5.13 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
open RecoilAtoms
open PaymentTypeContext
@react.component
let make = (
~isValid=Some(true),
~setIsValid=?,
~height="",
~fieldWidth="100%",
~inputFieldClassName="",
~value,
~onChange,
~onBlur=?,
~rightIcon=React.null,
~errorString=?,
~onFocus=?,
~fieldName="",
~name="",
~type_="text",
~maxLength=?,
~pattern=?,
~placeholder="",
~className="",
~inputRef,
~paymentType=?,
~isDisabled=false,
~autocomplete="on",
) => {
let {themeObj, config} = Recoil.useRecoilValueFromAtom(configAtom)
let {innerLayout} = config.appearance
let {readOnly} = Recoil.useRecoilValueFromAtom(optionAtom)
let {parentURL, iframeId} = Recoil.useRecoilValueFromAtom(keys)
let contextPaymentType = usePaymentType()
let paymentType = paymentType->Option.getOr(contextPaymentType)
let elementType = contextPaymentType->CardThemeType.getPaymentModeToString
let (inputFocused, setInputFocused) = React.useState(_ => false)
let handleFocus = ev => {
setInputFocused(_ => true)
switch setIsValid {
| Some(fn) => fn(_ => None)
| None => ()
}
switch onFocus {
| Some(fn) => fn(ev)
| None => ()
}
Utils.handleOnFocusPostMessage(~iframeId, ~elementType, ~targetOrigin=parentURL)
}
let handleBlur = ev => {
setInputFocused(_ => false)
switch onBlur {
| Some(fn) => fn(ev)
| None => ()
}
Utils.handleOnBlurPostMessage(~iframeId, ~elementType, ~targetOrigin=parentURL)
}
let backgroundClass = switch paymentType {
| Payment
| PaymentMethodsManagement =>
themeObj.colorBackground
| _ => "transparent"
}
let direction = if type_ == "password" || type_ == "tel" {
"ltr"
} else {
""
}
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 getClassName = initialLabel => {
if value->String.length == 0 {
`${initialLabel}--empty`
} else {
switch isValid {
| Some(valid) => valid ? `${initialLabel}--valid` : `${initialLabel}--invalid`
| None => ""
}
}
}
let labelClass = getClassName("Label")
let inputClass = getClassName("Input")
let inputLogoClass = getClassName("InputLogo")
let inputClassStyles = innerLayout === Spaced ? "Input" : "Input-Compressed"
<div className="flex flex-col w-full" style={color: themeObj.colorText}>
<RenderIf
condition={fieldName->String.length > 0 &&
config.appearance.labels == Above &&
innerLayout === Spaced}>
<div
className={`Label ${labelClass}`}
style={
fontWeight: themeObj.fontWeightNormal,
fontSize: themeObj.fontSizeLg,
marginBottom: "5px",
opacity: "0.6",
}
ariaHidden=true>
{React.string(fieldName)}
</div>
</RenderIf>
<div className="flex flex-row " style={direction: direction}>
<div className={`relative w-full ${inputFieldClassName}`}>
<input
style={
background: isDisabled ? themeObj.disabledFieldColor : backgroundClass,
padding: themeObj.spacingUnit,
width: fieldWidth,
height,
}
dataTestId={name}
disabled={isDisabled || readOnly}
ref={inputRef->ReactDOM.Ref.domRef}
type_
name
?maxLength
?pattern
className={`${inputClassStyles} ${inputClass} ${className} focus:outline-none transition-shadow ease-out duration-200`}
placeholder={config.appearance.labels == Above ? placeholder : ""}
value
autoComplete={autocomplete}
onChange
onBlur=handleBlur
onFocus=handleFocus
ariaLabel={`Type to fill ${fieldName->String.length > 0 ? fieldName : name} input`}
/>
<RenderIf condition={config.appearance.labels == Floating}>
<div
className={`Label ${floatinglabelClass} ${labelClass} 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>
<div className={`InputLogo ${inputLogoClass} relative flex -ml-10 items-center`}>
{rightIcon}
</div>
</div>
<RenderIf condition={innerLayout === Spaced}>
{switch errorString {
| Some(val) =>
<RenderIf condition={val->String.length > 0}>
<div
className="Error pt-1"
style={
color: themeObj.colorDangerText,
fontSize: themeObj.fontSizeSm,
alignSelf: "start",
textAlign: "left",
}>
{React.string(val)}
</div>
</RenderIf>
| None => React.null
}}
</RenderIf>
</div>
}