Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Aug 23, 2024
1 parent 60e1afe commit 6369102
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 436 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export function action<Values extends Dict=Dict,R=any>(getter: AsyncComputedGett
// 比如 getSnap(state.fields.xxx.xxx)也是返回整个state的快照
const data = getFormData(Object.assign({},scope))
return await (getter as unknown as AsyncComputedGetter<R>)(data,opts)
},[],options)
},[],Object.assign({},options,{async:true}))
}


Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export function createForm<State extends FormDefine=FormDefine>(schema: State,op
Field : createFieldComponent<State>(formStore,opts),
Group : createFieldGroupComponent<State>(formStore,opts),
Action : createActionComponent<State>(formStore),
Submit : createSubmitComponent<State>(formStore),
Submit : createSubmitComponent<State>(formStore,opts),
Reset : createResetComponent<State>(formStore,opts),
useAction : createUseAction<State>(formStore) as UseActionType,
fields : createObjectProxy(()=>formStore.state.fields) as FieldsType,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/reset.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Dict } from "@speedform/reactive"
import type { FormStore, RequiredFormOptions } from "./form"
import { ActionProps, createActionComponent } from "./action"
import { action, ActionProps, createActionComponent } from "./action"
import { DEFAULT_RESET_ACTION } from "./consts"

export type ResetComponentProps = React.PropsWithChildren<{
Expand Down Expand Up @@ -35,7 +35,7 @@ export const $reset = {
enable: true,
validate: true,
readonly: false,
execute: async () => {
execute: action(async () => {

}
})
}
51 changes: 32 additions & 19 deletions packages/core/src/submit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
*
*/
import React, { useRef } from 'react'
import React, { useCallback, useRef } from 'react'
import { Dict, getValueByPath } from "@speedform/reactive";
import type { FormSchemaBase, FormStore, RequiredFormOptions } from "./form";
import { CSSProperties, ReactElement, ReactNode } from "react";
import { isFieldGroup, isFieldList, isFieldValue } from "./utils";
import { ActionProps, createActionComponent } from "./action";
import { ActionProps, createActionComponent,action } from "./action";
import { DEFAULT_SUBMIT_ACTION } from "./consts";
import { styled } from 'flexstyled';

Expand Down Expand Up @@ -91,6 +91,7 @@ function createBehaviorRenderProps<State extends FormBehaviorState=FormBehaviorS
},
getFormAttrs(formState))
}

const BehaviorChildren = React.memo((props:{submitProps:BehaviorRenderProps<any>,children:any})=>{
return <>{
typeof(props.children)=='function' && props.children(props.submitProps as any)
Expand Down Expand Up @@ -183,7 +184,7 @@ export type SubmitComponentProps = React.PropsWithChildren<{
* 创建一个提交组件,某行为
*
* 提交整个表单
* <Submit label="" timeout={12}></Submit>
* <Submit label="" timeout={12}></Submit>
* 提交表单局部,scope=只能指定一个字段组
* <Submit label="" timeout={12} scope={["xx","xx"]}></Submit>
* <Submit label="" timeout={12} scope="user.password"}></Submit>
Expand All @@ -192,22 +193,33 @@ export type SubmitComponentProps = React.PropsWithChildren<{
* @param formOptions
* @returns
*/
export function createSubmitComponent<State extends Dict = Dict>(store:FormStore<State>) {
export function createSubmitComponent<State extends Dict = Dict>(store:FormStore<State>,formOptions:RequiredFormOptions<State>) {
const Action = createActionComponent(store)

return ((props:SubmitComponentProps)=>{
const submitRef = useRef<HTMLInputElement>(null)
const handleSubmit = (event: React.MouseEvent<HTMLButtonElement>) => {
event.preventDefault();

};

// 处理提交事件
const handleSubmit = useCallback((event: React.MouseEvent<HTMLButtonElement>) => {
if(formOptions.validAt==='submit'){
// store.computedObjects.runGroup(VALIDATE_COMPUTED_GROUP).then(()=>{
// submitRef.current?.click()
// })
}else{
submitRef.current?.click()
}
event.preventDefault();
},[])

return (<Action {...props} name={DEFAULT_SUBMIT_ACTION}>
{
({loading,title})=>{
({loading,title,run})=>{


return (
<div className="speedform-submit">
<input ref={submitRef} type="submit" value={props.label || title} />
<button type="submit" onClick={handleSubmit}>{props.label || title} </button>
<input ref={submitRef} type="submit" style={{display:'none'}} value={props.label || title} />
<button type="submit" onClick={run({extras:1})}>=={props.label || title} ==</button>
<span>{loading ? '提交中2' : ''}</span>
</div>
)
Expand All @@ -221,14 +233,15 @@ export function createSubmitComponent<State extends Dict = Dict>(store:FormStore

// 默认的提交动作
export const $submit = {
title: "提交",
help: "",
tips: "提交",
visible: true,
enable: true,
title : "提交",
help : "",
tips : "提交",
visible : true,
enable : true,
validate: true,
readonly: false,
execute: async () => {

}
execute: action(async (scope:any,options:any) => {
console.log("scope=",scope,"options=",options)
debugger
})
}
4 changes: 3 additions & 1 deletion packages/reactive/src/computed/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ async function executeComputedGetter<T extends Dict>(draft:any,computedRunContex
getProgressbar: (options)=>createComputeProgressbar(setState,valuePath,options),
getSnap : (scope:any)=>getSnap(scope,false),
abortSignal : abortController.signal,
cancel : abortController.abort
cancel : abortController.abort,
extras : computedOptions.extras
}
let hasAbort=false // 是否接收到可中止信号

Expand Down Expand Up @@ -203,6 +204,7 @@ function createComputed<T extends Dict>(computedRunContext:ComputedRunContext,co
store.options.log(`Async computed <${computedDesc}> is disabled`,'warn')
return
}

store.options.log(`Run async computed for : ${computedDesc}`);

const finalComputedOptions = Object.assign({},computedOptions,options) as Required<ComputedOptions>
Expand Down
6 changes: 5 additions & 1 deletion packages/reactive/src/computed/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export interface ComputedProgressbar{
* @returns
*/
cancel:()=>void
/**
* 额外的参数,用来传递给计算函数
*/
extras?:any
}

export interface ComputedOptions<Value=any,Extras extends Dict={}> {
Expand Down Expand Up @@ -181,7 +185,7 @@ export interface ComputedProgressbar{
* 当在hook中使用时就不需要保存到store.computedObjects中
*
*/
save?:boolean
save?:boolean

};

Expand Down
2 changes: 1 addition & 1 deletion packages/reactive/src/reactives/helux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class HeluxReactiveable<State extends Dict =Dict> extends Reactiveable<St
// 此函数在依赖变化时执行,用来异步计算
// extraArgs是在调用run方法时传入的额外计算参数,可用来覆盖计算参数
task: async ({ draft, setState, input, extraArgs }) => {
if(typeof(initial)=='function'){// @ts-ignore
if(typeof(onComputed)=='function'){// @ts-ignore
return onComputed({draft,setState,values:input,options:Object.assign({},extraArgs)})
}
},
Expand Down
Loading

0 comments on commit 6369102

Please sign in to comment.