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

onChange() for FormState #54

Merged
merged 7 commits into from
Nov 18, 2021
Merged

onChange() for FormState #54

merged 7 commits into from
Nov 18, 2021

Conversation

nighca
Copy link
Collaborator

@nighca nighca commented Aug 21, 2021

Fix #40, detail: #40 (comment)

1. Method onChange for FormState (& ArrayFormState)

// for FormState
const state = new FormState({
  foo: new FielState(1),
  bar: new FieldState(2)
})
state.onChange({ foo: 3, bar: 4 })
state.value // { foo: 3, bar: 4 }

// for ArrayFormState:
const state = new ArrayFormState([1, 2], v => new FieldState(v))
state.onChange([3, 4])
state.value // [3, 4]

2. Methods (remove, insert, append, move) for ArrayFormState

const state = new ArrayFormState(['a', 'b', 'c', 'd'], v => new FieldState(v))

state.remove(from, num) // remove items from specific index
state.remove(1, 2) // [a, b, c, d] => [a, d]

state.insert(from, ...items) // insert items from specific index
state.insert(0, 'e', 'f') // [a, b, c, d] => [e, f, a, b, c, d]

state.append(...items) // insert items at the end of current field list, shorthand for `state.insert(state.$.length, ...items)`
state.append('e', 'f') // [a, b, c, d] => [a, b, c, d, e, f]

state.move(from, to) // move item, with specific from index and target index
state.move(0, 2) // [a, b, c, d] => [b, c, a, d]

3. Make FormState (& ArrayFormState) fields readonly

Manipulating fields object (or array) $ in native way is not allowed any more. It's now impossible to do things like:

// for FormState
state.$.foo = ... // assignment to property

// for ArrayFormState:
state.$[1] = ...  // assignment to index
state.$.push(...) // array methods which causes mutation

If you want to do fields' manipulation, use methods provided above. Which means, you don't need to care about sub fields' creation & disposition any more, formstate-x takes care of it.

@nighca nighca mentioned this pull request Aug 21, 2021
Merged
13 tasks
src/adapter/index.ts Outdated Show resolved Hide resolved
@lzfee0227
Copy link
Collaborator

need more demos / cases?

  1. arrayFormState.onChange or arrayFormState.move/remove/insert/append? (index / ref)
  2. new ArrayFormState(list.map(() => new ArrayFormState())).set/reset/onChange() ?

@nighca
Copy link
Collaborator Author

nighca commented Aug 23, 2021

need more demos / cases?

@lzfee0227 是指这个 PR 的说明还是指 formstate-x 本身需要 demo?

src/formState.ts Show resolved Hide resolved
src/formState.ts Show resolved Hide resolved
@nighca nighca merged commit b853bd1 into qiniu:v3.x Nov 18, 2021
@nighca
Copy link
Collaborator Author

nighca commented Nov 18, 2021

看起来没有大的问题,这里先 merge(到 v3.x 分支),有后续问题可以在 v3.x 合并到 master 的时候再处理

@nighca nighca mentioned this pull request Nov 18, 2021
@lzfee0227
Copy link
Collaborator

是指这个 PR 的说明还是指 formstate-x 本身需要 demo

不是一个意思吗?
不是在这里写一下,然后最后 copy 到 formstate-x 相关文档里?

@nighca
Copy link
Collaborator Author

nighca commented Nov 19, 2021

不是一个意思吗? 不是在这里写一下,然后最后 copy 到 formstate-x 相关文档里?

不着急,文档或者 demo 在 v3 最后补;后边 API 名、接口细节大概率还会调整,现在写了,回头也没法 copy 直接用的

@nighca nighca mentioned this pull request Nov 28, 2021
nighca added a commit to nighca/formstate-x that referenced this pull request Mar 4, 2022
* onChange for FormState & methods for ArrayFormState

* test case for onChange & ArrayFormState methods

* enable CI for branch `v3.x`

* fix adapter

* typo

* typo

* cases for `onChange` of `FormState` (mode: object)
@nighca nighca mentioned this pull request Apr 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants