@kaliber/forms 1.2.1 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -9,6 +9,7 @@ export {
9
9
  useObjectFormField,
10
10
  useFormFieldSnapshot,
11
11
  useFormFieldValue,
12
+ useFormFieldsValues,
12
13
  } from './src/hooks'
13
14
  export {
14
15
  array, object,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaliber/forms",
3
- "version": "1.2.1",
3
+ "version": "2.1.1",
4
4
  "main": "index.js",
5
5
  "sideEffects": false,
6
6
  "publishConfig": {
package/readme.md CHANGED
@@ -313,6 +313,40 @@ array(validation, fields)
313
313
  array(fields)
314
314
  ```
315
315
 
316
+ If you want to use heterogeneous arrays (different types) you can use a `function` instead of a `fields` object:
317
+
318
+ ```js
319
+ array(initialValue => ({
320
+ _type: required,
321
+ ...(
322
+ initialValue._type === 'content' ? { text: required } :
323
+ initialValue._type === 'image' ? { image: required } :
324
+ null
325
+ )
326
+ }))
327
+ ```
328
+
329
+ When rendering the `array` field you can render a different component based on the value of the field:
330
+
331
+ ```jsx
332
+ const { state: { children }, helpers } = useArrayFormField(field)
333
+
334
+ return (
335
+ <>
336
+ {children.map(field => {
337
+ const { _type } = field.value.get()
338
+ return (
339
+ _type === 'content' ? <ContentForm key={field.name} {...{ field }} /> :
340
+ _type === 'image' ? <ImageForm key={field.name} {...{ field }} /> :
341
+ null
342
+ )
343
+ })}
344
+ <button type='button' onClick={_ => helpers.add({ _type: 'content' })}>Add content</button>
345
+ <button type='button' onClick={_ => helpers.add({ _type: 'image' })}>Add image</button>
346
+ </>
347
+ )
348
+ ```
349
+
316
350
  #### object
317
351
 
318
352
  Used to create an object form field.
package/src/fields.js CHANGED
@@ -41,7 +41,9 @@ export function createObjectFormField({ name = '', initialValue = {}, field }) {
41
41
  children.forEach(x => x.setSubmitted(isSubmitted))
42
42
  },
43
43
  reset() {
44
- internalState.update(x => updateState(x, initialState))
44
+ internalState.update(x =>
45
+ updateState(x, pick(initialState, ['isSubmitted']))
46
+ )
45
47
  children.forEach(x => x.reset())
46
48
  },
47
49
  value,
@@ -103,7 +105,9 @@ function createArrayFormField({ name, initialValue = [], field }) {
103
105
  children.forEach(x => x.setSubmitted(isSubmitted))
104
106
  },
105
107
  reset() {
106
- const { children } = internalState.update(x => initialState)
108
+ const { children } = internalState.update(x =>
109
+ updateState(x, pick(initialState, ['children', 'isSubmitted']))
110
+ )
107
111
  children.forEach(x => x.reset())
108
112
  },
109
113
  value,
@@ -126,10 +130,11 @@ function createArrayFormField({ name, initialValue = [], field }) {
126
130
 
127
131
  function createFormField(initialValue) {
128
132
  const fullName = `${name}[${index++}]`
133
+ const fields = typeof field.fields == 'function' ? field.fields(initialValue) : field.fields
129
134
  return createObjectFormField({
130
135
  name: fullName,
131
136
  initialValue,
132
- field: normalize({ type: 'object', fields: field.fields }, fullName),
137
+ field: normalize({ type: 'object', fields }, fullName),
133
138
  })
134
139
  }
135
140
  }
@@ -160,7 +165,9 @@ function createBasicFormField({ name, initialValue, field }) {
160
165
  internalState.update(x => updateState(x, { isSubmitted }))
161
166
  },
162
167
  reset() {
163
- internalState.update(x => initialFormFieldState)
168
+ internalState.update(x =>
169
+ updateState(x, pick(initialFormFieldState, ['value', 'isSubmitted', 'isVisited']))
170
+ )
164
171
  },
165
172
  value,
166
173
  state: { get: internalState.get, subscribe: internalState.subscribe },
@@ -186,6 +193,13 @@ function updateState(formFieldState, update) {
186
193
  return deriveFormFieldState({ ...formFieldState, ...update })
187
194
  }
188
195
 
196
+ function pick(o, properties) {
197
+ return properties.reduce(
198
+ (result, property) => ({ ...result, [property]: o[property] }),
199
+ {}
200
+ )
201
+ }
202
+
189
203
  function deriveFormFieldState({
190
204
  error = false,
191
205
  isSubmitted = false,
package/src/hooks.js CHANGED
@@ -36,7 +36,6 @@ export function useForm({ initialValues = undefined, fields, validate = undefine
36
36
 
37
37
  function handleReset() {
38
38
  formRef.current.reset()
39
- formRef.current.validate({ form: initialValues, parents: [] })
40
39
  }
41
40
  }
42
41