ui icon indicating copy to clipboard operation
ui copied to clipboard

[Date range picker] First day selected of range disappears

Open la-ruee-vers-le-bois opened this issue 2 years ago • 3 comments

Hi :)

When I use the date range picker and select a range, the first day selected "disappears":

screen-capture (1).webm

An idea to solve this ?

Thx for your help

la-ruee-vers-le-bois avatar May 04 '23 11:05 la-ruee-vers-le-bois

Hi, please can you share your code?

martin-dos-santos avatar May 05 '23 21:05 martin-dos-santos

Hi :)

Yes of course:

'use client';
import * as React from 'react';
import { Button } from '@/components/UI/button';
import { addDays, format } from 'date-fns';
import { DateRange } from 'react-day-picker';
import { fr } from 'date-fns/locale';
import { Calendar as CalendarIcon } from 'lucide-react';
import { Calendar } from '@/components/UI/calendar';
import { cn } from '@/lib/utils';
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@/components/UI/popover';

export default function DateRangeCalendar(props) {
  const [date, setDate] = React.useState({
    from: props.dateFrom,
    to: props.dateTo,
  });

  return (
    <div className='grid gap-2'>
      <Popover>
        <PopoverTrigger asChild>
          <Button
            id='date'
            variant={'outline'}
            className={cn(
              'w-[300px] justify-start text-left font-normal',
              !date && 'text-muted-foreground'
            )}>
            <CalendarIcon className='mr-2 h-4 w-4' />
            {date?.from ? (
              date.to ? (
                <>
                  Du{' '}
                  {format(date.from, 'dd LLL y', {
                    locale: fr,
                  })}{' '}
                  au{' '}
                  {format(date.to, 'dd LLL y', {
                    locale: fr,
                  })}
                </>
              ) : (
                format(date.from, 'dd LLL y', {
                  locale: fr,
                })
              )
            ) : (
              <span>Choisir un créneau</span>
            )}
          </Button>
        </PopoverTrigger>
        <PopoverContent className='w-auto p-0' align='start'>
          <Calendar
            initialFocus
            mode='range'
            defaultMonth={date?.from}
            selected={date}
            onSelect={setDate}
            numberOfMonths={2}
            locale={fr}
          />
        </PopoverContent>
      </Popover>
    </div>
  );
}
`

la-ruee-vers-le-bois avatar May 08 '23 19:05 la-ruee-vers-le-bois

You might have already found a solution here but here's what I did to solve the same issue.

In your /components/ui/calendar.tsx file, add the following to the classNames prop:

day_range_start:
  "aria-selected:bg-primary aria-selected:text-primary-foreground",
day_range_end:
  "aria-selected:bg-primary aria-selected:text-primary-foreground",

chrisjh avatar Jun 21 '23 21:06 chrisjh

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

shadcn avatar Jul 02 '24 23:07 shadcn