add resetForm method

This commit is contained in:
Bowen Tan 2021-09-16 16:02:41 +08:00
parent 8ec7837d3d
commit b677bf1ecc
3 changed files with 39 additions and 1 deletions

View File

@ -53,6 +53,18 @@
wait: {},
disabled: false,
},
{
event: 'onSubmit',
componentId: 'form',
method: {
name: 'resetForm',
},
wait: {
type: 'delay',
time: 1000,
},
disabled: false,
},
],
},
},

View File

@ -6,9 +6,11 @@ import Slot from '../_internal/Slot';
import { Button } from '@chakra-ui/react';
import { stateStore } from '../../store';
import { watch } from '@vue-reactivity/watch';
import { apiService } from '../../api-service';
const FormImpl: ComponentImplementation<Record<string, string>> = ({
mergeState,
subscribeMethods,
slotsMap,
callbackMap,
}) => {
@ -26,6 +28,20 @@ const FormImpl: ComponentImplementation<Record<string, string>> = ({
);
}, [slotsMap]);
useEffect(() => {
subscribeMethods({
resetForm() {
formControlIds.forEach(fcId => {
const inputId = stateStore[fcId].inputId;
apiService.send('uiMethod', {
componentId: inputId,
name: 'resetInputValue',
});
});
},
});
}, [formControlIds]);
useEffect(() => {
const stops: ReturnType<typeof watch>[] = [];
formControlIds.forEach((fcId, i) => {
@ -89,7 +105,11 @@ export default {
state: Type.Object({
data: Type.Any(),
}),
methods: [],
methods: [
{
name: 'resetForm',
},
],
},
}),
impl: FormImpl,

View File

@ -85,6 +85,9 @@ const Input: ComponentImplementation<{
setInputValue({ value }) {
setValue(value);
},
resetInputValue() {
setValue(initialValue || '');
},
});
}, []);
@ -184,6 +187,9 @@ export default {
value: Type.String(),
}),
},
{
name: 'resetInputValue',
},
],
},
}),