mtxuilib 0.1.277 → 0.1.279

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ import { type StateCreator } from "zustand";
2
+ export type ConfirmModalProps = {};
3
+ export interface ConfirmModalState extends ConfirmModalProps {
4
+ open?: boolean;
5
+ setOpen: (_open: boolean) => void;
6
+ onConfirm?: (values: any) => void;
7
+ onCancel?: () => void;
8
+ setOnConfirm: (_onConfirm: (values: any) => void) => void;
9
+ setOnCancel: (_onCancel: () => void) => void;
10
+ options?: ConfirmModalOptions;
11
+ setOptions: (_options: ConfirmModalOptions) => void;
12
+ values?: any;
13
+ setValues: (_values: any) => void;
14
+ }
15
+ export declare const createAppSlice: StateCreator<ConfirmModalState, [
16
+ ], [
17
+ ], ConfirmModalState>;
18
+ export declare const gomtmContext: import("react").Context<import("zustand").StoreApi<ConfirmModalState> | null>;
19
+ type AppProviderProps = React.PropsWithChildren<ConfirmModalProps>;
20
+ export declare const ConfirmModalProvider: (props: AppProviderProps) => import("react/jsx-runtime").JSX.Element;
21
+ export declare function useConfirmStore(): ConfirmModalState;
22
+ export declare function useConfirmStore<T>(selector: (state: ConfirmModalState) => T): T;
23
+ interface ConfirmModalOptions {
24
+ title?: string;
25
+ message: string;
26
+ description?: string;
27
+ confirmText?: string;
28
+ cancelText?: string;
29
+ onConfirm: (values: any) => void;
30
+ onCancel?: () => void;
31
+ }
32
+ interface UseConfirmProps extends ConfirmModalProps {
33
+ options: ConfirmModalOptions;
34
+ }
35
+ export declare const useConfirm: (props: UseConfirmProps) => {
36
+ showConfirm: (values: any) => void;
37
+ };
38
+ export declare function useConfirmV2(message: any, onConfirm: any, onAbort: any): (values: any) => void;
39
+ export {};
@@ -0,0 +1,68 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { createContext, useCallback, useContext, useEffect, useMemo, } from "react";
4
+ import { createStore, useStore } from "zustand";
5
+ import { useShallow } from "zustand/react/shallow";
6
+ import { ConfirmModal } from "./Confrom";
7
+ export const createAppSlice = (set, get, init) => {
8
+ return {
9
+ open: false,
10
+ ...init,
11
+ setOpen(_open) {
12
+ set({ open: _open });
13
+ },
14
+ setOnConfirm: (onConfirm) => set({ onConfirm }),
15
+ setOnCancel: (onCancel) => set({ onCancel }),
16
+ setOptions: (options) => set({ options }),
17
+ setValues: (values) => set({ values }),
18
+ };
19
+ };
20
+ const createMtAppStore = (initProps) => {
21
+ const initialState = { ...initProps };
22
+ return createStore()((...a) => ({
23
+ ...createAppSlice(...a),
24
+ ...initialState,
25
+ }));
26
+ };
27
+ export const gomtmContext = createContext(null);
28
+ export const ConfirmModalProvider = (props) => {
29
+ const { children, ...etc } = props;
30
+ const mystore = useMemo(() => createMtAppStore(etc), [etc]);
31
+ return (_jsxs(gomtmContext.Provider, { value: mystore, children: [children, _jsx(ConfirmModal, {})] }));
32
+ };
33
+ const DEFAULT_USE_SHALLOW = true;
34
+ export function useConfirmStore(selector) {
35
+ const store = useContext(gomtmContext);
36
+ if (!store)
37
+ throw new Error("useConfirmStore must in ConfirmModalProvider");
38
+ if (selector) {
39
+ return useStore(store, DEFAULT_USE_SHALLOW ? useShallow(selector) : selector);
40
+ }
41
+ return useStore(store);
42
+ }
43
+ export const useConfirm = (props) => {
44
+ const setOpen = useConfirmStore((x) => x.setOpen);
45
+ const setOnConfirm = useConfirmStore((x) => x.setOnConfirm);
46
+ const setOptions = useConfirmStore((x) => x.setOptions);
47
+ const setValues = useConfirmStore((x) => x.setValues);
48
+ useEffect(() => {
49
+ setOptions(props.options);
50
+ setOnConfirm(props.options.onConfirm);
51
+ }, [props.options, setOptions, setOnConfirm]);
52
+ const showConfirm = useCallback((values) => {
53
+ setOpen(true);
54
+ setValues(values);
55
+ }, [setOpen, setValues]);
56
+ return {
57
+ showConfirm: showConfirm,
58
+ };
59
+ };
60
+ export function useConfirmV2(message, onConfirm, onAbort) {
61
+ const confirm = useCallback((values) => {
62
+ if (window.confirm(message))
63
+ onConfirm(values);
64
+ else
65
+ onAbort();
66
+ }, [message, onConfirm, onAbort]);
67
+ return confirm;
68
+ }
@@ -0,0 +1 @@
1
+ export declare function ConfirmModal(): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,21 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { Button } from "../../ui/button";
4
+ import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogTitle, } from "../../ui/dialog";
5
+ import { useConfirmStore } from "./ConfirmProvider";
6
+ export function ConfirmModal() {
7
+ const open = useConfirmStore((x) => x.open);
8
+ const setOpen = useConfirmStore((x) => x.setOpen);
9
+ const onConfirm = useConfirmStore((x) => x.onConfirm);
10
+ const options = useConfirmStore((x) => x.options);
11
+ const values = useConfirmStore((x) => x.values);
12
+ const title = options?.title || "Confirm";
13
+ const message = options?.message || "Are you sure?";
14
+ const confirmText = options?.confirmText || "Confirm";
15
+ const cancelText = options?.cancelText || "Cancel";
16
+ return (_jsx(Dialog, { open: open, onOpenChange: setOpen, children: _jsxs(DialogContent, { children: [_jsx(DialogTitle, { children: title }), _jsx(DialogDescription, { children: message }), _jsxs(DialogFooter, { children: [_jsx(Button, { variant: "outline", className: "min-w-14", onClick: () => setOpen(false), children: cancelText }), _jsx(Button, { className: "min-w-14", onClick: () => {
17
+ setOpen(false);
18
+ console.log("onConfirm", values);
19
+ onConfirm?.(values);
20
+ }, children: confirmText })] })] }) }));
21
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mtxuilib",
3
3
  "private": false,
4
- "version": "0.1.277",
4
+ "version": "0.1.279",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },