UNPKG

test-jsonforms-react-spectrum-renderers

Version:

React Spectrum Renderer Set for JSONForms

194 lines (190 loc) 8.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); /* The MIT License Copyright (c) 2020 headwire.com, Inc https://github.com/headwirecom/jsonforms-react-spectrum-renderers Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ var react_1 = tslib_1.__importDefault(require("react")); var Add_1 = tslib_1.__importDefault(require("@spectrum-icons/workflow/Add")); var react_spectrum_1 = require("@adobe/react-spectrum"); var ErrorIndicator_1 = require("../../components/ErrorIndicator"); var SpectrumProvider_1 = tslib_1.__importDefault(require("../../additional/SpectrumProvider")); function getUIOptions(uischema, defaultLabel) { var _a, _b, _c; return { addButtonPosition: ((_a = uischema.options) === null || _a === void 0 ? void 0 : _a.addButtonPosition) === 'bottom' ? 'bottom' : 'top', addButtonLabel: ((_b = uischema.options) === null || _b === void 0 ? void 0 : _b.addButtonLabel) || "Add to " + defaultLabel, addButtonLabelType: ((_c = uischema.options) === null || _c === void 0 ? void 0 : _c.addButtonLabelType) === 'inline' ? 'inline' : 'tooltip', }; } exports.getUIOptions = getUIOptions; function AddButton(props) { var addButtonLabel = props.addButtonLabel, addButtonLabelType = props.addButtonLabelType, onPress = props.onPress; var button = (react_1.default.createElement(SpectrumProvider_1.default, null, react_1.default.createElement(react_spectrum_1.ActionButton, { UNSAFE_className: 'add-button', onPress: onPress }, react_1.default.createElement(Add_1.default, { "aria-label": 'Add' }), addButtonLabelType === 'inline' && react_1.default.createElement(react_spectrum_1.Text, null, addButtonLabel)))); return addButtonLabelType === 'tooltip' ? (react_1.default.createElement(SpectrumProvider_1.default, null, react_1.default.createElement(react_spectrum_1.TooltipTrigger, { delay: 0 }, button, react_1.default.createElement(react_spectrum_1.Tooltip, null, addButtonLabel)))) : (button); } exports.AddButton = AddButton; function ArrayHeader(props) { return (react_1.default.createElement(react_spectrum_1.Header, null, react_1.default.createElement(react_spectrum_1.Flex, { direction: 'row', alignItems: 'center', justifyContent: 'space-between' }, react_1.default.createElement(react_spectrum_1.Heading, { level: 4 }, props.labelText), react_1.default.createElement(react_spectrum_1.View, { marginEnd: 'auto' }, props.allErrorsMessages.length ? (react_1.default.createElement(ErrorIndicator_1.ErrorIndicator, { errors: props.allErrorsMessages.map(function (message, index) { return (react_1.default.createElement(react_1.default.Fragment, { key: message + "-" + index }, message, react_1.default.createElement("br", null), index === props.allErrorsMessages.length - 1 ? null : react_1.default.createElement("br", null))); }) })) : null), props.addButtonPosition === 'top' && (react_1.default.createElement(AddButton, tslib_1.__assign({}, props, { onPress: props.add })))))); } exports.ArrayHeader = ArrayHeader; function ArrayFooter(props) { return props.addButtonPosition === 'bottom' ? (react_1.default.createElement(react_spectrum_1.View, { paddingTop: 'size-125' }, react_1.default.createElement(AddButton, tslib_1.__assign({}, props, { onPress: props.add })))) : null; } exports.ArrayFooter = ArrayFooter; function getChildError(e, path) { var childPropErrors = e.filter(function (localError) { return localError.dataPath === path; }); if (childPropErrors.length > 0) { // TODO: is it possible to have multiple errors on a property? return childPropErrors[0].message; } else { return ''; } } exports.getChildError = getChildError; exports.indexOfFittingSchemaObject = {}; exports.UPDATE_DATA = 'jsonforms/UPDATE'; var update = function (path, updater) { return ({ type: exports.UPDATE_DATA, path: path, updater: updater, }); }; /** * Maps state to dispatch properties of an array control. * * @param dispatch the store's dispatch method * @returns {DispatchPropsOfArrayControl} dispatch props of an array control */ exports.mapDispatchToArrayControlProps = function (dispatch) { return ({ /* move: (path: any, from: number, to: number) => () => { dispatch( update(path, (array) => { move(array, from, to); return array; }) ); }, */ updateArray: function (path, data) { return function () { dispatch(update(path, function () { return exports.updateArray(data); })); }; }, }); }; exports.updateArray = function (data) { console.log('UPDATE ARRAY'); return data; }; var move = function (array, index, delta) { var newIndex = index + delta; if (newIndex < 0 || newIndex >= array.length) { return; } // Already at the top or bottom. if (newIndex > index) { var indexes = [index, newIndex].sort(function (a, b) { return a - b; }); // Sort the indixes array.splice(indexes[1], 1, array[indexes[0]], array[indexes[1]]); } else { var indexes = [index, newIndex].sort(function (a, b) { return a - b; }); // Sort the indixes array.splice(indexes[0], 1, array[indexes[1]], array[indexes[0]]); } }; exports.moveFromTo = function (data, from, to) { var delta = to - from; if (delta === 0) { return; // If nothing changed, do nothing } else { move(data, from, delta); } }; function swap(array, moveIndex, toIndex) { /* #move - Moves an array item from one position in an array to another. Note: This is a pure function so a new array will be returned, instead of altering the array argument. Arguments: 1. array (String) : Array in which to move an item. (required) 2. moveIndex (Object) : The index of the item to move. (required) 3. toIndex (Object) : The index to move item at moveIndex to. (required) */ var item = array[moveIndex]; var length = array.length; var diff = moveIndex - toIndex; if (diff > 0) { // move left return tslib_1.__spreadArrays(array.slice(0, toIndex), [ item ], array.slice(toIndex, moveIndex), array.slice(moveIndex + 1, length)); } else if (diff < 0) { // move right var targetIndex = toIndex + 1; return tslib_1.__spreadArrays(array.slice(0, moveIndex), array.slice(moveIndex + 1, targetIndex), [ item ], array.slice(targetIndex, length)); } return array; } exports.swap = swap; /** * Clamps `number` within the inclusive `lower` and `upper` bounds. * * @since 4.0.0 * @category Number * @param {number} number The number to clamp. * @param {number} lower The lower bound. * @param {number} upper The upper bound. * @returns {number} Returns the clamped number. * @example * * clamp(-10, -5, 5) * // => -5 * * clamp(10, -5, 5) * // => 5 */ function clamp(number, lower, upper) { number = +number; lower = +lower; upper = +upper; lower = lower === lower ? lower : 0; upper = upper === upper ? upper : 0; if (number === number) { number = number <= upper ? number : upper; number = number >= lower ? number : lower; } return number; } exports.clamp = clamp; //# sourceMappingURL=utils.js.map