Skip to content

Commit 8d9d3b0

Browse files
committed
Refresh modal component using key
1 parent e83f84e commit 8d9d3b0

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

components/modal/createModelData.vue

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,16 @@
1414
type SchemaRow = { name: string, type: string, immutable: boolean };
1515
1616
const model = useModel();
17+
const defaultSchema = model.target?.schema.map((sch: SchemaRow) => sch);
1718
const form = useForm({
1819
initialValues: {
1920
name: '',
20-
schema: [],
21+
schema: defaultSchema,
2122
}
2223
});
2324
2425
const isDisabled = computed(() => form.isSubmitting.value === true);
25-
26-
const { remove, push, fields } = useFieldArray<SchemaRow>('schema');
27-
28-
const resetDefaultSchema = () => {
29-
for (let i = 0; i < fields.value.length; i++) {
30-
remove(i);
31-
}
32-
33-
model.target?.schema.forEach((sch: SchemaRow) => {
34-
push(sch);
35-
});
36-
};
37-
38-
watch(() => model.target, () => {
39-
// Temporary solution to reactively assign the
40-
// default schema to the form
41-
resetDefaultSchema();
42-
});
26+
const { fields } = useFieldArray<SchemaRow>('schema');
4327
4428
const handleClose = () => {
4529
form.handleReset();

composables/useModal.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type UseModal = {
1616
open: () => void;
1717
};
1818

19+
const sleep = (ms: number) => new Promise((res) => setTimeout(() => res(ms), ms));
20+
1921
/**
2022
* A composable for managing modals.
2123
*
@@ -42,6 +44,7 @@ export default function (
4244
options: ModalProps = { id: 'base' }
4345
): UseModal {
4446
const display = ref(false);
47+
const refreshKey = ref(new Date());
4548

4649
const close = () => {
4750
display.value = false;
@@ -51,7 +54,12 @@ export default function (
5154
}
5255
};
5356

54-
const open = () => {
57+
const open = async () => {
58+
refreshKey.value = new Date();
59+
60+
await nextTick();
61+
await sleep(50);
62+
5563
display.value = true;
5664
};
5765

@@ -68,6 +76,7 @@ export default function (
6876
render() {
6977
return h(component, {
7078
...options,
79+
key: refreshKey.value,
7180
onClose: close,
7281
});
7382
},

0 commit comments

Comments
 (0)