Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Aug 19, 2024
1 parent cd27b49 commit b4977a0
Show file tree
Hide file tree
Showing 13 changed files with 372 additions and 79 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
.rslib
6 changes: 2 additions & 4 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ 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 NetworkForm from './forms/network'
// import UserForm from './forms/userform/UserForm'
// import UserFormWithValidate from "./UserFormValidate"

const menuItems=[
{id:"computed",title:"计算属性",component:<ComputedDemo/>},
{id:"useStore",title:"useStore",component:<UseStoreDemo/>,default:true},
{id:"NetworkForm",title:"网络配置",component:<NetworkForm/>,default:true},

// {id:"render",title:"useState",component:<UseStateDemo/>},
// {id:"user",title:"用户",component:<UserForm/>},
Expand Down
1 change: 1 addition & 0 deletions example/src/forms/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as NetworkForm from "./network"
7 changes: 1 addition & 6 deletions example/src/forms/network/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,7 @@ Network.fields.wifi.cancelableSubmit.execute
globalThis.Network = Network;
export default Network;


Network.setState(draft=>{
draft.fields.title.value="123"
draft.fields.ip.value="123"
})




// const form = createForm({
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function useActionRunner<State extends FormActionState=FormActionState>(actionSt



function useActionCanceller<State extends FormActionState=FormActionState>(state:State,valuePath:string | string[]){
function useActionCanceller<State>(state:State,valuePath:string | string[]){
return useCallback((event:any)=>{
const execute = getValueByPath(state,[...Array.isArray(valuePath) ? valuePath : valuePath.split(".") ,'execute'])
execute.cancel()
Expand Down Expand Up @@ -251,15 +251,14 @@ export function createActionComponent<State extends Dict = Dict>(store:FormStore
* @returns
*/
function Action<State extends FormActionState=FormActionState,Scope extends Dict=Dict>(props: ActionProps<State,Scope>):ReactNode{
const [state] = store.useState()

const [state] = store.useState()
let { name:actionKey } = props
// 如果动作是声明在actions里面可以省略actions前缀
if(!actionKey.includes(".")) actionKey = `actions.${actionKey}`

const actionState = getValueByPath(state,actionKey,".")
const actionRunner = useActionRunner(actionState,props)
const actionCanceller = useActionCanceller(actionState,actionKey)
const actionCanceller = useActionCanceller(state,actionKey)
// 用来引用当前动作
const ref = useRef<HTMLElement>(null)

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ export type FormStatus = 'idle'
*/
function createValidatorHook<State extends Dict = Dict>(valuePath:string[],getter:Function,options:ComputedOptions,formOptions:RequiredFormOptions<State>){
if(valuePath.length>=2 && valuePath[0]==FIELDS_STATE_KEY && valuePath[valuePath.length-1]==VALIDATE_COMPUTED_GROUP){
// 如果没有指定scope,则默认指向value
if(!options.scope) options.scope="value"
// 如果没有指定scope,则默认指向当前字段的value
if(!options.scope) options.scope="./value"
if(!options.depends) options.depends=[]
options.depends.push([...valuePath.slice(0,-1),"value"])
// 默认=null代表还未校验
Expand Down
5 changes: 3 additions & 2 deletions packages/reactive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@
}
},
"scripts": {
"build": "tsup"
"build": "tsup",
"build:rslib":"rslib build"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"flex-tools": "^1.4.5",
"limu": "^3.12.4",
"mitt": "^3.0.1"
},
"devDependencies": {
"@rslib/core": "^0.0.2",
"@testing-library/jest-dom": "^6.1.5",
"@testing-library/react": "^14.1.2",
"@vitejs/plugin-react": "^4.2.1",
Expand Down
40 changes: 40 additions & 0 deletions packages/reactive/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { defineConfig } from '@rslib/core';



export default defineConfig({
lib: [
{
format: 'esm',
dts:{
bundle:true,
},
output: {
minify: true,
distPath: {
root: './dist/esm',
},
},
},
{
format: 'cjs',
dts:{
bundle:true,
},
output: {
minify: true,
distPath: {
root: './dist/cjs',
},
},
},
],
source: {
entry: {
index: './src/index.ts',
},
},
output: {
target: 'node',
},
});
6 changes: 3 additions & 3 deletions packages/reactive/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
* watch函数的使用方式与computed类似,使用相同的逻辑
*
*/
import { IOperateParams } from "helux";
import { OBJECT_PATH_DELIMITER } from "./consts";
import { type ComputedScope, ComputedScopeRef, StoreOptions, Dict, IStore } from "./store/types";
import { type ComputedScope, ComputedScopeRef, StoreOptions, IStore } from "./store/types";
import { getRelValuePath, getValueByPath } from "./utils";
import { ComputedOptions, ComputedRunContext, ComputedType, StateComputedType } from "./computed/types";
import { ComputedOptions, ComputedType, StateComputedType } from "./computed/types";
import { Dict } from "./types";

/*
* 计算函数的context可以在全局Store中通过computedThis参数指定
Expand Down
6 changes: 0 additions & 6 deletions packages/reactive/src/reactives/limu.ts

This file was deleted.

5 changes: 3 additions & 2 deletions packages/reactive/src/utils/getRelValuePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { OBJECT_PATH_DELIMITER } from "../consts"
self = [ 'a', 'b', 'c', 'd', 'e', 'f' ]
root = []
parent = [ 'a', 'b', 'c', 'd' ]
current = [ 'a', 'b', 'c', 'd', 'e' ]
current = [ 'a', 'b', 'c', 'd', 'e' ] 当前对象
['a','b'] = [ 'a', 'b' ]
m = [ 'a', 'b', 'c', 'd', 'e', 'm' ]
x = [ 'a', 'b', 'c', 'd', 'e', 'x' ]
Expand All @@ -23,6 +23,7 @@ x = [ 'a', 'b', 'c', 'd', 'e', 'x' ]
*
* @param curPath
* @param relPath
* @param isRelPath 是否使用相对路径
* @returns
*/
export function getRelValuePath(curPath:string[],relPath:'self' | 'root' | 'parent' | 'current' | string[] | string,isRelPath?:boolean):string[]{
Expand All @@ -43,7 +44,7 @@ export function getRelValuePath(curPath:string[],relPath:'self' | 'root' | 'pare
return getRelValuePath(curPath.slice(0,-1),relPath.slice(3),true)
}else if(relPath.startsWith("/")){ // 绝对路径
relPath = relPath.replace(/^(\/)*/,"")
return [...relPath.split(OBJECT_PATH_DELIMITER)]
return relPath.split(OBJECT_PATH_DELIMITER)
}else{
return isRelPath ? [...curPath.slice(0,-1),...relPath.split(OBJECT_PATH_DELIMITER)] : [...relPath.split(OBJECT_PATH_DELIMITER)]
}
Expand Down
6 changes: 5 additions & 1 deletion packages/reactive/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
},
"exclude": [
"src/__tests__",
"vite.config.ts"
]
}

Loading

0 comments on commit b4977a0

Please sign in to comment.