| 1 |
/* |
| 2 |
* Copyright (C) 2024 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
|
| 8 |
import React, { useState } from "react"; |
| 9 |
|
| 10 |
import { Checkbox } from "@patternfly/react-core/dist/esm/components/Checkbox/index.js"; |
| 11 |
import { MultiTypeaheadSelect, MultiTypeaheadSelectOption } from "cockpit-components-multi-typeahead-select"; |
| 12 |
|
| 13 |
const MultiTypeahead = ({ options } : { options: MultiTypeaheadSelectOption[] }) => { |
| 14 |
const [notFoundIsString, setNotFoundIsString] = useState(false); |
| 15 |
const [selected, setSelected] = useState<(string | number)[]>([]); |
| 16 |
const [toggles, setToggles] = useState(0); |
| 17 |
const [changes, setChanges] = useState(0); |
| 18 |
|
| 19 |
function add(val: string | number) { |
| 20 |
setSelected(selected.concat([val])); |
| 21 |
} |
| 22 |
|
| 23 |
function rem(val: string | number) { |
| 24 |
setSelected(selected.filter(v => v != val)); |
| 25 |
} |
| 26 |
|
| 27 |
return ( |
| 28 |
<div> |
| 29 |
<MultiTypeaheadSelect |
| 30 |
id='multi-typeahead-widget' |
| 31 |
placeholder="Select flavors" |
| 32 |
isScrollable |
| 33 |
noOptionsFoundMessage={notFoundIsString ? "Not found" : val => cockpit.format("'$0' not found", val) } |
| 34 |
options={options} |
| 35 |
selected={selected} |
| 36 |
onAdd={add} |
| 37 |
onRemove={rem} |
| 38 |
onToggle={() => setToggles(val => val + 1)} |
| 39 |
onInputChange={() => setChanges(val => val + 1)} |
| 40 |
/> |
| 41 |
<div>Selected: <span id="multi-value">{JSON.stringify(selected)}</span></div> |
| 42 |
<div>Toggles: <span id="multi-toggles">{toggles}</span></div> |
| 43 |
<div>Changes: <span id="multi-changes">{changes}</span></div> |
| 44 |
<Checkbox |
| 45 |
id="notFoundIsStringMulti" |
| 46 |
label="notFoundIsString" |
| 47 |
isChecked={notFoundIsString} |
| 48 |
onChange={(_event, checked) => setNotFoundIsString(checked)} |
| 49 |
/> |
| 50 |
</div> |
| 51 |
); |
| 52 |
}; |
| 53 |
|
| 54 |
export const MultiTypeaheadDemo = () => { |
| 55 |
const flavors: string[] = [ |
| 56 |
"Alumni Swirl", |
| 57 |
"Apple Cobbler Crunch", |
| 58 |
"Arboretum Breeze", |
| 59 |
"August Pie", |
| 60 |
"Autumn Delight", |
| 61 |
"Bavarian Raspberry Crunch", |
| 62 |
"Berkey Brickle", |
| 63 |
"Birthday Bash", |
| 64 |
"Bittersweet Mint", |
| 65 |
"Black Cow", |
| 66 |
"Black Raspberry", |
| 67 |
"Blueberry Cheesecake", |
| 68 |
"Butter Pecan", |
| 69 |
"Candy Bar/Snickers", |
| 70 |
"Caramel Critters", |
| 71 |
"Centennial Vanilla Bean", |
| 72 |
"Cherry Cheesecake", |
| 73 |
"Cherry Chip", |
| 74 |
"Cherry Quist", |
| 75 |
"Cherry Sherbet", |
| 76 |
"Chocolate", |
| 77 |
"Chocolate Cherry Cordia", |
| 78 |
"Chocolate Chip", |
| 79 |
"Chocolate Chip Cheesecake", |
| 80 |
"Chocolate Chip Cookie Dough", |
| 81 |
"Chocolate Chocolate Nut", |
| 82 |
"Chocolate Marble", |
| 83 |
"Chocolate Marshmallow", |
| 84 |
"Chocolate Pretzel Crunch", |
| 85 |
"Chunky Chocolate", |
| 86 |
"Chunky Chocolate- Vanilla", |
| 87 |
"Coconut Chip", |
| 88 |
"Coffee Mocha Fudge", |
| 89 |
"Coffee w/Cream and Sugar", |
| 90 |
"Crazy Charlie Sundae Swirl", |
| 91 |
"Death By Chocolate", |
| 92 |
"Egg Nog", |
| 93 |
"Espresso Fudge Pie", |
| 94 |
"German Chocolate Cake", |
| 95 |
"Golden Chocolate Pecan", |
| 96 |
"Goo Goo Cluster", |
| 97 |
"Grape Sherbet", |
| 98 |
"Happy Happy Joy Joy", |
| 99 |
"Heath Bar Candy", |
| 100 |
"Just Fudge", |
| 101 |
"Kenney Beany Chocolate", |
| 102 |
"Lion Tracks", |
| 103 |
"LionS'more", |
| 104 |
"Mallo Cup", |
| 105 |
"Maple Nut", |
| 106 |
"Mint Nittany", |
| 107 |
"Monster Mash", |
| 108 |
"Orange Vanilla Sundae", |
| 109 |
"Palmer Mousseum With Almonds", |
| 110 |
"Peachy Paterno", |
| 111 |
"Peanut Butter Cup", |
| 112 |
"Peanut Butter Fudge Cluster", |
| 113 |
"Peanut Butter Marshmallow", |
| 114 |
"Peanut Butter Swirl", |
| 115 |
"Pecan Apple Danish", |
| 116 |
"Peppermint Stick", |
| 117 |
"Pistachio", |
| 118 |
"Pralines N Cream", |
| 119 |
"Pumpkin Pie", |
| 120 |
"Raspberry Fudge Torte", |
| 121 |
"Raspberry Parfait", |
| 122 |
"Rum Raisin", |
| 123 |
"Russ 'Digs' Roseberry", |
| 124 |
"Santa Fe Banana", |
| 125 |
"Scholar's Chip", |
| 126 |
"Sea Salt Chocolate Caramel", |
| 127 |
"Somerset Shortcake", |
| 128 |
"Southern Chocolate Pie", |
| 129 |
"Southern Pecan Cheesecake", |
| 130 |
"Strawberry", |
| 131 |
"Strawberry Cheesecake", |
| 132 |
"Teaberry", |
| 133 |
"Tin Roof Sundae", |
| 134 |
"Toasted Almond", |
| 135 |
"Toasted Almond Fudge", |
| 136 |
"Turtle Creek", |
| 137 |
"Vanilla", |
| 138 |
"White House", |
| 139 |
"Wicked Caramel Sundae", |
| 140 |
"WPSU Coffee Break", |
| 141 |
]; |
| 142 |
|
| 143 |
const options: MultiTypeaheadSelectOption[] = flavors.map((f, i) => ({ value: i + 1, content: f })); |
| 144 |
|
| 145 |
return <MultiTypeahead options={options} />; |
| 146 |
}; |