Skip to content
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

[cdk: forms]: reactive forms system #115

Closed
1 task done
danranVm opened this issue Jan 5, 2021 · 3 comments · Fixed by #121, #128, #139, #141 or #142
Closed
1 task done

[cdk: forms]: reactive forms system #115

danranVm opened this issue Jan 5, 2021 · 3 comments · Fixed by #121, #128, #139, #141 or #142
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@danranVm
Copy link
Member

danranVm commented Jan 5, 2021

  • I have searched the issues of this repository and believe that this is not a duplicate.

What problem does this feature solve?

What does the proposed API look like?

参考自 @angular/forms

特性

  • 显示创建表单模型: useFormGroup, useFormArray, useFormControl
  • 易扩展的表单验证: Validators
  • 响应式的表单状态: valid, invalid, validating, blurred, unblurred

API

示例

<template>
  <ix-form :control="group">
    Name: <ix-input control="name" /> <br />
    Age: <ix-input control="age" /> <br />
    Email: <ix-input control="email" />
  </ix-form>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { injectControl, provideControl, Validators, useFormGroup } from '@idux/cdk/forms'

const IxForm = defineComponent({
  props: ['control'],
  setup(props) {
    provideControl(props.control)
  },
  template: `<form><slot /></form>`,
})

const IxInput = defineComponent({
  props: ['control'],
  setup(props) {
    const control$ = injectControl(props.control)!

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const onChange = (evt: any) => {
      control$.setValue(evt.target!.value, { dirty: true })
    }

    return { control$, onChange }
  },
  template: `<input :value="control$.valueRef.value" @input="onChange" @blur="control$.markAsBlurred()" />`,
})

export default defineComponent({
  components: { IxForm, IxInput },
  setup() {
    const { required, min, max, minLength, maxLength } = Validators
    const group = useFormGroup({
      name: ['tom', required],
      age: [18, [required, min(1), max(99)]],
      email: ['', [minLength(8), maxLength(40)]],
    })

    group.watchValue(value => console.log(value))
    group.watchStatus(stats => console.log(stats))

    group.get('name')?.watchStatus(stats => console.log(stats))

    return { group }
  },
})
</script>

后续支持

  • useFormAccessor: 注册表单控制器

    • onChange(fn: any): void // 注册 onChange 事件。
      onTouched(fn: any): void // 注册 onTouched 事件
  • useValidators: 注册同步验证器 (自定义业务表单组件需要)

    • validate(value: any): ValidationErrors | null // 自定义验证函数
      onValidatorChange?(fn: () => void): void // 注册验证状态改变的事件 可选实现
  • useAsyncValidators:注册异步验证器 (自定义业务表单组件需要)

    • validate(value: any): Promise<ValidationErrors | null> // 自定义验证函数
      onValidatorChange?(fn: () => void): void // 注册验证状态改变的事件 可选实现
@danranVm danranVm added the enhancement New feature or request label Jan 5, 2021
@danranVm danranVm added this to the v1.0.0 milestone Jan 5, 2021
@danranVm danranVm assigned hele10086 and unassigned huangtenghui Jan 5, 2021
@danranVm
Copy link
Member Author

danranVm commented Jan 5, 2021

@coolyuantao @hele10086 负责此组件的 review, API 设计完成后请 @ 他们

@zymoplastic
Copy link
Contributor

touch 令人想到触摸屏的 touchEvent, 但是这里的语义应该是 blur, 我们组件库不支持手机端, 能否考虑换个名字

@danranVm
Copy link
Member Author

danranVm commented Jan 8, 2021

收到几个疑问:

  • Validators 这里的校验不通过后的提示信息是否放在required, min,max内部约定好,是否可配置?
  • useFormGroup这里的函数的声明是怎样的?(这里会有自定义的validator)
  • 是否要支持指令的形式,比如 ix-input v-model="xxx" :rules="[min(0), max(100)]" />
  • 需要有关于一些统一的错误提示方式的配置吧?

回复:

  • 内部约定好,可以配置和扩展
  • 只要是符合我们 Validator 类型声明的函数,就可以传到 useFormGroup 中,大概的类型就是 (value)=> ValidMessage | null 这种格式
  • 这个暂时先不支持吧,后面可以加上来,而且这个是要每个组件都去实现 rules 这个 API 的,跟我CDK 的关系可能不是很大,最多我提供一个注册 rules 的API
  • 这个肯定是有的,也是在具体的 form 组件去是实现, CDK 里暂不考虑。。

@danranVm danranVm closed this as completed Jan 8, 2021
@danranVm danranVm reopened this Jan 8, 2021
danranVm added a commit that referenced this issue Jan 10, 2021
@danranVm danranVm reopened this Jan 10, 2021
danranVm added a commit that referenced this issue Jan 10, 2021
@danranVm danranVm linked a pull request Jan 10, 2021 that will close this issue
13 tasks
danranVm added a commit that referenced this issue Jan 16, 2021
danranVm added a commit that referenced this issue Jan 16, 2021
This was linked to pull requests Jan 17, 2021
danranVm added a commit that referenced this issue Jan 18, 2021
CruellyJoHn pushed a commit to CruellyJoHn/components that referenced this issue Jan 22, 2021
fix IDuxFE#77

build: upgrade vite (IDuxFE#102)

refactor(comp:all): updalte all components ts definition (IDuxFE#103)

docs: update all components index.zh.md (IDuxFE#105)

* docs: update gen script

fix(comp: divider): dynamic slot (IDuxFE#104)

fix IDuxFE#76

feat(comp: spin): add spin component (IDuxFE#101)

fix IDuxFE#72

refactor(comp:core): useGlobalConfig (IDuxFE#107)

fix: vite traverse filename config (IDuxFE#106)

docs: update spin component demo (IDuxFE#109)

fix(comp: divider): slots rerender (IDuxFE#110)

refactor(comp:i18n): update useI18n to useLocale (IDuxFE#111)

feat(comp: space): add component space (IDuxFE#97)

fix IDuxFE#63

fix(comp:all): solot dynamic load (IDuxFE#114)

fix: add engines to package.json (IDuxFE#117)

yarn start error because of the low version of Node.js

fix IDuxFE#116

refactor: script gen type structure (IDuxFE#118)

refactor: script gen types template (IDuxFE#119)

* Update the template to make it more simpler.

style(scripts): add brace style eslint rule (IDuxFE#123)

fix IDuxFE#122

refactor(comp:all): export type declaration from current component (IDuxFE#124)

docs: contribution (IDuxFE#125)

update contributing.zh and add contributing.en

fix(comp: image): export type to fix vite error (IDuxFE#127)

feat(cdk:forms): add useFormControl and Validtors (IDuxFE#121)

* test(cdk:forms): add test

fix IDuxFE#115

docs(cdk:forms): add validators docs (IDuxFE#128)

re IDuxFE#115

docs: modify components' docs' type (IDuxFE#131)

feat(comp: empty): add component empty (IDuxFE#132)

* feat(comp:empty): update and add test

fix IDuxFE#54

feat(cdk:utils): add hasOwnProperty function (IDuxFE#137)

fix(comp: badge): overflowCount not work (IDuxFE#135)

fix IDuxFE#134

test(comp:badge,icon): fix warning (IDuxFE#140)

feat(comp: result): add result component (IDuxFE#136)

fix IDuxFE#112

feat(cdk:forms): add formGroup and formArray (IDuxFE#139)

* feat(cdk:forms): add test

re IDuxFE#115

feat(cdk:forms): add utils (IDuxFE#141)

re IDuxFE#115

refactor(cdk:forms): `modelRef` renamed to `valueRef` (IDuxFE#142)

re IDuxFE#115

feat(cdk:forms): add watchValue and watchStatus (IDuxFE#143)

re IDuxFE#115

refactor(cdk:forms): add dirty status and marks `valueRef` to readonly (IDuxFE#144)

re IDuxFE#115

feat(cdk:forms): setValue support configuration options (IDuxFE#146)

re IDuxFE#115

feat: add global types (IDuxFE#150)

docs(cdk:forms): add docs and demo (IDuxFE#149)

fix IDuxFE#115

refactor(cdk:forms): update typescript defintions (IDuxFE#151)

* docs(cdk:forms): update docs

feat(cdk: subject): add subject (IDuxFE#155)

fix IDuxFE#154

feat(comp: typography): add directive typography (IDuxFE#148)

* docs(comp: typography): modify docs

* feat(comp: typography): add type check

* feat(comp: typography): style

* test(comp: typography): add test

fix IDuxFE#130

feature(comp:card): 1.修改dom结构 2.补全css样式 3.重跑单测

feat(comp:card): 1.解决合并冲突

feat(comp:card): add card component
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment