forked from flexpool/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuideInput.tsx
79 lines (69 loc) · 1.99 KB
/
GuideInput.tsx
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
import React, { useMemo, useState, useCallback } from 'react';
import { useRouter } from 'next/router';
import { TextInput } from 'src/components/Form/TextInput';
import { Trans, useTranslation } from 'next-i18next';
import { getLocationSearch } from 'utils/url';
import qs from 'query-string';
const GuideInput: React.FC<{
label: string;
placeholderText: string;
param?: string;
setExternalValue?: (value: string | null) => void;
regexp?: RegExp;
verifyFunc?: (s: string) => boolean;
}> = ({ label, placeholderText, param, setExternalValue, regexp, verifyFunc }) => {
const initValue = useMemo(() => {
const parsedSearch = qs.parse(getLocationSearch());
return parsedSearch.walletAddress || '';
// eslint-disable-next-line
}, []);
if (regexp === undefined) {
regexp = /.*/;
}
const [value, setValue] = useState(initValue || '');
const [valueValid, setValueValid] = useState(true);
const handleInputChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
setValue(value);
var valid = false;
if (value === '') {
valid = true;
} else {
if (verifyFunc !== undefined) {
valid = verifyFunc(value);
} else {
valid = regexp?.test(value) as boolean;
}
}
if (setExternalValue !== undefined) {
if (valid) {
setExternalValue(value);
} else {
setExternalValue(null);
}
}
setValueValid(valid);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
);
return (
<div>
<TextInput
autoComplete="off"
spellCheck="false"
label={label}
placeholder={placeholderText}
value={value}
onChange={handleInputChange}
errorMessage={
!valueValid ? (
<Trans ns="get-started" i18nKey="detail.invalid" values={{ value: label }} />
) : null
}
/>
</div>
);
};
export default GuideInput;