UNPKG

mt-flowbite-react

Version:

Official React components built for Flowbite and Tailwind CSS

83 lines (82 loc) 2.72 kB
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 } from 'zustand/middleware'; export 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) // } }); export const createDropdownStore = (initProps) => { return create()(persist((...a) => ({ ...createDropdownSlice(...a), // ...createFishSlice(...a), }), { name: 'httpToolSlice', version: 1, skipHydration: true, onRehydrateStorage(state) { console.log("onRehydrateStorage[httpToolSlice]", state); }, })); }; //-----------------------------------------------------------------------------------------context const Context = createContext(null); export function DropdownProvider({ children, ...props }) { const storeRef = useRef(); if (!storeRef.current) { storeRef.current = createDropdownStore(props); } // useEffect(() => { // //当输入的默认值变化时,重置数据上下文 // storeRef?.current?.getState()?.setDefaultValue(props.defaultValues) // }, [ // props.defaultValues // ]) // useEffect(() => { // //当from id 变化后,从远端加载表单配置。 // storeRef?.current?.getState()?.loadFormData() // }, [ // props.id // ]) return (_jsxs(Context.Provider, { value: storeRef.current, children: [children, _jsx(SubscriptionSetup, {})] })); } //------------事件订阅 function SubscriptionSetup() { const store = useDropdownStore(); 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 (_jsx(_Fragment, {})); } export function useDropdownStore(selector, equals) { const store = useContext(Context); if (!store) throw new Error('Missing DropdownProvider'); if (selector) { return useStore(store, selector, equals); } else { return store; } }