| 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 { TypeaheadSelect, TypeaheadSelectOption } from "cockpit-components-typeahead-select"; |
| 12 |
|
| 13 |
const Typeahead = ({ options } : { options: TypeaheadSelectOption[] }) => { |
| 14 |
const [isCreatable, setIsCreatable] = useState(false); |
| 15 |
const [notFoundIsString, setNotFoundIsString] = useState(false); |
| 16 |
const [value, setValue] = useState<string | number | null>(); |
| 17 |
const [toggles, setToggles] = useState(0); |
| 18 |
const [changes, setChanges] = useState(0); |
| 19 |
|
| 20 |
return ( |
| 21 |
<div> |
| 22 |
<TypeaheadSelect |
| 23 |
id='typeahead-widget' |
| 24 |
placeholder="Select a state" |
| 25 |
isScrollable |
| 26 |
noOptionsFoundMessage={notFoundIsString ? "Not found" : val => cockpit.format("'$0' not found", val) } |
| 27 |
isCreatable={isCreatable} |
| 28 |
createOptionMessage={val => cockpit.format("Create $0", val)} |
| 29 |
onClearSelection={() => setValue(null)} |
| 30 |
selectOptions={options} |
| 31 |
selected={value} |
| 32 |
onSelect={(_, value) => setValue(value) } |
| 33 |
onToggle={() => setToggles(val => val + 1)} |
| 34 |
onInputChange={() => setChanges(val => val + 1)} |
| 35 |
/> |
| 36 |
<div>Selected: <span id="value">{value || "-"}</span></div> |
| 37 |
<div>Toggles: <span id="toggles">{toggles}</span></div> |
| 38 |
<div>Changes: <span id="changes">{changes}</span></div> |
| 39 |
<Checkbox |
| 40 |
id="isCreatable" |
| 41 |
label="isCreatable" |
| 42 |
isChecked={isCreatable} |
| 43 |
onChange={(_event, checked) => setIsCreatable(checked)} |
| 44 |
/> |
| 45 |
<Checkbox |
| 46 |
id="notFoundIsString" |
| 47 |
label="notFoundIsString" |
| 48 |
isChecked={notFoundIsString} |
| 49 |
onChange={(_event, checked) => setNotFoundIsString(checked)} |
| 50 |
/> |
| 51 |
</div> |
| 52 |
); |
| 53 |
}; |
| 54 |
|
| 55 |
export const TypeaheadDemo = () => { |
| 56 |
const states: Record<string, string> = { |
| 57 |
AL: "Alabama", |
| 58 |
AK: "Alaska", |
| 59 |
AZ: "Arizona", |
| 60 |
AR: "Arkansas", |
| 61 |
CA: "California", |
| 62 |
CO: "Colorado", |
| 63 |
CT: "Connecticut", |
| 64 |
DE: "Delaware", |
| 65 |
FL: "Florida", |
| 66 |
GA: "Georgia", |
| 67 |
HI: "Hawaii", |
| 68 |
ID: "Idaho", |
| 69 |
IL: "Illinois", |
| 70 |
IN: "Indiana", |
| 71 |
IA: "Iowa", |
| 72 |
KS: "Kansas", |
| 73 |
KY: "Kentucky", |
| 74 |
LA: "Louisiana", |
| 75 |
ME: "Maine", |
| 76 |
MD: "Maryland", |
| 77 |
MA: "Massachusetts", |
| 78 |
MI: "Michigan", |
| 79 |
MN: "Minnesota", |
| 80 |
MS: "Mississippi", |
| 81 |
MO: "Missouri", |
| 82 |
MT: "Montana", |
| 83 |
NE: "Nebraska", |
| 84 |
NV: "Nevada", |
| 85 |
NH: "New Hampshire", |
| 86 |
NJ: "New Jersey", |
| 87 |
NM: "New Mexico", |
| 88 |
NY: "New York", |
| 89 |
NC: "North Carolina", |
| 90 |
ND: "North Dakota", |
| 91 |
OH: "Ohio", |
| 92 |
OK: "Oklahoma", |
| 93 |
OR: "Oregon", |
| 94 |
PA: "Pennsylvania", |
| 95 |
RI: "Rhode Island", |
| 96 |
SC: "South Carolina", |
| 97 |
SD: "South Dakota", |
| 98 |
TN: "Tennessee", |
| 99 |
TX: "Texas", |
| 100 |
UT: "Utah", |
| 101 |
VT: "Vermont", |
| 102 |
VA: "Virginia", |
| 103 |
WA: "Washington", |
| 104 |
WV: "West Virginia", |
| 105 |
WI: "Wisconsin", |
| 106 |
WY: "Wyoming", |
| 107 |
}; |
| 108 |
|
| 109 |
const options: TypeaheadSelectOption[] = []; |
| 110 |
let last = ""; |
| 111 |
|
| 112 |
options.push({ value: "start", content: "The Start" }); |
| 113 |
|
| 114 |
for (const st of Object.keys(states).sort()) { |
| 115 |
if (st[0] != last) { |
| 116 |
options.push({ decorator: "divider", key: "_divider-" + st }); |
| 117 |
options.push({ |
| 118 |
decorator: "header", |
| 119 |
key: "_header-" + st, |
| 120 |
content: "Starting with " + st[0] |
| 121 |
}); |
| 122 |
last = st[0]; |
| 123 |
} |
| 124 |
options.push({ value: st, content: states[st] }); |
| 125 |
} |
| 126 |
|
| 127 |
options.push({ decorator: "divider", key: "_divider-end" }); |
| 128 |
options.push({ value: "end", content: "The End" }); |
| 129 |
|
| 130 |
return <Typeahead options={options} />; |
| 131 |
}; |