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

Support localization and DateRangePicker #572

Open
2 tasks
cheton opened this issue Apr 18, 2022 · 1 comment
Open
2 tasks

Support localization and DateRangePicker #572

cheton opened this issue Apr 18, 2022 · 1 comment

Comments

@cheton
Copy link
Member

cheton commented Apr 18, 2022

  • Localization
    https://mui.com/x/react-date-pickers/date-picker/
    <LocalizationProvider dateAdapter={AdapterDateFns}>
      <DateCalendar locale="en-US" />
      <DateCalendar locale="de-DE" />
    </LocalizationProvider>
  • Range selection
    <DateRangeCalendar defaultValue={[startDate, endDate]} />
import { isSameDay, isWithinInterval } from 'date-fns';
import React, { useState, useCallback } from 'react';
import { Box } from '../../box';
import DateCalendar from '../DateCalendar';

const DateRangeCalendar = ({
  value, // Controlled value
  defaultValue = [null, null], // Uncontrolled initial value
  onChange, // Callback for value change
  ...rest
}) => {
  const [internalValue, setInternalValue] = useState(defaultValue);
  const [hoveredDate, setHoveredDate] = useState(null);

  const isControlled = value !== undefined;
  const [startDate, endDate] = isControlled ? value : internalValue;

  const handleDateChange = useCallback(
    (date) => {
      let newValue;
      if (!startDate || (startDate && endDate)) {
        // Reset selection or start a new range
        newValue = [date, null];
      } else {
        // Set the end date
        if (date >= startDate) {
          newValue = [startDate, date];
        } else {
          newValue = [date, startDate];
        }
      }

      if (!isControlled) {
        setInternalValue(newValue);
      }
      onChange?.(newValue);
    },
    [startDate, endDate, onChange, isControlled]
  );

  const isDateInRange = useCallback(
    (date) => {
      if (startDate && endDate) {
        return isWithinInterval(date, { start: startDate, end: endDate });
      }
      if (startDate && hoveredDate) {
        const start = startDate < hoveredDate ? startDate : hoveredDate;
        const end = startDate > hoveredDate ? startDate : hoveredDate;
        return isWithinInterval(date, { start, end });
      }
      return false;
    },
    [startDate, endDate, hoveredDate]
  );

  const renderDay = useCallback(
    (day) => {
      const isSelectedStart = startDate && isSameDay(day, startDate);
      const isSelectedEnd = endDate && isSameDay(day, endDate);
      const isInRange = isDateInRange(day);

      const style = {
        backgroundColor: isSelectedStart || isSelectedEnd ? 'blue' : isInRange ? 'lightblue' : 'transparent',
        color: isSelectedStart || isSelectedEnd ? 'white' : 'black',
        borderRadius: isSelectedStart ? '50%' : isSelectedEnd ? '50%' : '0%',
      };

      return (
        <div style={style}>
          {day.getDate()}
        </div>
      );
    },
    [startDate, endDate, isDateInRange]
  );

  return (
    <Box display="flex" gap="16px" {...rest}>
      <DateCalendar
        value={startDate}
        onChange={handleDateChange}
        onDayHover={setHoveredDate}
        //renderDay={renderDay}
      />
      <DateCalendar
        value={endDate}
        onChange={handleDateChange}
        onDayHover={setHoveredDate}
        //renderDay={renderDay}
      />
    </Box>
  );
};

export default DateRangeCalendar;
@cheton cheton changed the title New enhancements for Calendar and DatePicker Add localization and range selection to Calendar and DatePicker Apr 27, 2022
@cheton cheton changed the title Add localization and range selection to Calendar and DatePicker Support localization and range selection for Calendar and DatePicker components Apr 27, 2022
@cheton cheton added the P2 label Jun 19, 2024
@cheton cheton removed the P2 label Jul 11, 2024
@cheton
Copy link
Member Author

cheton commented Dec 17, 2024

@cheton cheton changed the title Support localization and range selection for Calendar and DatePicker components Support localization and DateRangePicker Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant