react-day-picker icon indicating copy to clipboard operation
react-day-picker copied to clipboard

Improve docs about customizing the dropdown

Open gpbl opened this issue 1 year ago • 3 comments

gpbl avatar Aug 17 '24 11:08 gpbl

It seems I'm looking for info on this right now.

I'm making custom month and year dropdowns, and have gotten them to display, however, I'm not sure how to implement the props.onChange function.

Edit: On a custom component

MonthsDropdown: (props: DropdownProps) => {
        console.log(props);
        return (
          <div className="flex items-center gap-1">
            <Select
              disabled={props.disabled}
              onValueChange={(value) => {
                console.log("monthchange", value);
                props.onChange(Number.parseInt(value, 10));
              }}
              value={props.value?.toString()}
            >
              <SelectTrigger className="hover:bg-accent hover:text-accent-foreground h-7 border-none bg-transparent px-2 py-0 text-sm font-medium">
                <SelectValue placeholder={props["aria-label"]} />
              </SelectTrigger>
              <SelectContent>
                {props.options?.map((option) => (
                  <SelectItem
                    disabled={option.disabled}
                    key={option.value}
                    value={option.value.toString()}
                  >
                    {option.label}
                  </SelectItem>
                ))}
              </SelectContent>
            </Select>
          </div>
        );
      },

Sparticuz avatar Apr 15 '25 17:04 Sparticuz

Hi @Sparticuz

This is tricky indeed, you need to simulate an event object when calling the function, check this comment from another discussion about this issue: https://github.com/gpbl/react-day-picker/discussions/2676#discussioncomment-11984571.

rodgobbi avatar Apr 16 '25 14:04 rodgobbi

Thanks, that's the same solution I eventually found:

onValueChange={(value) => {
  if (props.onChange) {
    // Create a synthetic event object that matches ChangeEvent<HTMLSelectElement>
    const syntheticEvent = {
      target: { value },
    } as ChangeEvent<HTMLSelectElement>;
    props.onChange(syntheticEvent);
  }
}}

Sparticuz avatar Apr 16 '25 16:04 Sparticuz