UNPKG

@ui-schema/react

Version:

Schema-driven UI generator for React using JSON Schema. Build powerful form and interface generators with headless components and hooks.

20 lines 1.77 kB
import { storeBuildScopeTree } from '@ui-schema/react/storeBuildScopeTree'; import { addNestKey, prependKey } from '@ui-schema/react/UIStore'; import { updateStoreScope } from '@ui-schema/react/storeScopeUpdater'; import { List, Map } from 'immutable'; export const scopeUpdaterValidity = (store, storeKeys, newValue, op) => { store = storeBuildScopeTree(storeKeys, 'validity', store, op === 'set' || typeof newValue === 'undefined' ? key => typeof key === 'number' ? List() : Map() : undefined, () => Map({})).store; if (op === 'delete' || typeof newValue === 'undefined') { store = store.deleteIn(prependKey(storeKeys.size ? addNestKey('children', storeKeys) : Array.isArray(storeKeys) ? List(storeKeys) : storeKeys, 'validity').push('valid')); store = store.deleteIn(prependKey(storeKeys.size ? addNestKey('children', storeKeys) : Array.isArray(storeKeys) ? List(storeKeys) : storeKeys, 'validity').push('errors')); } else { if (!store.validity) { store = store.set('validity', Map()); } const valid = typeof newValue === 'boolean' ? newValue : Map.isMap(newValue) ? newValue.get('valid') : typeof newValue === 'object' && newValue && typeof newValue.valid === 'boolean' && newValue.valid; const errors = Map.isMap(newValue) ? newValue.get('errors') : typeof newValue === 'object' && newValue ? newValue.errors : undefined; store = updateStoreScope(store, 'validity', (storeKeys.size ? addNestKey('children', storeKeys) : Array.isArray(storeKeys) ? List(storeKeys) : storeKeys).push('valid'), Boolean(valid)); store = updateStoreScope(store, 'validity', (storeKeys.size ? addNestKey('children', storeKeys) : Array.isArray(storeKeys) ? List(storeKeys) : storeKeys).push('errors'), errors || null); } return store; };