-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathFormsyInput.tsx
167 lines (150 loc) · 4.11 KB
/
FormsyInput.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { FormsyInjectedProps, withFormsy } from 'formsy-react';
import React, {
cloneElement,
Component,
createElement,
InputHTMLAttributes,
isValidElement,
} from 'react';
import {
Form,
Input,
InputOnChangeData,
StrictFormFieldProps,
StrictInputProps,
} from 'semantic-ui-react';
import { filterSuirElementProps } from './utils';
type SemanticFormField = Pick<
StrictFormFieldProps,
'as' | 'className' | 'error' | 'width' | 'inline' | 'disabled'
>;
type SemanticInputProps = Omit<StrictInputProps, 'error'>;
export type IFormsyInputProps<
HtmlBaseElement = InputHTMLAttributes<any>,
InputValueType = any
> = FormsyInjectedProps<InputValueType> &
SemanticFormField &
SemanticInputProps &
Omit<
HtmlBaseElement,
| keyof (SemanticFormField &
SemanticInputProps &
FormsyInjectedProps<InputValueType>)
| 'onBlur'
| 'rel'
| 'rev'
| 'content'
> & {
id?: string;
inputClassName?: string;
passRequiredToField?: boolean;
inputAs?: React.ReactNode | React.ReactElement;
errorLabel?: React.ReactElement;
label?: string | React.ReactNode;
instantValidation?: boolean;
defaultValue?: InputValueType;
onBlur?(
event: React.ChangeEvent<HTMLInputElement>,
data: StrictInputProps & { value: InputValueType }
): void;
};
class FormsyInput extends Component<IFormsyInputProps> {
state = { allowError: false };
componentDidMount() {
const { defaultValue, setValue } = this.props;
if (defaultValue) setValue(defaultValue);
}
componentDidUpdate(prevProps: IFormsyInputProps) {
if (
prevProps.isFormSubmitted !== this.props.isFormSubmitted &&
this.props.isFormSubmitted
) {
this.showError();
}
}
handleChange = (
e: React.ChangeEvent<HTMLInputElement>,
data: InputOnChangeData
) => {
const { value } = data;
this.props.setValue(value);
if (this.props.onChange) this.props.onChange(e, data);
if (this.props.instantValidation) this.showError();
};
handleBlur = (
e: React.ChangeEvent<HTMLInputElement>,
data: InputOnChangeData
) => {
this.showError();
if (this.props.onBlur) this.props.onBlur(e, data);
};
showError = () => this.setState({ allowError: true });
render() {
const {
id,
inputAs = Input,
inputClassName,
required,
label,
defaultValue,
value,
isValid,
isPristine,
errorMessage,
errorLabel,
// Form.Field props
as,
width,
className,
disabled,
inline,
passRequiredToField = true,
} = this.props;
const { allowError } = this.state;
const error = !isPristine && !isValid && allowError;
const inputProps: Record<string, any> = {
...filterSuirElementProps(this.props),
value: value || (isPristine && defaultValue) || '',
onChange: this.handleChange,
onBlur: this.handleBlur,
className: inputClassName,
error: !disabled && error,
label,
id,
};
const isFormField = inputAs === Form.Input || inputAs === Form.TextArea;
const inputNode = isFormField
? (createElement(inputAs as any).props as any).control
: inputAs;
if (isFormField) {
inputProps.label = undefined;
if (inputAs === Form.TextArea) {
inputProps.error = undefined;
}
}
const inputElement =
!isFormField && isValidElement(inputAs)
? cloneElement(inputAs, { ...inputProps, ...(inputAs as any).props })
: createElement(inputNode, { ...inputProps });
const shouldShowFormLabel = isFormField || isValidElement(inputAs);
return (
<Form.Field
as={as}
className={className}
required={required && passRequiredToField}
error={!disabled && error}
width={width}
inline={inline}
disabled={disabled}
>
{shouldShowFormLabel && label && <label htmlFor={id}>{label}</label>}
{inputElement}
{!disabled &&
error &&
errorLabel &&
cloneElement(errorLabel, {}, errorMessage)}
</Form.Field>
);
}
}
export default withFormsy(FormsyInput);