File tree Expand file tree Collapse file tree 1 file changed +38
-1
lines changed Expand file tree Collapse file tree 1 file changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -71,7 +71,44 @@ This approach can become repetitive, so we advise creating a simple higher-order
71
71
``` jsx
72
72
// Higher-Order Component to enhance form inputs
73
73
import React , { useCallback } from ' react' ;
74
- // ... [HOC Code] ...
74
+
75
+ export interface IWithFormInputProps {
76
+ form : UseFormReturn <any >;
77
+ name : string ;
78
+ }
79
+
80
+ export interface IInjectedFormProps {
81
+ value : string ;
82
+ onChange : (e : React .ChangeEvent <HTMLInputElement >) => void ;
83
+ onBlur : () => void ;
84
+ error ?: string ;
85
+ }
86
+
87
+ function withAJVInput<T>(
88
+ WrappedComponent : React .ComponentType < IInjectedFormProps & T > ,
89
+ ) {
90
+ return function HOC ({ form, name, ... rest }: IWithFormInputProps & T ) {
91
+ const handleChange = useCallback (
92
+ (e : React .ChangeEvent < HTMLInputElement > ) => {
93
+ form .set ({ [name]: e .target .value });
94
+ },
95
+ [form, name],
96
+ );
97
+
98
+ const handleBlur = useCallback (() => {
99
+ form .onBlur (name);
100
+ }, [form, name]);
101
+
102
+ const injectedProps: IInjectedFormProps = {
103
+ value: form .state [name]? .value || ' ' ,
104
+ onChange: handleChange,
105
+ onBlur: handleBlur,
106
+ error: form .state [name]? .error ,
107
+ };
108
+
109
+ return < WrappedComponent {... injectedProps} {... (rest as T )} / > ;
110
+ };
111
+ }
75
112
76
113
const Input = withAJVInput (YourOwnInputComponent);
77
114
< Input form= {form} name= " title" / > ;
You can’t perform that action at this time.
0 commit comments