-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Form & FormItem component #108
Conversation
✅ Deploy Preview for ikun-ui ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
# Conflicts: # package.json # pnpm-lock.yaml
# Conflicts: # components/Checkbox/src/index.svelte # components/Input/src/index.svelte # components/Radio/src/index.svelte # components/Select/src/index.svelte # package.json # pnpm-lock.yaml # preset/src/shortcuts/index.ts
callback: (node: FormContext) => void | ||
) => { | ||
function traverse(node: Contexts) { | ||
if (node.__FormContext__) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个判断什么情况下会命中?我调试你给的例子中似乎没有进入过
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Form
- FormItem.a
- FormItem.a1
- FormItem.a2
- FormItem.a
这样的Form转化过来的Context的实际结构
{
__this__: FormContext a
a1:{
__this__: FormContext a1
}
a2:{
__this:__: FormContext a2
}
}
如果不判断if(node.FormContext)
遍历第一层则会遍历到三个属性__this__,a1,a2但这并不是FormContext结构。
anotherNodesKeys.forEach((item) => { | ||
traverse(node[item]); | ||
}); | ||
callback(currentContext); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个为什么不和重置一样,只对anotherNodesKeys.length === 0的上下文对象调用验证函数?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为FormItem可以嵌套,我们可以设想这么一个结构
- Form
- FormItem.a
- FormItem.a1
- FormItem.a2
- FormItem.b
- FormItem.a
有anotherNodesKeys.length === 0的情况, 意味着只遍历没有子节点的Context,在这个结构里只会遍历到a1、a2、b这类边缘节点。
如果没有anotherNodesKeys.length === 0,则a,a1,a2,b都会遍历到
validateValues需要遍历所有节点(a,a1,a2,b),这是因为用户可能在FormItem.a上设置rules(校验规则),我们允许直接在a上写rules。
而像resetValues(重置所有字段) 我们只需要重置a1,a2和b 这些边缘节点就好了,因为如果重置了a1,a2,再去重置a是冗余操作。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
顺带一提,当时调研validate用什么库,我最终选择了valibot这个库,如果有不妥的地方我们可以再讨论讨论
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validateValues需要遍历所有节点(a,a1,a2,b),这是因为用户可能在FormItem.a上设置rules(校验规则),我们允许直接在a上写rules。
我理解在整个数据结构上可能是 const form = { a: {a1:‘xxx’, a2: 'xxx'}, b: 'xxx'}, 我无法想象在对a1 和 a2 进行校验后还需要对 a 做什么样的校验
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
顺带一提,当时调研validate用什么库,我最终选择了valibot这个库,如果有不妥的地方我们可以再讨论讨论
valibot 库有什么优势?作为组件,我们只需要封装非空校验,复杂的校验应该由用户去完成,也就是说我认为应该让用户去使用 valibot ,而不是把它加入到组件库中
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validateValues需要遍历所有节点(a,a1,a2,b),这是因为用户可能在FormItem.a上设置rules(校验规则),我们允许直接在a上写rules。
我理解在整个数据结构上可能是 const form = { a: {a1:‘xxx’, a2: 'xxx'}, b: 'xxx'}, 我无法想象在对a1 和 a2 进行校验后还需要对 a 做什么样的校验
主要是允许用户在任意FormItem上写rules都生效,否则某用户直接在a上写rules,而a1和a2的rules===undefined的情况下,交验逻辑会失效
# Conflicts: # components/Checkbox/src/index.svelte # components/Radio/src/index.svelte # components/Switch/src/index.svelte # components/index.ts # pnpm-lock.yaml # preset/src/shortcuts/index.ts
# Conflicts: # components/Select/package.json # components/Select/src/index.svelte # components/index.ts # pnpm-lock.yaml # preset/src/shortcuts/index.ts
Preview
Featuret:
TODO:
form validatesupport Form components(Switch,CheckBox) to use inside FormItem(only need to bind the field bidirectionally)
and so on.....