UNPKG

2.85 kBJavaScriptView Raw
1/*
2 * Copyright 2021 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import * as React from "react";
17import { Button } from "@blueprintjs/core";
18import { MenuItem2 } from "@blueprintjs/popover2";
19import { Select2 } from "../components/select/select2";
20import { areFilmsEqual, createFilm, filterFilm, getFilmItemProps, maybeAddCreatedFilmToArrays, maybeDeleteCreatedFilmFromArrays, renderCreateFilmMenuItem, TOP_100_FILMS, } from "./films";
21export function FilmSelect({ allowCreate = false, fill, ...restProps }) {
22 const [items, setItems] = React.useState([...TOP_100_FILMS]);
23 const [createdItems, setCreatedItems] = React.useState([]);
24 const [selectedFilm, setSelectedFilm] = React.useState(TOP_100_FILMS[0]);
25 const handleItemSelect = React.useCallback((newFilm) => {
26 // Delete the old film from the list if it was newly created.
27 const step1Result = maybeDeleteCreatedFilmFromArrays(items, createdItems, selectedFilm);
28 // Add the new film to the list if it is newly created.
29 const step2Result = maybeAddCreatedFilmToArrays(step1Result.items, step1Result.createdItems, newFilm);
30 setCreatedItems(step2Result.createdItems);
31 setSelectedFilm(newFilm);
32 setItems(step2Result.items);
33 }, []);
34 const itemRenderer = React.useCallback((film, props) => {
35 if (!props.modifiers.matchesPredicate) {
36 return null;
37 }
38 return React.createElement(MenuItem2, { ...getFilmItemProps(film, props), selected: film === selectedFilm });
39 }, [selectedFilm]);
40 return (React.createElement(Select2, { createNewItemFromQuery: allowCreate ? createFilm : undefined, createNewItemRenderer: allowCreate ? renderCreateFilmMenuItem : undefined, fill: fill, itemPredicate: filterFilm, itemRenderer: itemRenderer, items: items, itemsEqual: areFilmsEqual, menuProps: { "aria-label": "films" }, noResults: React.createElement(MenuItem2, { disabled: true, text: "No results.", roleStructure: "listoption" }), onItemSelect: handleItemSelect, ...restProps },
41 React.createElement(Button, { disabled: restProps.disabled, fill: fill, icon: "film", rightIcon: "caret-down", text: selectedFilm ? `${selectedFilm.title} (${selectedFilm.year})` : "(No selection)" })));
42}
43//# sourceMappingURL=filmSelect.js.map
\No newline at end of file