UNPKG

test-jsonforms-react-spectrum-renderers

Version:

React Spectrum Renderer Set for JSONForms

222 lines (216 loc) 15.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); /* The MIT License Copyright (c) 2017-2019 EclipseSource Munich https://github.com/eclipsesource/jsonforms Copyright (c) 2022 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.__importStar(require("react")); var react_spectrum_1 = require("@adobe/react-spectrum"); var core_1 = require("@jsonforms/core"); var react_2 = require("@jsonforms/react"); var Delete_1 = tslib_1.__importDefault(require("@spectrum-icons/workflow/Delete")); var Edit_1 = tslib_1.__importDefault(require("@spectrum-icons/workflow/Edit")); var SaveFloppy_1 = tslib_1.__importDefault(require("@spectrum-icons/workflow/SaveFloppy")); var SaveAsFloppy_1 = tslib_1.__importDefault(require("@spectrum-icons/workflow/SaveAsFloppy")); var ChevronUp_1 = tslib_1.__importDefault(require("@spectrum-icons/workflow/ChevronUp")); var ChevronDown_1 = tslib_1.__importDefault(require("@spectrum-icons/workflow/ChevronDown")); var react_spring_1 = require("react-spring"); require("./SpectrumArrayModalItem.css"); var SpectrumProvider_1 = tslib_1.__importDefault(require("../../additional/SpectrumProvider")); var utils_1 = require("./utils"); var SpectrumArrayModalItem = react_1.default.memo(function (_a) { var childLabel = _a.childLabel, childData = _a.childData, expanded = _a.expanded, handleExpand = _a.handleExpand, index = _a.index, indexOfFittingSchema = _a.indexOfFittingSchema, path = _a.path, removeItem = _a.removeItem, renderers = _a.renderers, schema = _a.schema, uischema = _a.uischema, uischemas = _a.uischemas; var _b; var foundUISchema = core_1.findUISchema(uischemas, schema, uischema.scope, path); var childPath = core_1.composePaths(path, "" + index); var _c = react_1.useState(false), open = _c[0], setOpen = _c[1]; var handleClose = react_1.useCallback(function () { return setOpen(false); }, [setOpen]); var findValue = function (obj, key) { if (obj[key]) { return obj[key]; } for (var prop in obj) { if (obj.hasOwnProperty(prop)) { if (typeof obj[prop] === 'object') { var result = findValue(obj[prop], key); if (result) { return result; } } } } }; react_1.useEffect(function () { var _a, _b, _c; utils_1.indexOfFittingSchemaObject[childPath] = (_a = indexOfFittingSchema !== null && indexOfFittingSchema !== void 0 ? indexOfFittingSchema : findValue(childData, 'indexOfFittingSchema')) !== null && _a !== void 0 ? _a : 0; /* let fittingSchema = null; schema.map((item,index) => item.componentType.title === childData.componentType ? fittingSchema = index : null); */ if ((_b = uischema.options) === null || _b === void 0 ? void 0 : _b.OneOfModal) { utils_1.indexOfFittingSchemaObject['OneOfModal'] = true; } if ((_c = uischema.options) === null || _c === void 0 ? void 0 : _c.OneOfPicker) { utils_1.indexOfFittingSchemaObject['OneOfPicker'] = true; } }, []); // WIP LOOK INTO CHANNEL // window.addEventListener('message', (event) => { // if (event.data.type !== 'expanded-item') {return;} // prettier-ignore // console.log(event.data); // }); // let callOnClose = false; // useEffect(() => { // console.log('\x1b[31m~ useEffect EXPANDED was called... index:', index); // if (expanded || callOnClose) { // window.postMessage({ type: 'expanded-item', index, path, label: childLabel }, '*'); // prettier-ignore // callOnClose = expanded ? true : false; // } // }, [expanded]); var enableDetailedView = (_b = uischema === null || uischema === void 0 ? void 0 : uischema.options) === null || _b === void 0 ? void 0 : _b.enableDetailedView; var refForPosition = react_1.useRef(); var _d = react_1.useState('66px'), baseHeight = _d[0], setBaseHeight = _d[1]; var _e = react_1.useState('100%'), baseWidth = _e[0], setBaseWidth = _e[1]; var _f = react_1.useState(0), baseTop = _f[0], setBaseTop = _f[1]; var _g = react_1.useState(0), baseLeft = _g[0], setBaseLeft = _g[1]; react_1.useEffect(function () { if (refForPosition.current) { setBaseHeight(refForPosition.current.clientHeight + 'px'); setBaseWidth(refForPosition.current.clientWidth + 'px'); setBaseTop(refForPosition.current.offsetTop); setBaseLeft(refForPosition.current.offsetLeft); } }, []); var _h = react_spring_1.useSpring({ top: expanded ? 0 : baseTop, left: expanded ? 0 : baseLeft, height: expanded ? window.innerHeight + 'px' : baseHeight, width: expanded ? window.innerWidth + 'px' : baseWidth, // I need to use some other value, because position is not animatable opacity: expanded ? 1 : 0, }), top = _h.top, left = _h.left, height = _h.height, width = _h.width, opacity = _h.opacity; var ConditionalAnimationWrapper = function (_a) { var condition = _a.condition, children = _a.children; return condition ? (react_1.default.createElement(react_spring_1.animated.div, { style: { top: top, left: left, height: height, width: width, position: opacity.to(function (e) { return e > 0 ? 'absolute' : 'static'; }), } }, children)) : (children); }; return (react_1.default.createElement(SpectrumProvider_1.default, { flex: 'auto', width: '100%' }, react_1.default.createElement(ConditionalAnimationWrapper, { condition: enableDetailedView }, react_1.default.createElement("div", { ref: refForPosition }, react_1.default.createElement(react_spectrum_1.View, { UNSAFE_className: "list-array-item " + (enableDetailedView ? 'enableDetailedView' : '') + " " + (expanded ? 'expanded' : 'collapsed'), borderWidth: 'thin', borderColor: 'dark', borderRadius: 'medium', padding: 'size-150' }, react_1.default.createElement(react_spectrum_1.View, { "aria-selected": expanded, UNSAFE_className: 'array-item-header' }, react_1.default.createElement(react_spectrum_1.Flex, { direction: 'row', margin: 'size-50', justifyContent: 'space-between', alignItems: 'center' }, react_1.default.createElement(react_spectrum_1.View, { UNSAFE_className: 'spectrum-array-item-number' }, react_1.default.createElement(react_spectrum_1.Text, null, index + 1)), react_1.default.createElement(react_spectrum_1.ActionButton, { flex: 'auto', isQuiet: true, onPress: handleExpand(index), "aria-label": "expand-item-" + childLabel }, react_1.default.createElement(react_spectrum_1.Text, { UNSAFE_className: 'spectrum-array-item-name', UNSAFE_style: { textAlign: 'left' } }, childLabel)), react_1.default.createElement(react_spectrum_1.View, null, react_1.default.createElement(react_spectrum_1.Flex, { gap: enableDetailedView ? 'size-300' : '' }, enableDetailedView && expanded && (react_1.default.createElement(react_spectrum_1.TooltipTrigger, { delay: 0 }, react_1.default.createElement(react_spectrum_1.ActionButton, { onPress: function () { return console.log('Pressed "Save & continue editing"'); }, "aria-label": "save-and-continue-editing-" + childLabel }, react_1.default.createElement(SaveAsFloppy_1.default, null)), react_1.default.createElement(react_spectrum_1.Tooltip, null, "Save & continue editing"))), react_1.default.createElement(react_spectrum_1.TooltipTrigger, { delay: 0 }, react_1.default.createElement(react_spectrum_1.ActionButton, { onPress: handleExpand(index), isQuiet: !enableDetailedView, "aria-label": "expand-item-" + childLabel }, expanded ? (enableDetailedView ? react_1.default.createElement(SaveFloppy_1.default, { "aria-label": 'Save & Close' }) : react_1.default.createElement(ChevronUp_1.default, { "aria-label": 'Collapse' }) //prettier-ignore ) : //prettier-ignore enableDetailedView ? react_1.default.createElement(Edit_1.default, { "aria-label": 'Edit' }) : react_1.default.createElement(ChevronDown_1.default, { "aria-label": 'Expand' }) //prettier-ignore ), react_1.default.createElement(react_spectrum_1.Tooltip, null, expanded ? enableDetailedView ? 'Save & Close' : 'Collapse' : enableDetailedView ? 'Edit' : 'Expand')), react_1.default.createElement(react_spectrum_1.TooltipTrigger, { delay: 0 }, react_1.default.createElement(react_spectrum_1.ActionButton, { onPress: function () { return setOpen(true); }, "aria-label": "delete-item-" + childLabel, isQuiet: enableDetailedView }, react_1.default.createElement(Delete_1.default, { "aria-label": 'Delete' })), react_1.default.createElement(react_spectrum_1.Tooltip, null, "Delete")), react_1.default.createElement(react_spectrum_1.DialogContainer, { onDismiss: handleClose }, open && (react_1.default.createElement(react_spectrum_1.Dialog, null, react_1.default.createElement(react_spectrum_1.Heading, null, "Delete Item?"), react_1.default.createElement(react_spectrum_1.Divider, null), react_1.default.createElement(react_spectrum_1.Content, null, "Are you sure you wish to delete this item?"), react_1.default.createElement(react_spectrum_1.ButtonGroup, null, react_1.default.createElement(react_spectrum_1.Button, { variant: 'secondary', onPress: handleClose }, "Cancel"), react_1.default.createElement(react_spectrum_1.Button, { autoFocus: true, variant: 'cta', onPressStart: removeItem(path, index), onPressEnd: handleClose }, "Delete"))))))))), expanded ? (react_1.default.createElement(react_spectrum_1.View, null, react_1.default.createElement(react_2.ResolvedJsonFormsDispatch, { key: childPath, path: childPath, renderers: renderers, schema: schema, uischema: foundUISchema || uischema }))) : ('')))))); }); /** * Map state to control props.No indexOfFittingSchema found * @param state the store's state * @param ownProps any own props * @returns {StatePropsOfControl} state props for a control */ exports.mapStateToSpectrumArrayModalItemProps = function (state, ownProps) { var _a, _b, _c, _d, _e, _f, _g; var schema = ownProps.schema, path = ownProps.path, index = ownProps.index, uischema = ownProps.uischema; var firstPrimitiveProp = schema.properties ? Object.keys(schema.properties).find(function (propName) { var prop = schema.properties[propName]; return (prop.type === 'string' || prop.type === 'number' || prop.type === 'integer'); }) : undefined; var childPath = core_1.composePaths(path, "" + index); var childData = core_1.Resolve.data(core_1.getData(state), childPath); var findValue = function (obj, key) { if (obj[key]) { return obj[key]; } for (var prop in obj) { if (obj.hasOwnProperty(prop)) { if (typeof obj[prop] === 'object') { var result = findValue(obj[prop], key); if (result) { return result; } } } } }; var childLabel = ((_c = (_b = (_a = uischema.options) === null || _a === void 0 ? void 0 : _a.elementLabelProp) !== null && _b !== void 0 ? _b : firstPrimitiveProp) !== null && _c !== void 0 ? _c : typeof ((_d = uischema.options) === null || _d === void 0 ? void 0 : _d.DataAsLabel) === 'number') ? Object.values(childData)[(_e = uischema.options) === null || _e === void 0 ? void 0 : _e.DataAsLabel] : (_g = findValue(childData, (_f = uischema.options) === null || _f === void 0 ? void 0 : _f.DataAsLabel)) !== null && _g !== void 0 ? _g : "Item " + (index + 1); return tslib_1.__assign(tslib_1.__assign({}, ownProps), { childLabel: childLabel, childData: childData }); }; exports.ctxToSpectrumArrayModalItemProps = function (ctx, ownProps) { return exports.mapStateToSpectrumArrayModalItemProps({ jsonforms: tslib_1.__assign({}, ctx) }, ownProps); }; var withContextToSpectrumArrayModalItemProps = function (Component) { return function (_a) { var ctx = _a.ctx, props = _a.props; var stateProps = exports.ctxToSpectrumArrayModalItemProps(ctx, props); return react_1.default.createElement(Component, tslib_1.__assign({}, stateProps)); }; }; exports.withJsonFormsSpectrumArrayModalItemProps = function (Component) { return react_2.withJsonFormsContext(withContextToSpectrumArrayModalItemProps(react_1.default.memo(Component, function (prevProps, nextProps) { var prevHandleExpand = prevProps.handleExpand, prevRemoveItem = prevProps.removeItem, restPrevProps = tslib_1.__rest(prevProps, ["handleExpand", "removeItem"]); var nextHandleExpand = nextProps.handleExpand, nextRemoveItem = nextProps.removeItem, restNextProps = tslib_1.__rest(nextProps, ["handleExpand", "removeItem"]); return react_2.areEqual(restPrevProps, restNextProps); }))); }; exports.default = exports.withJsonFormsSpectrumArrayModalItemProps(SpectrumArrayModalItem); //# sourceMappingURL=SpectrumArrayModalItem.js.map