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.

189 lines (188 loc) 10.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.storeUpdater = exports.createActionsReducer = void 0; var _storeScopeUpdater = require("@ui-schema/react/storeScopeUpdater"); var _UIStore = require("@ui-schema/react/UIStore"); var _schemaTypeIs = require("@ui-schema/ui-schema/schemaTypeIs"); var _Utils = require("@ui-schema/ui-schema/Utils"); var _immutable = require("immutable"); function getScopedData(store, scopes, storeKeys) { var data = {}; if (scopes.includes('value')) { var _store$values; data.value = storeKeys.size ? (_store$values = store.values) === null || _store$values === void 0 ? void 0 : _store$values.getIn(storeKeys) : store.values; } if (scopes.includes('internal')) { var _store$internals, _store$internals2; data.internal = storeKeys.size ? getSelf((_store$internals = store.internals) === null || _store$internals === void 0 ? void 0 : _store$internals.getIn((0, _UIStore.addNestKey)('children', storeKeys)), 'self') : (_store$internals2 = store.internals) === null || _store$internals2 === void 0 ? void 0 : _store$internals2.get('self'); } if (scopes.includes('valid')) { var validity = (0, _UIStore.getValidity)(storeKeys, store.getValidity()); data.valid = typeof validity === 'undefined' || validity === null ? undefined : { valid: Boolean(getSelf(validity, 'valid')), errors: getSelf(validity, 'errors') }; } if (scopes.includes('meta')) { data.meta = store.meta; } return data; } var getSelf = function getSelf(value) { var selfKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'self'; if (_immutable.Map.isMap(value)) { return value.get(selfKey); } return undefined; }; function updateScopedData(store, scopes, storeKeys, data, op) { if (scopes.includes('value')) { store = (0, _storeScopeUpdater.scopeUpdaterValues)(store, storeKeys, data.value, op); } if (scopes.includes('internal')) { store = (0, _storeScopeUpdater.scopeUpdaterInternals)(store, storeKeys, data.internal, op); } if (scopes.includes('valid')) { store = (0, _storeScopeUpdater.scopeUpdaterValidity)(store, storeKeys, data.valid, op); } if (scopes.includes('meta')) { store = store.set('meta', data.meta); } return store; } var shouldDelete = function shouldDelete(value, action) { var _action$schema, _action$schema2; return (0, _UIStore.shouldDeleteOnEmpty)(value, (action === null || action === void 0 || (_action$schema = action.schema) === null || _action$schema === void 0 ? void 0 : _action$schema.get('deleteOnEmpty')) || (action === null || action === void 0 ? void 0 : action.required), action === null || action === void 0 || (_action$schema2 = action.schema) === null || _action$schema2 === void 0 ? void 0 : _action$schema2.get('type')); }; var storeActionHandlersMap = { 'list-item-add': function listItemAdd(store, action) { var data = getScopedData(store, ['value'], action.storeKeys); if ('itemValue' in action) { data.value || (data.value = (0, _immutable.List)()); data.value = data.value.push(action.itemValue); } else { var schema = action.schema; var items = schema === null || schema === void 0 ? void 0 : schema.get('items'); var type = schema === null || schema === void 0 ? void 0 : schema.getIn(['items', 'type']); var itemDefault = schema === null || schema === void 0 ? void 0 : schema.getIn(['items', 'default']); data.value || (data.value = (0, _immutable.List)()); data.value = data.value.push(typeof itemDefault !== 'undefined' ? itemDefault : (0, _schemaTypeIs.schemaTypeIs)(type, 'object') ? (0, _immutable.OrderedMap)() : _immutable.List.isList(items) || (0, _schemaTypeIs.schemaTypeIs)(type, 'array') ? (0, _immutable.List)() : (0, _schemaTypeIs.schemaTypeIs)(type, 'string') ? '' : (0, _schemaTypeIs.schemaTypeIs)(type, 'number') ? '' : (0, _schemaTypeIs.schemaTypeIs)(type, 'boolean') ? false : null); } store = (0, _storeScopeUpdater.scopeUpdaterValues)(store, action.storeKeys, data.value, 'set'); store = (0, _storeScopeUpdater.scopeUpdaterInternals2)(store, action.storeKeys, 'set', function (internal) { return (internal || (0, _immutable.Map)()).update('children', function () { var internalInternals = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _immutable.List)(); return internalInternals.set(data.value.size - 1, (0, _immutable.Map)({ self: (0, _immutable.Map)({ defaultHandled: true }) })); }); }); return store; }, 'list-item-delete': function listItemDelete(store, action) { var data = getScopedData(store, ['value'], action.storeKeys); data.value || (data.value = (0, _immutable.List)()); data.value = data.value.splice(action.index, 1); store = (0, _storeScopeUpdater.scopeUpdaterInternals2)(store, action.storeKeys, 'delete', function (internal) { return internal === null || internal === void 0 ? void 0 : internal.update('children', function () { var internalInternals = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _immutable.List)(); return internalInternals.splice(action.index, 1); }); }); if (shouldDelete(data.value, action)) { store = (0, _storeScopeUpdater.scopeUpdaterValues)(store, action.storeKeys, data.value, 'delete'); return store; } store = (0, _storeScopeUpdater.scopeUpdaterValues)(store, action.storeKeys, data.value, 'set'); return store; }, 'list-item-move': function listItemMove(store, action) { var data = getScopedData(store, ['value'], action.storeKeys); data.value || (data.value = (0, _immutable.List)()); data.value = (0, _Utils.moveItem)(data.value, action.fromIndex, action.toIndex); store = (0, _storeScopeUpdater.scopeUpdaterValues)(store, action.storeKeys, data.value, 'set'); store = (0, _storeScopeUpdater.scopeUpdaterInternals2)(store, action.storeKeys, 'set', function () { var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _immutable.Map)(); return (value || (0, _immutable.Map)()).update('children', function () { var internalInternals = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _immutable.List)(); return (0, _Utils.moveItem)(internalInternals.size - 1 < action.toIndex ? internalInternals.set(action.toIndex, undefined) : internalInternals.size - 1 < action.fromIndex ? internalInternals.set(action.fromIndex, undefined) : internalInternals, action.fromIndex, action.toIndex); }); }); return store; }, 'set': function set(store, action) { var _action$scopes; if ((_action$scopes = action.scopes) !== null && _action$scopes !== void 0 && _action$scopes.includes('value') && shouldDelete(action.data.value, action)) { var _action$scopes2; store = (0, _storeScopeUpdater.scopeUpdaterValues)(store, action.storeKeys, undefined, 'delete'); return updateScopedData(store, (_action$scopes2 = action.scopes) === null || _action$scopes2 === void 0 ? void 0 : _action$scopes2.filter(function (s) { return s !== 'value'; }), action.storeKeys, action.data, 'set'); } return updateScopedData(store, action.scopes, action.storeKeys, action.data, 'set'); }, 'update': function update(store, action) { var _action$scopes3; var data = action.updater(getScopedData(store, action.scopes, action.storeKeys)); if ((_action$scopes3 = action.scopes) !== null && _action$scopes3 !== void 0 && _action$scopes3.includes('value') && shouldDelete(data.value, action)) { var _action$scopes4; store = (0, _storeScopeUpdater.scopeUpdaterValues)(store, action.storeKeys, undefined, 'delete'); return updateScopedData(store, (_action$scopes4 = action.scopes) === null || _action$scopes4 === void 0 ? void 0 : _action$scopes4.filter(function (s) { return s !== 'value'; }), action.storeKeys, data, 'set'); } return updateScopedData(store, action.scopes, action.storeKeys, data, 'set'); }, 'delete': function _delete(store, action) { var _action$scopes5, _action$scopes6, _action$scopes7; if (!action.storeKeys.size) { throw new Error('Can not delete root value.'); } var data = getScopedData(store, ['value'], action.storeKeys.slice(0, -1)); if (typeof data.value === 'undefined') { return store; } if (!_immutable.Map.isMap(data.value)) { throw new Error('Can only delete in object values.'); } if (!action.scopes || (_action$scopes5 = action.scopes) !== null && _action$scopes5 !== void 0 && _action$scopes5.includes('value')) { store = (0, _storeScopeUpdater.scopeUpdaterValues)(store, action.storeKeys, undefined, 'delete'); } if (!action.scopes || (_action$scopes6 = action.scopes) !== null && _action$scopes6 !== void 0 && _action$scopes6.includes('internal')) { store = (0, _storeScopeUpdater.scopeUpdaterInternals)(store, action.storeKeys, (0, _immutable.Map)(), 'delete'); } if (!action.scopes || (_action$scopes7 = action.scopes) !== null && _action$scopes7 !== void 0 && _action$scopes7.includes('valid')) { store = (0, _storeScopeUpdater.scopeUpdaterValidity)(store, action.storeKeys, undefined, 'delete'); } return store; } }; var createActionsReducer = exports.createActionsReducer = function createActionsReducer(handlersMap) { return function (actions) { var actionsList; if (Array.isArray(actions)) { actionsList = actions; } else { actionsList = [actions]; } return function (oldStore) { return actionsList.reduce(function (nextStore, action) { var actionHandler = handlersMap[action.type]; if (actionHandler) { nextStore = actionHandler(nextStore, action); if (action.effect) { action.effect(getScopedData(nextStore, ['value', 'internal', 'valid', 'meta'], action.storeKeys), nextStore); } } else { console.log('store updater for type not found: ' + ('type' in action ? action.type : 'missing type'), action); } return nextStore; }, oldStore); }; }; }; var storeUpdater = exports.storeUpdater = createActionsReducer(storeActionHandlersMap);