UNPKG

aws-northstar

Version:
82 lines (78 loc) 4.93 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** ******************************************************************************************************************* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. * ******************************************************************************************************************** */ const react_1 = __importStar(require("react")); const use_field_api_1 = __importDefault(require("@data-driven-forms/react-form-renderer/use-field-api")); const Table_1 = __importDefault(require("../Table")); const FormField_1 = __importDefault(require("../FormField")); const useUniqueId_1 = __importDefault(require("../../hooks/useUniqueId")); const getErrorText_1 = require("../FormRenderer/utils/getErrorText"); const isEqual_1 = __importDefault(require("../../utils/isEqual")); const TableMapping = (props) => { const _a = (0, use_field_api_1.default)(props), { label, description, items = [], columnDefinitions = [], isDisabled, isReadOnly, placeholder, input: { onChange, name, value }, validateOnMount, showError, getRowId, stretch, meta: { error, submitFailed }, sortBy: defaultSortBy } = _a, rest = __rest(_a, ["label", "description", "items", "columnDefinitions", "isDisabled", "isReadOnly", "placeholder", "input", "validateOnMount", "showError", "getRowId", "stretch", "meta", "sortBy"]); const controlId = (0, useUniqueId_1.default)(name); const errorText = (0, getErrorText_1.getErrorText)(validateOnMount, submitFailed, showError, error); const selectedRowIds = (0, react_1.useMemo)(() => { return (value && getRowId && value.map(getRowId)) || []; }, [value, getRowId]); const handleSelectedRowIdsChange = (0, react_1.useCallback)((selectedIds) => { if (!(0, isEqual_1.default)(selectedIds, selectedRowIds)) { const selectedItems = items.filter((i) => { const id = getRowId === null || getRowId === void 0 ? void 0 : getRowId(i); return id && selectedIds.includes(id); }); onChange(selectedItems); } }, [onChange, items, getRowId, selectedRowIds]); return (react_1.default.createElement(FormField_1.default, { controlId: controlId, errorText: errorText, stretch: stretch }, react_1.default.createElement(Table_1.default, Object.assign({ tableTitle: label, tableDescription: description, items: items, selectedRowIds: selectedRowIds, columnDefinitions: columnDefinitions, onSelectedRowIdsChange: handleSelectedRowIdsChange, getRowId: getRowId }, rest)))); }; exports.default = (0, react_1.memo)(TableMapping);