mt-flowbite-react
Version: 
Official React components built for Flowbite and Tailwind CSS
90 lines (89 loc) • 3.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDropdownStore = exports.DropdownProvider = exports.createDropdownStore = exports.createDropdownSlice = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const zustand_1 = require("zustand");
const middleware_1 = require("zustand/middleware");
const createDropdownSlice = (set, get) => ({
    // schema: httpReqSchema,
    title: "-",
    items: [],
    id: "",
    name: "",
    handleSelect(value) {
        console.log("dropdown handle select", value);
    },
    // sendRequest() {
    //   const pre = get()
    //   console.log("请求参数", pre.formValues)
    // }
});
exports.createDropdownSlice = createDropdownSlice;
const createDropdownStore = (initProps) => {
    return (0, zustand_1.create)()((0, middleware_1.persist)((...a) => ({
        ...(0, exports.createDropdownSlice)(...a),
        // ...createFishSlice(...a),
    }), {
        name: 'httpToolSlice',
        version: 1,
        skipHydration: true,
        onRehydrateStorage(state) {
            console.log("onRehydrateStorage[httpToolSlice]", state);
        },
    }));
};
exports.createDropdownStore = createDropdownStore;
//-----------------------------------------------------------------------------------------context
const Context = (0, react_1.createContext)(null);
function DropdownProvider({ children, ...props }) {
    const storeRef = (0, react_1.useRef)();
    if (!storeRef.current) {
        storeRef.current = (0, exports.createDropdownStore)(props);
    }
    // useEffect(() => {
    //   //当输入的默认值变化时,重置数据上下文
    //   storeRef?.current?.getState()?.setDefaultValue(props.defaultValues)
    // }, [
    //   props.defaultValues
    // ])
    // useEffect(() => {
    //   //当from id 变化后,从远端加载表单配置。
    //   storeRef?.current?.getState()?.loadFormData()
    // }, [
    //   props.id
    // ])
    return ((0, jsx_runtime_1.jsxs)(Context.Provider, { value: storeRef.current, children: [children, (0, jsx_runtime_1.jsx)(SubscriptionSetup, {})] }));
}
exports.DropdownProvider = DropdownProvider;
//------------事件订阅
function SubscriptionSetup() {
    const store = useDropdownStore();
    (0, react_1.useEffect)(() => {
        // const unsub2 = store.subscribe((state) => state.defaultValues, (a) => {
        //   console.log("defaultValues 变化事件:", a)
        // }, {
        //   fireImmediately: true,
        // })
        // const unsubActivateId = store.subscribe((state) => state.activateId, (value) => {
        //   console.log("activateId 事件:", value)
        // })
        return () => {
            // unsub2()
            // unsubActivateId()
        };
    }, []);
    return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {}));
}
function useDropdownStore(selector, equals) {
    const store = (0, react_1.useContext)(Context);
    if (!store)
        throw new Error('Missing DropdownProvider');
    if (selector) {
        return (0, zustand_1.useStore)(store, selector, equals);
    }
    else {
        return store;
    }
}
exports.useDropdownStore = useDropdownStore;