Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Jan 16, 2024
1 parent f1f5f60 commit 3f60b68
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
},
"dependencies": {
"color": "^4.2.3",
"helux": "^4.1.6",
"helux": "^4.2.0",
"helux-store": "workspace:^",
"speed-form": "workspace:^"
}
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"axios": "^1.6.2",
"classnames": "^2.3.2",
"color": "^4.2.3",
"helux": "^4.1.6",
"helux": "^4.2.0",
"helux-store": "workspace:^",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion example/src/FormDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const NetworkForm = ()=>{
</FieldRow>
}}
</Network.Field>
<Network.Action<'submit'> type="submit" scope="fdfd">
<Network.Action<typeof Network.fields.wifi.submit> state="">
{({title,visible,loading,enable,execute,timeout})=>{
return <Button loading={loading} timeout={timeout} visible={visible} enable={enable} onClick={execute()}>{title}</Button>
}}
Expand Down
9 changes: 9 additions & 0 deletions example/src/forms/network.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ const formSchema = {
help: "密码长度应不小于6位",
enable: (net: any) => (net as NetworkType).interface.value === "wifi",
validate: (value: string) => value.length > 6,
},
submit: { // 这是一个动作
title: "提交wifi",
enable: (net: any) => (net as NetworkType).interface.value === "wifi",
validate: (value: string) => value.length > 6,
execute:async (wifi:any)=>{
await delay(2000)
console.log(wifi)
}
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@testing-library/react": "^14.1.2",
"@types/react": "^18.2.42",
"@vitejs/plugin-react": "^4.2.1",
"helux": "^4.1.6",
"helux": "^4.2.0",
"jsdom": "^23.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
16 changes: 9 additions & 7 deletions packages/form/src/action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ export type ActionRecords<Actions extends Record<string,any>> = {
[Name in keyof Actions]: (options?:ActionExecuteOptions)=>Promise<void>
}


export type FormActionState = {
execute:AsyncComputedObject
} & Record<string,any>

function createFormAction<Scope extends Dict = Dict,Result=any>(name:string,setState:any){
return async (options?:ActionExecuteOptions)=>{
// action.execute依赖于scope和count两个属性,当变化时会触发重新执行
// 由于action.execute依赖于count,所以当count++时会触发动作执行
const opts = Object.assign({timeout:0,debounce:0,noReentry:false},options)

let fn = async ()=> setState((state:any)=>state.actions[name].count++)
if(opts.noReentry){
fn = noReentry(fn)
Expand Down Expand Up @@ -170,9 +171,8 @@ export type ActionRender<State extends Dict,Params extends Dict = Dict>= (props:
*
*
*/
export type ActionProps<State extends Dict = Dict,PropTypes extends Dict = Dict,Params extends Dict = Dict,ActionKeys extends string = string> = {
// 动作类型名称
type:ActionKeys
export type ActionProps<State extends Dict = Dict,PropTypes extends Dict = Dict,Params extends Dict = Dict> = {
state:string | string[] // 声明该动作对应的状态路径
scope?: string | string[]
children: ActionRender<State,Params>
}
Expand Down Expand Up @@ -207,6 +207,8 @@ function createActionRenderProps(props:ActionProps,store:any,actionState:string,
ref
},actionState)
}



/**
* 创建动作组件
Expand All @@ -231,7 +233,7 @@ export function createActionComponent<Store extends Dict = Dict,ActionStates ext
* @param props
* @returns
*/
function Action<Type extends string=string,Params extends Dict=Dict,Scope extends Dict=Dict>(props: ActionProps<Store['state']['actions'][Type],Scope,Params,ActionKeys>):ReactNode{
function Action<State extends FormActionState=FormActionState,Params extends Dict=Dict,Scope extends Dict=Dict>(props: ActionProps<Store['state']['actions'][Type],Scope,Params,ActionKeys>):ReactNode{
const [state,setState] = store.useState()

const { type:actionKey,scope } = props
Expand All @@ -254,7 +256,7 @@ export function createActionComponent<Store extends Dict = Dict,ActionStates ext
}
return React.memo(Action,(oldProps:any, newProps:any)=>{
return oldProps.type === newProps.type || oldProps.scope === newProps.scope
}) as (<Type extends string=string,Params extends Dict=Dict,Scope extends Dict=Dict>(props: ActionProps<Store['state']['actions'][Type],Scope,Params,ActionKeys>)=>ReactNode)
}) as (<State extends FormActionState=FormActionState,Params extends Dict=Dict,Scope extends Dict=Dict>(props: ActionProps<Store['state']['actions'][Type],Scope,Params,ActionKeys>)=>ReactNode)
}


Expand Down
2 changes: 1 addition & 1 deletion packages/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@testing-library/react": "^14.1.2",
"@types/react": "^18.2.42",
"@vitejs/plugin-react": "^4.2.1",
"helux": "^4.1.6",
"helux": "^4.2.0",
"jsdom": "^23.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
56 changes: 28 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3f60b68

Please sign in to comment.