Skip to content
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

Allow ReactElements to be used in Combobox dropdowns #2474

Merged
merged 36 commits into from
Oct 7, 2024

Conversation

charliepark
Copy link
Contributor

@charliepark charliepark commented Sep 25, 2024

For #1837, we had a few more places that needed to get switched from a Listbox to a Combobox. Interestingly, all of our existing Comboboxes had had the same string used for both the label and the value, so there were a few places where the Combobox was using the label where it would more accurately use the value. As these were identical before, it didn't cause any problems. With this update, though, we'll begin using elements that have a different value from their label, so it's more important that we use the value.

One example of this is in the image selector, where we now have three elements — the value, the label, and an optional selectedLabel — a custom string for the ComboboxItem.
Screenshot 2024-09-25 at 4 43 53 PM
Here we see the selectedLabel in the input field, where the label for each item is the richer HTML in the dropdown.

Closes #1837
Closes #2483

Copy link

vercel bot commented Sep 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
console ✅ Ready (Inspect) Visit Preview Oct 3, 2024 10:47pm

@charliepark charliepark changed the title Enable ReactElements in Combobox dropdowns Allow ReactElements to be used in Combobox dropdowns Sep 25, 2024
@david-crespo
Copy link
Collaborator

The behavior where it automatically focuses the image field on error is broken — In theory, ComboboxField needs the same fix ListboxField got in #2364. However, I am trying it and it's not working yet.

diff --git a/app/ui/lib/Combobox.tsx b/app/ui/lib/Combobox.tsx
index 946cd236..0dbfa2d9 100644
--- a/app/ui/lib/Combobox.tsx
+++ b/app/ui/lib/Combobox.tsx
@@ -15,7 +15,7 @@ import {
 } from '@headlessui/react'
 import cn from 'classnames'
 import { matchSorter } from 'match-sorter'
-import { useId, useState, type ReactNode } from 'react'
+import { useId, useState, type ReactNode, type Ref } from 'react'
 
 import { SelectArrows6Icon } from '@oxide/design-system/icons/react'
 
@@ -69,6 +69,8 @@ type ComboboxProps = {
   selectedItemLabel: string
   hasError?: boolean
   onChange: (value: string) => void
+  /** Necessary if you want RHF to be able to focus it on error */
+  inputRef?: Ref<HTMLInputElement>
 } & ComboboxBaseProps
 
 export const Combobox = ({
@@ -86,6 +88,7 @@ export const Combobox = ({
   onInputChange,
   allowArbitraryValues = false,
   hideOptionalTag,
+  inputRef,
   ...props
 }: ComboboxProps) => {
   const [query, setQuery] = useState(selectedItemValue || '')
@@ -147,6 +150,7 @@ export const Combobox = ({
                 : 'bg-default',
               hasError && 'focus-error'
             )}
+            ref={inputRef}
           />
           {items.length > 0 && (
             <ComboboxButton

@david-crespo
Copy link
Collaborator

Ok, it's working now. Weird.

@charliepark
Copy link
Contributor Author

This also closes #2483

@charliepark
Copy link
Contributor Author

This morning, talked through an edge case we'd like to polish up in a future release, but don't want to hold up merging this with the current release. I'll file a ticket, but the summary is that when there's a validation error, we can't open up the popup (by giving the input focus) without also obscuring the error message that's highlighting why it's in an error state.

Ben had a few ideas that we'll explore a bit in a subsequent branch.

Copy link
Collaborator

@david-crespo david-crespo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woohoo!

@charliepark charliepark merged commit 1733b76 into main Oct 7, 2024
8 checks passed
@charliepark charliepark deleted the allow-react-elements-in-combobox-dropdowns branch October 7, 2024 19:59
david-crespo added a commit to oxidecomputer/omicron that referenced this pull request Oct 7, 2024
david-crespo added a commit to oxidecomputer/omicron that referenced this pull request Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants