-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib: Support headers with TypeaheadSelect #21183
Merged
mvollmer
merged 4 commits into
cockpit-project:main
from
mvollmer:lib-typeahead-select-groups
Nov 26, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a2536a3
playground: Enable dark mode for React Patterns
mvollmer 871efa5
lib: Translate one more UI string in TypeaheadSelect
mvollmer 4eff7b6
lib: Ignore dividers when key navigating in TypeaheadSelect
mvollmer 68ac285
lib: Support headers with TypeaheadSelect
mvollmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.ct-typeahead-header .pf-v5-c-menu__item-main { | ||
color: var(--pf-v5-c-menu__group-title--Color); | ||
font-size: var(--pf-v5-c-menu__group-title--FontSize); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* This file is part of Cockpit. | ||
* | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Cockpit is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation; either version 2.1 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Cockpit is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Cockpit; If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import cockpit from "cockpit"; | ||
|
||
import React, { useState } from "react"; | ||
import { createRoot, Container } from 'react-dom/client'; | ||
|
||
import { Checkbox } from '@patternfly/react-core'; | ||
import { TypeaheadSelect, TypeaheadSelectOption } from "cockpit-components-typeahead-select"; | ||
|
||
const TypeaheadDemo = ({ options } : { options: TypeaheadSelectOption[] }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm really amazed that you could fully type this without too much effort. Makes the "type ahead demo" twice as interesting! 😉 |
||
const [isCreatable, setIsCreatable] = useState(false); | ||
const [notFoundIsString, setNotFoundIsString] = useState(false); | ||
const [value, setValue] = useState<string | number | null>(); | ||
const [toggles, setToggles] = useState(0); | ||
const [changes, setChanges] = useState(0); | ||
|
||
return ( | ||
<div> | ||
<TypeaheadSelect | ||
id='typeahead-widget' | ||
placeholder="Select a state" | ||
isScrollable | ||
noOptionsFoundMessage={notFoundIsString ? "Not found" : val => cockpit.format("'$0' not found", val) } | ||
isCreatable={isCreatable} | ||
createOptionMessage={val => cockpit.format("Create $0", val)} | ||
onClearSelection={() => setValue(null)} | ||
selectOptions={options} | ||
selected={value} | ||
onSelect={(_, value) => setValue(value) } | ||
onToggle={() => setToggles(val => val + 1)} | ||
onInputChange={() => setChanges(val => val + 1)} | ||
/> | ||
<div>Selected: <span id="value">{value || "-"}</span></div> | ||
<div>Toggles: <span id="toggles">{toggles}</span></div> | ||
<div>Changes: <span id="changes">{changes}</span></div> | ||
<Checkbox | ||
id="isCreatable" | ||
label="isCreatable" | ||
isChecked={isCreatable} | ||
onChange={(_event, checked) => setIsCreatable(checked)} | ||
/> | ||
<Checkbox | ||
id="notFoundIsString" | ||
label="notFoundIsString" | ||
isChecked={notFoundIsString} | ||
onChange={(_event, checked) => setNotFoundIsString(checked)} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export function showTypeaheadDemo(rootElement: Container) { | ||
const states: Record<string, string> = { | ||
AL: "Alabama", | ||
AK: "Alaska", | ||
AZ: "Arizona", | ||
AR: "Arkansas", | ||
CA: "California", | ||
CO: "Colorado", | ||
CT: "Connecticut", | ||
DE: "Delaware", | ||
FL: "Florida", | ||
GA: "Georgia", | ||
HI: "Hawaii", | ||
ID: "Idaho", | ||
IL: "Illinois", | ||
IN: "Indiana", | ||
IA: "Iowa", | ||
KS: "Kansas", | ||
KY: "Kentucky", | ||
LA: "Louisiana", | ||
ME: "Maine", | ||
MD: "Maryland", | ||
MA: "Massachusetts", | ||
MI: "Michigan", | ||
MN: "Minnesota", | ||
MS: "Mississippi", | ||
MO: "Missouri", | ||
MT: "Montana", | ||
NE: "Nebraska", | ||
NV: "Nevada", | ||
NH: "New Hampshire", | ||
NJ: "New Jersey", | ||
NM: "New Mexico", | ||
NY: "New York", | ||
NC: "North Carolina", | ||
ND: "North Dakota", | ||
OH: "Ohio", | ||
OK: "Oklahoma", | ||
OR: "Oregon", | ||
PA: "Pennsylvania", | ||
RI: "Rhode Island", | ||
SC: "South Carolina", | ||
SD: "South Dakota", | ||
TN: "Tennessee", | ||
TX: "Texas", | ||
UT: "Utah", | ||
VT: "Vermont", | ||
VA: "Virginia", | ||
WA: "Washington", | ||
WV: "West Virginia", | ||
WI: "Wisconsin", | ||
WY: "Wyoming", | ||
}; | ||
|
||
const options: TypeaheadSelectOption[] = []; | ||
let last = ""; | ||
|
||
options.push({ value: "start", content: "The Start" }); | ||
|
||
for (const st of Object.keys(states).sort()) { | ||
if (st[0] != last) { | ||
options.push({ decorator: "divider", key: "_divider-" + st }); | ||
options.push({ | ||
decorator: "header", | ||
key: "_header-" + st, | ||
content: "Starting with " + st[0] | ||
}); | ||
last = st[0]; | ||
} | ||
options.push({ value: st, content: states[st] }); | ||
} | ||
|
||
options.push({ decorator: "divider", key: "_divider-end" }); | ||
options.push({ value: "end", content: "The End" }); | ||
|
||
const root = createRoot(rootElement); | ||
root.render(<TypeaheadDemo options={options} />); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really cool!