Skip to content

Commit ab60cbc

Browse files
committed
Hook form working
1 parent ef080a8 commit ab60cbc

5 files changed

Lines changed: 16 additions & 101 deletions

File tree

src/examples/FullyManaged.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const FullyManaged = () => {
2020
<br />
2121
<div>
2222
<FetchForm
23-
slug='6fa85752-e237-493d-9c63-c441049cb53d'
23+
slug='fcd4ca8b-12d4-4b8c-882d-00144d54d02c'
2424
onSubmit={onSubmit}
2525
/>
2626
</div>

src/examples/hookform/HookForm.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import {
77
Checkbox,
88
Select,
99
Radio,
10-
InputNumber
10+
InputNumber,
11+
Alert
1112
} from 'antd';
1213
import './styles.css';
1314

1415
const 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>

src/examples/hookform/Parent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import HookForm from './HookForm';
44

55
const HookParent = () => {
66
return (
7-
<FetchFormsProvider permission='ad784124b18825b417ab71426f13f050'>
7+
<FetchFormsProvider permission='92a7a5221aef332efa00bd23012ad6b2'>
88
<HookForm />
99
</FetchFormsProvider>
1010
);

src/lib/exports/hooks/useCloudSave.js

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/lib/exports/scripts/Validations.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,3 @@ export const validate = (values, validation) => {
4646
}
4747
return errors;
4848
};
49-
50-
export const validateField = (value, validations) => {
51-
console.log(value, validations);
52-
for (let i = 0; i < validations.length; i++) {
53-
const validation = validations[i];
54-
55-
if (validation.rule === 'regex' && !validRegex(validation.limit, value)) {
56-
return validation.message;
57-
} else if (
58-
validation.rule === 'min' &&
59-
!biggerThan(validation.limit, value)
60-
) {
61-
return validation.message;
62-
} else if (
63-
validation.rule === 'max' &&
64-
!smallerTan(validation.limit, value)
65-
) {
66-
return validation.message;
67-
} else if (validation.rule === 'required' && !hasValue(value)) {
68-
return validation.message;
69-
}
70-
}
71-
return undefined;
72-
};

0 commit comments

Comments
 (0)