-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.js
93 lines (84 loc) · 1.86 KB
/
input.js
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
import React, { memo, useRef, forwardRef } from 'react'
import { func, bool, arrayOf, oneOfType, string } from 'prop-types'
import { useValidation } from './use-validation'
const propTypes = {
onBlur: func,
onChange: func,
onClick: func,
onFocus: func,
validation: oneOfType([arrayOf(func), func]),
other: oneOfType([arrayOf(string), string]),
recheck: bool,
blur: bool,
change: bool,
click: bool,
onError: func,
onValid: func,
onInvalid: func,
onValidated: func,
name: string.isRequired, // form elements must have name!
}
const createInput = (inputType) => {
const Wrapped = forwardRef(
(
{
onBlur,
onClick,
onChange,
onFocus,
validation,
other,
recheck,
blur,
change,
click,
onError,
onValid,
onInvalid,
onValidated,
...rest
},
ref
) => {
const innerRef = useRef(ref)
const {
handleBlur,
handleChange,
handleClick,
handleFocus,
} = useValidation(innerRef, {
onBlur,
onChange,
onClick,
onFocus,
validation,
other,
recheck,
blur,
change,
click,
onError,
onValid,
onInvalid,
onValidated,
})
return React.createElement(inputType, {
ref: innerRef,
onBlur: handleBlur,
onChange: handleChange,
onClick: handleClick,
onFocus: handleFocus,
...rest,
})
}
)
Wrapped.displayName = `Validated(${inputType})`
Wrapped.propTypes = propTypes
const memoized = memo(Wrapped)
memoized.displayName = `Memo(${Wrapped.displayName})`
return Wrapped
}
const Input = createInput('input')
const Select = createInput('select')
const TextArea = createInput('textarea')
export { Input, Select, TextArea }