Skip to content

Instantly share code, notes, and snippets.

@j-bullard
Created July 4, 2021 07:15
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save j-bullard/dfa2ec92bff5ff0a498f649c3c4cb432 to your computer and use it in GitHub Desktop.
Save j-bullard/dfa2ec92bff5ff0a498f649c3c4cb432 to your computer and use it in GitHub Desktop.
Headless UI with React Hook Forms
import "./styles.css";
import { Person } from "./types";
import { Listbox } from "./listbox/listbox";
import { useForm } from "react-hook-form";
const people: Person[] = [
{ id: 1, name: "Durward Reynolds", unavailable: false },
{ id: 2, name: "Kenton Towne", unavailable: false },
{ id: 3, name: "Therese Wunsch", unavailable: false },
{ id: 4, name: "Benedict Kessler", unavailable: true },
{ id: 5, name: "Katelyn Rohan", unavailable: false }
];
export default function App() {
const { control } = useForm();
return (
<div className="App">
<Listbox
name="people"
control={control}
rules={{ required: true }}
people={people}
/>
</div>
);
}
import { useController, UseControllerProps } from "react-hook-form";
import { Listbox as ListBox } from "@headlessui/react";
import { Person } from "../types";
type Props = {
people: Person[];
};
export const Listbox = (props: Props & UseControllerProps) => {
const {
field: { value, onChange }
} = useController(props);
const { people } = props;
return (
<>
<ListBox value={value} onChange={onChange}>
<ListBox.Button>
{value ? (value as Person).name : "Select Person"}
</ListBox.Button>
<ListBox.Options>
{people.map((person) => (
<ListBox.Option
key={person.id}
value={person}
disabled={person.unavailable}
>
{person.name}
</ListBox.Option>
))}
</ListBox.Options>
</ListBox>
</>
);
};
export type Person = {
id: number;
name: string;
unavailable: boolean;
};
@eddking
Copy link

eddking commented Aug 25, 2022

nice gist, thanks for the assist

@RibasAlexx
Copy link

thanks

@tkcel
Copy link

tkcel commented Sep 22, 2022

very helpful for me, thank you!

@FajarAlnito
Copy link

Thanks

@joaofranciscoguarda
Copy link

Thank you man! Helped me a lot, i wasn't using the "&" inside props definition on ListBox, and it solved my problem

@faqihalif
Copy link

combobox please....

@vbullsey
Copy link

ty bro

@j-bullard
Copy link
Author

combobox please....

I will get an example up soon.

@tomaszzmudzinski
Copy link

I am also curious how it will be implemented for combobox 😄

@DecadentIpsum
Copy link

It will be super nice to have an example with Combobox :)

@faqihalif
Copy link

combobox please....

I will get an example up soon.

combobox with react-window it will be nice 😁

@j-bullard
Copy link
Author

here is a demo repo for combobox. tomorrow I will resolve the display issue with headless, but as of right now, RHF and headless are both in sync when it comes to state (contact selected).

essentially you are using the Controller component to pass the required props to the Combobox component. HUI will consider it to be an uncontrolled component if you pass defaultValue to the main combobox component.

https://github.com/jasonabullard/hui-rhf-demo

@j-bullard
Copy link
Author

j-bullard commented Oct 18, 2022

@DecadentIpsum
Copy link

Thanks a lot @jasonabullard !

@faqihalif
Copy link

@nirglow
Copy link

nirglow commented Oct 31, 2022

Thanks for the Gist! It really helped me.
I used it with RadioGroup. The only adjustment I needed to make was to add the by attribute on the RadioGroup

@mihirsoni781
Copy link

mihirsoni781 commented Nov 16, 2022

Thanks @jasonabullard ! It worked fine, but has one issue when field is errored it is not getting autofocused, maybe issue related to ref, still not able to find any solution.

@stephent
Copy link

Thanks you @jasonabullard. Based on this, I added an example of a controlled component using headlessui Switch here.

@gabrrielsilva
Copy link

Thanks bro! It really helped me.

@JohanAltamar
Copy link

Does the error message work?

@LinnJS
Copy link

LinnJS commented Jan 4, 2023

Its funny cause when I think of contributions to open source I am always stuck in the mindset of "packages" but this post is a wonderful example of a super impactful open source contribution that is super achievable.

Thank you for saving me two hours of reading through docs, going to try to think of these kind of contributions in the future.

@buiducnhat
Copy link

It's very helpful, but when I use useForm like this:

const schema = yup....
type FormData = yup.InferType<typeof schema>;

const {
  control
} = useForm<FormData>({
  resolver: yupResolver(schema),
});

I have to use component like this:

<ListBox
  name="type"
  options={options}
  control={control as any}
/>

cause if I use control={control}, it show error of "types...."

@aqibgatoo
Copy link

how to do validation, consider a user hasn't selected any option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment