UNPKG

@blueprintjs/select

Version:

Components related to selecting items from a list

56 lines 3.64 kB
"use strict"; /* * Copyright 2021 Palantir Technologies, Inc. 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.FilmSelect = void 0; var tslib_1 = require("tslib"); var classnames_1 = tslib_1.__importDefault(require("classnames")); var React = tslib_1.__importStar(require("react")); var core_1 = require("@blueprintjs/core"); var select_1 = require("../components/select/select"); var films_1 = require("./films"); function FilmSelect(_a) { var _b; var _c; var _d = _a.allowCreate, allowCreate = _d === void 0 ? false : _d, fill = _a.fill, restProps = tslib_1.__rest(_a, ["allowCreate", "fill"]); var _e = React.useState(tslib_1.__spreadArray([], films_1.TOP_100_FILMS, true)), items = _e[0], setItems = _e[1]; var _f = React.useState([]), createdItems = _f[0], setCreatedItems = _f[1]; var _g = React.useState(undefined), selectedFilm = _g[0], setSelectedFilm = _g[1]; var handleItemSelect = React.useCallback(function (newFilm) { // Delete the old film from the list if it was newly created. var step1Result = (0, films_1.maybeDeleteCreatedFilmFromArrays)(items, createdItems, selectedFilm); // Add the new film to the list if it is newly created. var step2Result = (0, films_1.maybeAddCreatedFilmToArrays)(step1Result.items, step1Result.createdItems, newFilm); setCreatedItems(step2Result.createdItems); setSelectedFilm(newFilm); setItems(step2Result.items); }, [createdItems, items, selectedFilm]); var itemRenderer = React.useCallback(function (film, props) { if (!props.modifiers.matchesPredicate) { return null; } return (React.createElement(core_1.MenuItem, tslib_1.__assign({}, (0, films_1.getFilmItemProps)(film, props), { roleStructure: "listoption", selected: film === selectedFilm }))); }, [selectedFilm]); return (React.createElement(select_1.Select, tslib_1.__assign({ createNewItemFromQuery: allowCreate ? films_1.createFilm : undefined, createNewItemRenderer: allowCreate ? films_1.renderCreateFilmMenuItem : undefined, fill: fill, itemPredicate: films_1.filterFilm, itemRenderer: itemRenderer, items: items, itemsEqual: films_1.areFilmsEqual, menuProps: { "aria-label": "films" }, noResults: React.createElement(core_1.MenuItem, { disabled: true, text: "No results.", roleStructure: "listoption" }), onItemSelect: handleItemSelect }, restProps), React.createElement(core_1.Button, { alignText: "left", disabled: restProps.disabled, fill: fill, icon: "film", rightIcon: "caret-down", text: (_c = maybeRenderSelectedFilm(selectedFilm)) !== null && _c !== void 0 ? _c : "(No selection)", textClassName: (0, classnames_1.default)((_b = {}, _b[core_1.Classes.TEXT_MUTED] = selectedFilm === undefined, _b)) }))); } exports.FilmSelect = FilmSelect; function maybeRenderSelectedFilm(selectedFilm) { return selectedFilm ? "".concat(selectedFilm.title, " (").concat(selectedFilm.year, ")") : undefined; } //# sourceMappingURL=filmSelect.js.map