-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseInput.ts
More file actions
96 lines (82 loc) · 2.38 KB
/
Copy pathuseInput.ts
File metadata and controls
96 lines (82 loc) · 2.38 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
import { useState } from 'react';
import { hasWhiteSpace } from 'helpers';
import { whitespaceNotAllowedMessage } from 'const';
/*
Text input logic and state. Returns values and functions to use in component.
Usage:
* Include and re-assign variables according to needed input.
* Set options if needed. Be sure to include type: 'email' option if using email input.
* Add UI component in page.
Sample:
// Name input logic.
const {
// Values.
value: valueName,
error: errorName,
setError: setErrorName,
onChange: onChangeName,
} = useInput({
// Options.
noWhiteSpace: true,
});
// Name input UI.
<FormInput
type='text'
name='name'
label='Name'
value={valueName}
error={errorName}
onChange={e => onChangeName(e)}
/>
// Form submit.
const onSubmit = (e: React.FormEvent) => {
e.preventDefault(); // Prevent form sending.
// Validate name.
if (valueName.length === 0) {
return setErrorName(requiredInputMessage);
};
};
*/
interface IUseInput {
noWhiteSpace?: boolean;
type?: string;
};
const useInput = (props: IUseInput) => {
// Options.
const { noWhiteSpace, type } = props;
// State.
const [value, setValue] = useState(''); // Value.
const [error, setError] = useState(''); // Error.
// Handle change.
const onChange = (e: any) => {
setError(''); // Clear error.
const newValue = e.target.value; // Typed value. Put it into new variable to check first.
// Helper. White space error.
const handleWhiteSpaceError = () => {
setTimeout(() => { setError('') }, 2000); // Clear error after timeout.
setError(whitespaceNotAllowedMessage);
};
// Whitespace check.
if (noWhiteSpace && hasWhiteSpace(newValue)) {
return handleWhiteSpaceError(); // Prevent typing.
};
// Bug Fix. Input email white space react bug:
// https://github.com/facebook/react/issues/6368
if (noWhiteSpace && type === 'email') {
if (e.key === " ") {
handleWhiteSpaceError();
e.preventDefault(); // Prevent typing. e.preventDefault() should be used for input type email.
};
};
// If checks passed - update input value.
setValue(newValue);
};
// Return values and functions to use.
return {
value,
error,
setError,
onChange,
};
};
export default useInput;