-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFormField.jsx
More file actions
241 lines (210 loc) · 6.03 KB
/
FormField.jsx
File metadata and controls
241 lines (210 loc) · 6.03 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
'use client';
import { AlertFillIcon, EyeClosedIcon, EyeIcon } from '@primer/octicons-react';
import { Button, Checkbox, FormControl, Select, Text, TextInput } from '@primer/react';
import { Tooltip } from '@primer/react/next';
import { forwardRef, useImperativeHandle, useRef, useState } from 'react';
const defaultProps = {
block: true,
contrast: true,
size: 'large',
};
const textInputProps = {
...defaultProps,
autoCorrect: 'off',
autoCapitalize: 'off',
spellCheck: false,
sx: {
px: 2,
'&:focus-within': {
backgroundColor: 'canvas.default',
},
'> input': {
px: 1,
},
},
};
export const FormField = forwardRef(
(
{
caption,
checked,
error,
hidden,
inputMode,
isValid,
label,
name,
options,
required,
suggestion,
sx,
type,
...props
},
externalRef,
) => {
const [isPasswordVisible, setIsPasswordVisible] = useState(false);
const [capsLockWarningMessage, setCapsLockWarningMessage] = useState(null);
const ref = useRef();
useImperativeHandle(externalRef, () => ref.current);
if (hidden) return null;
const inputProps = {
validationStatus: error ? 'error' : isValid ? 'success' : null,
inputMode,
...props,
};
inputProps.ref = ref;
if (type === 'password') {
function focusAfterEnd() {
setTimeout(() => {
const input = ref.current;
const len = input.value.length;
input.focus();
input.setSelectionRange(len, len);
});
}
function detectCapsLock(event) {
if (inputMode !== 'numeric' && event.getModifierState('CapsLock')) {
setCapsLockWarningMessage('Caps Lock está ativado.');
} else {
setCapsLockWarningMessage(null);
}
}
function handlePasswordVisible(event) {
event.preventDefault();
setIsPasswordVisible(!isPasswordVisible);
focusAfterEnd();
detectCapsLock(event);
}
inputProps.type = isPasswordVisible ? 'text' : 'password';
inputProps.trailingVisual = inputProps.trailingVisual || (
<TextInput.Action
aria-label={isPasswordVisible ? `Ocultar ${label}` : `Visualizar ${label}`}
tooltipDirection="nw"
onClick={handlePasswordVisible}
icon={isPasswordVisible ? EyeClosedIcon : EyeIcon}
/>
);
inputProps.onKeyUp = (e) => {
detectCapsLock(e);
if (props.onKeyUp) props.onKeyUp(e);
};
inputProps.onKeyDown = (e) => {
detectCapsLock(e);
if (props.onKeyDown) props.onKeyDown(e);
};
inputProps.onBlur = (e) => {
setCapsLockWarningMessage(null);
if (props.onBlur) props.onBlur(e);
};
inputProps.sx = { ...textInputProps.sx, pr: 0 };
}
const isCheckbox = typeof checked === 'boolean';
return (
<FormControl id={name} required={required} sx={{ minHeight: '86px', ...sx }}>
<FormControl.Label>{label}</FormControl.Label>
{caption && <FormControl.Caption>{caption}</FormControl.Caption>}
{error && !suggestion?.value && !options && !isCheckbox && (
<FormControl.Validation variant="error">{error}</FormControl.Validation>
)}
<Suggestion suggestion={suggestion} />
<WarningMessage message={capsLockWarningMessage} />
{!options && !isCheckbox && <TextInput type={type} {...textInputProps} {...inputProps} />}
{options && (
<Select {...defaultProps} sx={{ py: 0 }} {...inputProps}>
{options.map((option) => (
<Select.Option key={option.value} {...option}>
{option.label}
</Select.Option>
))}
</Select>
)}
{isCheckbox && <Checkbox checked={checked} {...inputProps} />}
<style jsx="true">{`
::-ms-reveal {
display: none;
}
`}</style>
</FormControl>
);
},
);
FormField.displayName = 'FormField';
export function Suggestion({ suggestion }) {
if (!suggestion?.value) return null;
return (
<Text
sx={{
display: 'inline-flex',
flexWrap: 'wrap',
wordBreak: 'break-word',
fontSize: '12px',
lineHeight: '14px',
fontWeight: 'bold',
columnGap: 1,
mt: 0,
alignItems: 'center',
color: 'attention.fg',
}}>
<AlertFillIcon size={12} />
<span>{suggestion.label ?? 'Você quis dizer'}</span>
<TooltippedButton
tooltip={suggestion.tooltip || 'Aceitar sugestão'}
onClick={suggestion.onClick}
color="success.fg">
<span>{suggestion.pre}</span>
<Text sx={{ textDecoration: 'underline' }}>{suggestion.mid}</Text>
<span>{suggestion.post}</span>
</TooltippedButton>
<span>{suggestion.labelEnd ?? '?'}</span>
{suggestion.ignoreClick && (
<TooltippedButton
tooltip={suggestion.ignoreTooltip || 'Ignorar sugestão'}
onClick={suggestion.ignoreClick}
color="accent.fg"
sx={{ flex: 1 }}>
{suggestion.ignoreLabel || 'Ignorar'}
</TooltippedButton>
)}
</Text>
);
}
function TooltippedButton({ children, color, direction = 'nw', sx, tooltip, ...props }) {
return (
<Tooltip text={tooltip} direction={direction}>
<Button
variant="invisible"
size="small"
labelWrap={true}
sx={{
color,
my: '-4px',
px: 0,
textAlign: 'start',
'> *': { justifyContent: 'end' },
':hover': {
bg: 'transparent',
},
...sx,
}}
{...props}>
{children}
</Button>
</Tooltip>
);
}
function WarningMessage({ message }) {
if (!message) return null;
return (
<Text
sx={{
wordBreak: 'break-word',
fontSize: '12px',
lineHeight: '14px',
fontWeight: 'bold',
color: 'attention.fg',
}}>
<AlertFillIcon size={12} /> {message}
</Text>
);
}