mt-flowbite-react
Version:
Official React components built for Flowbite and Tailwind CSS
84 lines (83 loc) • 2.6 kB
JavaScript
'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useRef } from 'react';
import { create, useStore } from 'zustand';
import { persist, subscribeWithSelector } from 'zustand/middleware';
import { mtmApiCall } from '../mtmapi/mtmapi';
export const FormDesignService = {
ready: false,
count: {
// builtin: 0,
},
init() {
if (this.ready) {
return;
}
this.ready = true;
},
search(text) { },
};
const createFormDesignStore = (initProps) => {
const DEFAULT_PROPS = {
id: 0,
};
return create()(persist(subscribeWithSelector((set, get) => {
console.log('createFormDesignStore', initProps);
return {
...DEFAULT_PROPS,
...initProps,
getFormData: async () => {
console.log('getFormData');
const pre = get();
const formData = await mtmApiCall('resForm', 'byId', { id: pre.id });
set((state) => ({
formData: formData,
}));
},
};
}), {
name: `form-design-${initProps?.id}`,
version: 1,
// skipHydration: true,
onRehydrateStorage(state) {
console.log('onRehydrateStorage[formdesign]', state);
},
migrate(persistedState, version) {
console.log('version', version, 'persistedState', persistedState);
return persistedState;
},
}));
};
//-----------context
const Context = createContext(null);
export function FormDesignProvider({ children, ...props }) {
const storeRef = useRef();
if (!storeRef.current) {
storeRef.current = createFormDesignStore(props);
}
return (_jsxs(Context.Provider, { value: storeRef.current, children: [children, _jsx(SubscriptionSetup, {})] }));
}
//------------事件订阅
function SubscriptionSetup() {
// const store = useFormDesignStore()
useEffect(() => {
// const unsub2 = store.subscribe((state) => state.items, (a) => {
// // console.log("items 事件:", a)
// })
return () => {
// unsub2()
};
}, []);
return _jsx(_Fragment, {});
}
export function useFormDesignStore(selector, equals) {
const store = useContext(Context);
if (!store)
throw new Error('useFormDesignStore Missing FormDesignProvider');
if (selector) {
return useStore(store, selector, equals);
}
else {
return store;
}
}