-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSelect.ts
More file actions
60 lines (52 loc) · 1.26 KB
/
Copy pathuseSelect.ts
File metadata and controls
60 lines (52 loc) · 1.26 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
import { useState } from 'react';
/*
Select logic and state. Returns values and functions to use in component.
Usage:
* Include and re-assign variables according to needed select.
* Add UI component in page.
Sample:
// Account select logic.
const {
// Values.
value: valueAccount,
error: errorAccount,
setError: setErrorAccount,
onChange: onChangeAccount,
} = useSelect();
// Account select UI.
<FormSelect
name='account'
label='Account'
value={valueAccount}
error={errorAccount}
onChange={e => onChangeAccount(e)}
options={[
{
value: 'account-1',
text: 'Account 1'
},
{
value: 'account-2',
text: 'Account 2'
},
]}
/>
*/
const useSelect = () => {
// State.
const [value, setValue] = useState(''); // Value.
const [error, setError] = useState(''); // Error.
// Handle change.
const onChange = (e: any) => {
setError(''); // Clear error. We don't set anny errors in select here. But we leave error cleaning because error can be setted on form submit in parent component.
setValue(e.target.value);
};
// Return values and functions to use.
return {
value,
error,
setError,
onChange,
};
};
export default useSelect;