-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCodeInputRegex.tsx
More file actions
81 lines (74 loc) · 2.14 KB
/
CodeInputRegex.tsx
File metadata and controls
81 lines (74 loc) · 2.14 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
import React, { useMemo, useState } from 'react';
import styled from 'styled-components';
import { theme } from '../../css/theme';
import { Color } from '../../types/styled';
import { IconProps } from '../Icon/Icon';
import { Text } from '../Text/Text';
// import { TextField, TextFieldProps } from '../TextField';
import { TextField } from '../TextField';
import { TextFieldProps } from '../TextField/TextField';
const StyledTextField: React.FC<TextFieldProps> = styled(TextField)`
input {
background: ${theme.colors.code.background};
color: ${theme.colors.code.color};
font-family: ${theme.fontFamily.code};
border-color: ${theme.colors.code.background};
&, &:hover, &:focus { border-color: ${theme.colors.code.background}; }
}
`;
const Status = styled(Text)`
margin-top: ${theme.size('sm')};
font-weight: ${theme.fontWeight.bold};
`;
export interface CodeInputRegexProps extends TextFieldProps {
regex: RegExp;
minText?: number
}
export const CodeInputRegex: React.FC<CodeInputRegexProps> = ({
regex,
minText = 8,
placeholder = 'Write your code here…',
...props
}) => {
const [value, setValue] = useState('');
const [icon, color, status] = useMemo<[
IconProps['icon'] | undefined,
Color | undefined,
string
]>(() => {
if (!value || value.length < minText) {
let s;
switch (value.length) {
case 0:
s = 'You never know until you try…';
break;
case 1:
case 2:
s = 'That\'s the spirit!';
break;
case 3:
case 4:
s = 'You got this!';
break;
case 5:
case 6:
default:
s = 'Almost there…';
break;
}
return [undefined, undefined, s];
}
if (regex.test(value)) return ['check', 'green', 'Well done!'];
return ['x', 'tertiary', 'Keep trying!'];
}, [value]);
return <>
<StyledTextField
onChange={e => setValue((e.target as HTMLInputElement).value)}
iconSecondary={icon}
iconSecondaryColor={color}
placeholder={placeholder}
{...props}
/>
<Status color={color} variant="small">{status}</Status>
</>;
};