-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
InputWrapper.js
34 lines (29 loc) · 1.11 KB
/
InputWrapper.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
import React, {forwardRef, useContext} from 'react';
import PropTypes from 'prop-types';
import FormContext from './FormContext';
const propTypes = {
InputComponent: PropTypes.oneOfType([PropTypes.func, PropTypes.elementType]).isRequired,
inputID: PropTypes.string.isRequired,
valueType: PropTypes.string,
forwardedRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({current: PropTypes.instanceOf(React.Component)})]),
};
const defaultProps = {
forwardedRef: undefined,
valueType: 'string',
};
function InputWrapper(props) {
const {InputComponent, inputID, forwardedRef, ...rest} = props;
const {registerInput} = useContext(FormContext);
// eslint-disable-next-line react/jsx-props-no-spreading
return <InputComponent {...registerInput(inputID, {ref: forwardedRef, ...rest})} />;
}
InputWrapper.propTypes = propTypes;
InputWrapper.defaultProps = defaultProps;
InputWrapper.displayName = 'InputWrapper';
export default forwardRef((props, ref) => (
<InputWrapper
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
forwardedRef={ref}
/>
));