Skip to content

Commit

Permalink
fix: 🐛 ShadowModal button loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
charlzyx committed Aug 23, 2024
1 parent 2a22135 commit f9354e7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
33 changes: 17 additions & 16 deletions src/pro-array-table/mixin.pro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export const Flex = (
start?: boolean;
end?: boolean;
} & Pick<React.CSSProperties, "marginTop" | "marginBottom">
>,
>
) => {
const justifyContent = Object.keys(props).find((key) =>
justifyContentList.find((prop) => new RegExp(key).test(prop)),
justifyContentList.find((prop) => new RegExp(key).test(prop))
);

return props.hidden ? null : (
Expand Down Expand Up @@ -110,7 +110,7 @@ export const RowSelectionPro = (props: {
m[i] = true;
return m;
},
{},
{}
);
const keys: (string | number)[] = [];
ds.forEach((item) => {
Expand All @@ -135,12 +135,12 @@ export interface CommonShadowPopup extends IShadowFormOptions {
act?: string;
onCancel?: (
ctx: ReturnType<typeof useProArrayTableContext>,
querylistCtx: ReturnType<typeof useQueryListContext>,
querylistCtx: ReturnType<typeof useQueryListContext>
) => void | Promise<void>;
onOk?: (
data: any,
ctx: ReturnType<typeof useProArrayTableContext>,
querylistCtx: ReturnType<typeof useQueryListContext>,
querylistCtx: ReturnType<typeof useQueryListContext>
) => void | Promise<void>;
}

Expand All @@ -149,7 +149,7 @@ export const ArrayTableShowModal: React.FC<
CommonShadowPopup
> = (props) => {
const { SchemaField, form, schema } = useShadowForm(
pick(props, "schema", "schemaFieldOptions", "subFormOptions"),
pick(props, "schema", "schemaFieldOptions", "subFormOptions")
);
const mySchema = useFieldSchema();
const act = props.act ?? mySchema.name;
Expand Down Expand Up @@ -189,7 +189,6 @@ export const ArrayTableShowModal: React.FC<
}, 200);
}).finally(() => {
pending.current = false;
setLoading(false);
});
};

Expand All @@ -201,9 +200,11 @@ export const ArrayTableShowModal: React.FC<
onCancel={() => {
if (pending.current) return;
setLoading(true);
return Promise.resolve(props?.onCancel?.(ctx, queryListCtx)).then(() =>
reset(),
);
return Promise.resolve(props?.onCancel?.(ctx, queryListCtx))
.then(() => reset())
.finally(() => {
setLoading(false);
});
}}
cancelButtonProps={{
loading,
Expand All @@ -218,13 +219,13 @@ export const ArrayTableShowModal: React.FC<
return form
.submit()
.then((data) => {
return Promise.resolve(
props?.onOk?.(data, ctx, queryListCtx),
).finally(() => {
pending.current = false;
});
return Promise.resolve(props?.onOk?.(data, ctx, queryListCtx));
})
.then(() => reset());
.then(() => reset())
.finally(() => {
pending.current = false;
setLoading(false);
});
}}
>
<FormProvider form={form}>
Expand Down
21 changes: 17 additions & 4 deletions src/shadow-form/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const ShadowModal: React.FC<
const [visible, setVisible] = useState(false);
const pending = useRef(false);
const navRef = useRef<HTMLSpanElement>(null);
const [loading, setLoading] = useState(false);

const init = (c = 0) => {
if (!form) {
Expand All @@ -42,7 +43,7 @@ export const ShadowModal: React.FC<
return Promise.resolve(initLoader.current(field.record)).then(
(init) => {
form?.setValues(toJS(init || {}));
},
}
);
} else {
return Promise.resolve(form?.setValues(toJS(field.record)));
Expand Down Expand Up @@ -87,19 +88,31 @@ export const ShadowModal: React.FC<
<Modal
{...props}
open={visible}
okButtonProps={{
loading,
}}
cancelButtonProps={{ loading }}
onCancel={(e: any) => {
if (pending.current) return;
setLoading(true);
return form
?.reset()
.then(() => props?.onCancel?.(e))
.then(() => reset());
.then(() => reset())
.finally(() => {
setLoading(false);
});
}}
onOk={() => {
if (pending.current) return;
setLoading(true);
return form
?.submit()
.then((data: any) => props?.onOk?.(data))
.then(() => reset());
.then(() => reset())
.finally(() => {
setLoading(false);
});
}}
>
<FormProvider form={form}>
Expand All @@ -122,5 +135,5 @@ export const ShadowModal: React.FC<
)}
</React.Fragment>
);
},
}
);

0 comments on commit f9354e7

Please sign in to comment.