diff --git a/example/src/App.tsx b/example/src/App.tsx index f61ce4a..e918c8a 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -3,12 +3,16 @@ import { useState } from 'react' import classnames from "classnames" // import UseStateDemo from './UseStateDemo' import ComputedDemo from './computeds/basic' +import UseStoreDemo from './computeds/useStore' + // import NetworkForm from './forms/network' // import UserForm from './forms/userform/UserForm' // import UserFormWithValidate from "./UserFormValidate" const menuItems=[ - {id:"computed",title:"计算属性",component:,default:true}, + {id:"computed",title:"计算属性",component:}, + {id:"useStore",title:"useStore",component:,default:true}, + // {id:"render",title:"useState",component:}, // {id:"user",title:"用户",component:}, // {id:"user_valid",title:"校验",component:,default:true}, diff --git a/example/src/computeds/useStore/index.tsx b/example/src/computeds/useStore/index.tsx new file mode 100644 index 0000000..fdf8c4b --- /dev/null +++ b/example/src/computeds/useStore/index.tsx @@ -0,0 +1,181 @@ +import { useReactive } from "ahooks"; +import { useStore, $} from "@speedform/reactive"; +import { delay } from "flex-tools/async/delay" +import { Card, ColorBlock, Divider, Tips } from "@speedform/demo-components"; + + +export type BookType = { + name:string + price:number + author:string + count:number + total:number +} +export type MyStateType = { + user: { + id: string; + firstName: string; + lastName: string; + fullName: (user:any) =>string; + age: number; + sex: 1 | 0, + addresss: { + city: string; + street: string; + }[]; + }; + books: { + name: string; + price: number; + author: string; + count:number + }[]; + orders: ({id:number,bookId:string,price:number,count:number})[] + sales: { + total: number; + }; +}; + +export default () => { + const store = useStore({ + user: { + id: "zhangfisher", + firstName: "zhang", + lastName: "tom", + fullName: (user: any) => { + return (user.firstName + user.lastName) as string; + }, + age: 18, + sex: 1, + addresss: [ + { city: "北京", street: "朝阳区" }, + { city: "上海", street: "浦东区" }, + { city: "广州", street: "天河区" }, + ], + // 异步计算,由于没有指定依赖,所以只会运行一次,如果要重新计算需要手动调用 + avatar: async () => { + await delay(1000); + return "User Avatar"; + }, + }, + books: [ + { + name: "张三", + price: 18, + author: "tom", + count: 2, + total: (book: BookType) => book.price * book.count, + }, + { + name: "李四", + price: 19, + author: "jack", + count: 3, + total: (book: BookType) => book.price * book.count, + }, + { + name: "王五", + price: 20, + author: "bob", + count: 4, + total: (book: BookType) => book.price * book.count, + }, + ], + orders: [], + sales: { + total: 0, + }, + }); + + // @ts-ignore + // globalThis.Store = store + const newBook = useReactive({ + name: "新书", + author: "zhang", + price: 100, + count: 1, + }); + + return ( +
+ + +
+
+ firstName= + +
+ +
+ + +
+ Age= + + +
+ +
+ + +
+
+ + + + + +
+ + {store.state.books.map((book, index: any) => { + return ( +
+ + + + + +
+ ); + })} + +
+ (newBook.name = e.target.value)} + /> + (newBook.author = e.target.value)} + /> + (newBook.price = Number(e.target.value))} + /> + (newBook.count = Number(e.target.value))} + /> +
+ {/* + */} +
+
+
+ ); +} + diff --git a/packages/reactive/src/index.ts b/packages/reactive/src/index.ts index 65060e3..7b130c9 100644 --- a/packages/reactive/src/index.ts +++ b/packages/reactive/src/index.ts @@ -1,4 +1,4 @@ -export * from "./store/store" +export * from "./store" export * from "./computed" export * from "./types" export * from "./watch" @@ -12,6 +12,7 @@ export { $, share as createObject } from "helux" + export { getValueByPath, isIncludePath, diff --git a/packages/reactive/src/reactives/helux.ts b/packages/reactive/src/reactives/helux.ts index fe3c176..e243ebd 100644 --- a/packages/reactive/src/reactives/helux.ts +++ b/packages/reactive/src/reactives/helux.ts @@ -24,7 +24,7 @@ export class HeluxReactiveable extends Reactiveable + return this._stateCtx.reactive as ComputedState } /** * const [ state ] = useState() diff --git a/packages/reactive/src/store/index.ts b/packages/reactive/src/store/index.ts index 5f813dc..75750b4 100644 --- a/packages/reactive/src/store/index.ts +++ b/packages/reactive/src/store/index.ts @@ -1,2 +1,3 @@ export * from "./store" -export * from "./types" \ No newline at end of file +export * from "./types" +export * from "./useStore" \ No newline at end of file diff --git a/packages/reactive/src/store/types.ts b/packages/reactive/src/store/types.ts index 9ed42ce..459af1e 100644 --- a/packages/reactive/src/store/types.ts +++ b/packages/reactive/src/store/types.ts @@ -1,4 +1,3 @@ -import { ISharedCtx} from "helux" import { ComputedState, StateComputedType } from '../computed/types'; import { type ComputedObjects, ComputedOptions } from '../computed'; import type { WatchObjects, createUseWatch, createWatch } from "../watch"; diff --git a/packages/reactive/src/store/useStore.ts b/packages/reactive/src/store/useStore.ts new file mode 100644 index 0000000..3034fab --- /dev/null +++ b/packages/reactive/src/store/useStore.ts @@ -0,0 +1,17 @@ +/** + * + * + * + */ + +import { useState } from "react" +import { createStore } from "./store" +import { Dict, IStore, StoreOptions } from "../types" + + +export function useStore(schema:T,options?:Partial>):IStore{ + const [ store ] = useState(()=>createStore(schema,Object.assign({ + immediate:true + },options))) + return store +} \ No newline at end of file