@rtdui/dialogs
Version:
React dialogs base on Rtdui components
63 lines (60 loc) • 1.74 kB
JavaScript
'use client';
import { createStore, useStore, randomId } from '@rtdui/hooks';
const createDialogsStore = () => createStore({
dialogs: []
});
const defaultDialogsStore = createDialogsStore();
const useDialogs = (store = defaultDialogsStore) => useStore(store);
function updateDialogsState(store, update) {
const state = store.getState();
const dialogs2 = update(state.dialogs);
store.setState({
dialogs: dialogs2
});
}
function showDialog(dialog, store = defaultDialogsStore) {
const dialogId = dialog.dialogId || randomId();
updateDialogsState(store, (dialogs2) => {
if (dialog.dialogId && dialogs2.some((d) => d.dialogId === dialog.dialogId)) {
return dialogs2;
}
return [...dialogs2, { ...dialog, dialogId }];
});
return dialogId;
}
function hideDialog(dialogId, store = defaultDialogsStore) {
updateDialogsState(
store,
(dialogs2) => dialogs2.filter((d) => {
if (d.dialogId === dialogId) {
return false;
}
return true;
})
);
return dialogId;
}
function updateDialog(dialog, store = defaultDialogsStore) {
updateDialogsState(
store,
(dialogs2) => dialogs2.map((d) => {
if (d.dialogId === dialog.dialogId) {
return { ...d, ...dialog };
}
return d;
})
);
return dialog.dialogId;
}
function cleanDialogs(store = defaultDialogsStore) {
updateDialogsState(store, () => []);
}
const dialogs = {
show: showDialog,
hide: hideDialog,
update: updateDialog,
clean: cleanDialogs,
updateState: updateDialogsState
};
export { cleanDialogs, createDialogsStore, defaultDialogsStore, dialogs, hideDialog, showDialog, updateDialog, updateDialogsState, useDialogs };
//# sourceMappingURL=dialogs.store.mjs.map