mirror of
https://github.com/snowball-tools/snowballtools-base
synced 2025-08-08 11:54:07 +00:00
* 🎨 style: adjust z index of the date picker popover * 🎨 style: add new spacing and rename box shadow from calendar to dropdown * 🐛 fix: console error becasue button inside button * ♻️ refactor: rename shadow calendar to shador dropdown on calendar component * 🚀 perf: remove vscode settings inside `packages/frontend` * ⚡️ feat: create check radio icon and chevron down icon component * 🔧 chore: install `downshift` * ⚡️ feat: create select component * 🎨 style: adjust the popover position based on the user screen * ⚡️ feat: separate select item to be a component * ⚡️ feat: separate select value to be a component * ♻️ refactor: adjust style and refactor to a new component * ⚡️ feat: create a type for merge two interface but keep the last value * 🐛 fix: forward ref the component to fix console error * ⚡️ feat: add `hideValues` prop to hide the values when on multiple * 🐛 fix: no result not showing * ⚡️ feat: make the select to be controller component * ♻️ refactor: remove console log * ♻️ refactor: update pr review
104 lines
2.8 KiB
TypeScript
104 lines
2.8 KiB
TypeScript
import React, { useCallback, useState } from 'react';
|
|
import { Input, InputProps } from 'components/shared/Input';
|
|
import * as Popover from '@radix-ui/react-popover';
|
|
import { datePickerTheme } from './DatePicker.theme';
|
|
import { Calendar, CalendarProps } from 'components/shared/Calendar';
|
|
import { CalendarIcon } from 'components/shared/CustomIcon';
|
|
import { Value } from 'react-calendar/dist/cjs/shared/types';
|
|
import { format } from 'date-fns';
|
|
|
|
export interface DatePickerProps
|
|
extends Omit<InputProps, 'onChange' | 'value'> {
|
|
/**
|
|
* The props for the calendar component.
|
|
*/
|
|
calendarProps?: CalendarProps;
|
|
/**
|
|
* Optional callback function that is called when the value of the input changes.
|
|
* @param {string} value - The new value of the input.
|
|
* @returns None
|
|
*/
|
|
onChange?: (value: Value) => void;
|
|
/**
|
|
* The value of the input.
|
|
*/
|
|
value?: Value;
|
|
/**
|
|
* Whether to allow the selection of a date range.
|
|
*/
|
|
selectRange?: boolean;
|
|
}
|
|
|
|
/**
|
|
* A date picker component that allows users to select a date from a calendar.
|
|
* @param {DatePickerProps} props - The props for the date picker component.
|
|
* @returns The rendered date picker component.
|
|
*/
|
|
export const DatePicker = ({
|
|
className,
|
|
calendarProps,
|
|
value,
|
|
onChange,
|
|
selectRange = false,
|
|
...props
|
|
}: DatePickerProps) => {
|
|
const { input } = datePickerTheme();
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
/**
|
|
* Renders the value of the date based on the current state of `props.value`.
|
|
* @returns {string | undefined} - The formatted date value or `undefined` if `props.value` is falsy.
|
|
*/
|
|
const renderValue = useCallback(() => {
|
|
if (!value) return undefined;
|
|
if (Array.isArray(value)) {
|
|
return value
|
|
.map((date) => format(date as Date, 'dd/MM/yyyy'))
|
|
.join(' - ');
|
|
}
|
|
return format(value, 'dd/MM/yyyy');
|
|
}, [value]);
|
|
|
|
/**
|
|
* Handles the selection of a date from the calendar.
|
|
*/
|
|
const handleSelect = useCallback(
|
|
(date: Value) => {
|
|
setOpen(false);
|
|
onChange?.(date);
|
|
},
|
|
[setOpen, onChange],
|
|
);
|
|
|
|
return (
|
|
<Popover.Root open={open}>
|
|
<Popover.Trigger>
|
|
<Input
|
|
{...props}
|
|
rightIcon={<CalendarIcon onClick={() => setOpen(true)} />}
|
|
readOnly
|
|
placeholder="Select a date..."
|
|
value={renderValue()}
|
|
className={input({ className })}
|
|
onClick={() => setOpen(true)}
|
|
/>
|
|
</Popover.Trigger>
|
|
<Popover.Portal>
|
|
<Popover.Content
|
|
className="z-10"
|
|
onInteractOutside={() => setOpen(false)}
|
|
>
|
|
<Calendar
|
|
{...calendarProps}
|
|
selectRange={selectRange}
|
|
value={value}
|
|
onCancel={() => setOpen(false)}
|
|
onSelect={handleSelect}
|
|
/>
|
|
</Popover.Content>
|
|
</Popover.Portal>
|
|
</Popover.Root>
|
|
);
|
|
};
|