@@ -7,14 +7,15 @@ import {
77 Checkbox ,
88 Select ,
99 Radio ,
10- InputNumber
10+ InputNumber ,
11+ Alert
1112} from 'antd' ;
1213import './styles.css' ;
1314
1415const HookForm = ( ) => {
1516 // FetchFormProvider needs to wrap this component
1617 const [ fetchForm , loading , error ] = useFetchForms (
17- 'a6e92e44-fe5e-4018-b555-3ed9bd60fc70 '
18+ 'fcd4ca8b-12d4-4b8c-882d-00144d54d02c '
1819 ) ;
1920
2021 const onFinish = ( values ) => {
@@ -26,8 +27,7 @@ const HookForm = () => {
2627 } ;
2728
2829 // custom form validation
29- const validate = ( fieldType , validations ) => {
30- console . log ( 'validate' ) ;
30+ const createValidationRules = ( fieldType , validations ) => {
3131 const rules = validations . map ( ( validation ) => {
3232 if ( validation . rule === 'required' ) {
3333 return { required : true , message : validation . message } ;
@@ -40,16 +40,15 @@ const HookForm = () => {
4040 return {
4141 [ validation . rule ] : validation . limit ,
4242 message : validation . message ,
43- type : fieldType
43+ type : fieldType === 'number' ? 'number' : 'string'
4444 } ;
4545 }
4646 } ) ;
47-
48- // console.log('rules', rules);
47+ console . log ( 'rules' , rules ) ;
4948 return rules ;
5049 } ;
5150
52- const selectField = ( item ) => {
51+ const findFieldType = ( item ) => {
5352 switch ( item . fieldType ) {
5453 case 'select' :
5554 const { Option } = Select ;
@@ -71,7 +70,7 @@ const HookForm = () => {
7170 case 'number' :
7271 return < InputNumber key = { item . name } /> ;
7372 default :
74- return < Input key = { item . name } /> ;
73+ return < Input { ... item . fieldHtml } key = { item . name } /> ;
7574 }
7675 } ;
7776
@@ -83,19 +82,20 @@ const HookForm = () => {
8382 with custom components and layouts
8483 </ p >
8584 < br />
86- { error && < div > Error: { error . message } </ div > }
85+ { error && < Alert message = { error } type = 'error' showIcon / >}
8786
8887 { loading ? (
8988 < div > Loading...</ div >
9089 ) : (
9190 fetchForm && (
9291 < Form
9392 name = 'HookForm'
94- labelCol = { { span : 6 } }
93+ labelCol = { { span : 4 } }
9594 wrapperCol = { { span : 18 } }
9695 onFinish = { onFinish }
9796 onFinishFailed = { onFinishFailed }
9897 autoComplete = 'off'
98+ noValidate
9999 >
100100 { fetchForm . formItems . map ( ( item , i ) => (
101101 < Form . Item
@@ -105,7 +105,7 @@ const HookForm = () => {
105105 valuePropName = {
106106 item . fieldType === 'checkbox' ? 'checked' : 'value'
107107 }
108- rules = { validate ( item . fieldType , item . validation ) }
108+ rules = { createValidationRules ( item . fieldType , item . validation ) }
109109 validateTrigger = 'onBlur'
110110 >
111111 { item . fieldType === 'radio' ? (
@@ -117,7 +117,7 @@ const HookForm = () => {
117117 ) ) }
118118 </ Radio . Group >
119119 ) : (
120- selectField ( item )
120+ findFieldType ( item )
121121 ) }
122122 </ Form . Item >
123123 ) ) }
@@ -128,7 +128,7 @@ const HookForm = () => {
128128 } }
129129 >
130130 < Button type = 'primary' htmlType = 'submit' >
131- { fetchForm . submitText || 'Submit' }
131+ { fetchForm . submitText }
132132 </ Button >
133133 </ Form . Item >
134134 </ Form >
0 commit comments