mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-09-15 07:12:24 +03:00
add time and date picker components
This commit is contained in:
parent
3ed8260877
commit
3263dbd6da
5 changed files with 302 additions and 0 deletions
129
src/app/components/time-date/DatePicker.tsx
Normal file
129
src/app/components/time-date/DatePicker.tsx
Normal file
|
@ -0,0 +1,129 @@
|
|||
import React, { forwardRef } from 'react';
|
||||
import { Menu, Box, Text, Chip } from 'folds';
|
||||
import dayjs from 'dayjs';
|
||||
import * as css from './styles.css';
|
||||
import { PickerColumn } from './PickerColumn';
|
||||
import { dateFor, daysInMonth, daysToMs } from '../../utils/time';
|
||||
|
||||
type DatePickerProps = {
|
||||
min: number;
|
||||
max: number;
|
||||
value: number;
|
||||
onChange: (value: number) => void;
|
||||
};
|
||||
export const DatePicker = forwardRef<HTMLDivElement, DatePickerProps>(
|
||||
({ min, max, value, onChange }, ref) => {
|
||||
const selectedYear = dayjs(value).year();
|
||||
const selectedMonth = dayjs(value).month() + 1;
|
||||
const selectedDay = dayjs(value).date();
|
||||
|
||||
const handleSubmit = (newValue: number) => {
|
||||
onChange(Math.min(Math.max(min, newValue), max));
|
||||
};
|
||||
|
||||
const handleDay = (day: number) => {
|
||||
const seconds = daysToMs(day);
|
||||
const lastSeconds = daysToMs(selectedDay);
|
||||
const newValue = value + (seconds - lastSeconds);
|
||||
handleSubmit(newValue);
|
||||
};
|
||||
|
||||
const handleMonthAndYear = (month: number, year: number) => {
|
||||
const mDays = daysInMonth(month, year);
|
||||
const currentDate = dateFor(selectedYear, selectedMonth, selectedDay);
|
||||
const time = value - currentDate;
|
||||
|
||||
const newDate = dateFor(year, month, mDays < selectedDay ? mDays : selectedDay);
|
||||
|
||||
const newValue = newDate + time;
|
||||
handleSubmit(newValue);
|
||||
};
|
||||
|
||||
const handleMonth = (month: number) => {
|
||||
handleMonthAndYear(month, selectedYear);
|
||||
};
|
||||
|
||||
const handleYear = (year: number) => {
|
||||
handleMonthAndYear(selectedMonth, year);
|
||||
};
|
||||
|
||||
const minYear = dayjs(min).year();
|
||||
const maxYear = dayjs(max).year();
|
||||
const yearsRange = maxYear - minYear + 1;
|
||||
|
||||
const minMonth = dayjs(min).month() + 1;
|
||||
const maxMonth = dayjs(max).month() + 1;
|
||||
|
||||
const minDay = dayjs(min).date();
|
||||
const maxDay = dayjs(max).date();
|
||||
return (
|
||||
<Menu className={css.PickerMenu} ref={ref}>
|
||||
<Box direction="Row" gap="200" className={css.PickerContainer}>
|
||||
<PickerColumn title="Day">
|
||||
{Array.from(Array(daysInMonth(selectedMonth, selectedYear)).keys())
|
||||
.map((i) => i + 1)
|
||||
.map((day) => (
|
||||
<Chip
|
||||
key={day}
|
||||
size="500"
|
||||
variant={selectedDay === day ? 'Primary' : 'SurfaceVariant'}
|
||||
fill="None"
|
||||
radii="300"
|
||||
aria-selected={selectedDay === day}
|
||||
onClick={() => handleDay(day)}
|
||||
disabled={
|
||||
(selectedYear === minYear && selectedMonth === minMonth && day < minDay) ||
|
||||
(selectedYear === maxYear && selectedMonth === maxMonth && day > maxDay)
|
||||
}
|
||||
>
|
||||
<Text size="T300">{day}</Text>
|
||||
</Chip>
|
||||
))}
|
||||
</PickerColumn>
|
||||
<PickerColumn title="Month">
|
||||
{Array.from(Array(12).keys())
|
||||
.map((i) => i + 1)
|
||||
.map((month) => (
|
||||
<Chip
|
||||
key={month}
|
||||
size="500"
|
||||
variant={selectedMonth === month ? 'Primary' : 'SurfaceVariant'}
|
||||
fill="None"
|
||||
radii="300"
|
||||
aria-selected={selectedMonth === month}
|
||||
onClick={() => handleMonth(month)}
|
||||
disabled={
|
||||
(selectedYear === minYear && month < minMonth) ||
|
||||
(selectedYear === maxYear && month > maxMonth)
|
||||
}
|
||||
>
|
||||
<Text size="T300">
|
||||
{dayjs()
|
||||
.month(month - 1)
|
||||
.format('MMM')}
|
||||
</Text>
|
||||
</Chip>
|
||||
))}
|
||||
</PickerColumn>
|
||||
<PickerColumn title="Year">
|
||||
{Array.from(Array(yearsRange).keys())
|
||||
.map((i) => minYear + i)
|
||||
.map((year) => (
|
||||
<Chip
|
||||
key={year}
|
||||
size="500"
|
||||
variant={selectedYear === year ? 'Primary' : 'SurfaceVariant'}
|
||||
fill="None"
|
||||
radii="300"
|
||||
aria-selected={selectedYear === year}
|
||||
onClick={() => handleYear(year)}
|
||||
>
|
||||
<Text size="T300">{year}</Text>
|
||||
</Chip>
|
||||
))}
|
||||
</PickerColumn>
|
||||
</Box>
|
||||
</Menu>
|
||||
);
|
||||
}
|
||||
);
|
23
src/app/components/time-date/PickerColumn.tsx
Normal file
23
src/app/components/time-date/PickerColumn.tsx
Normal file
|
@ -0,0 +1,23 @@
|
|||
import React, { ReactNode } from 'react';
|
||||
import { Box, Text, Scroll } from 'folds';
|
||||
import { CutoutCard } from '../cutout-card';
|
||||
import * as css from './styles.css';
|
||||
|
||||
export function PickerColumn({ title, children }: { title: string; children: ReactNode }) {
|
||||
return (
|
||||
<Box direction="Column" gap="100">
|
||||
<Text className={css.PickerColumnLabel} size="L400">
|
||||
{title}
|
||||
</Text>
|
||||
<Box grow="Yes">
|
||||
<CutoutCard variant="Background">
|
||||
<Scroll variant="Background" size="300" hideTrack>
|
||||
<Box className={css.PickerColumnContent} direction="Column" gap="100">
|
||||
{children}
|
||||
</Box>
|
||||
</Scroll>
|
||||
</CutoutCard>
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
}
|
132
src/app/components/time-date/TimePicker.tsx
Normal file
132
src/app/components/time-date/TimePicker.tsx
Normal file
|
@ -0,0 +1,132 @@
|
|||
import React, { forwardRef } from 'react';
|
||||
import { Menu, Box, Text, Chip } from 'folds';
|
||||
import dayjs from 'dayjs';
|
||||
import * as css from './styles.css';
|
||||
import { PickerColumn } from './PickerColumn';
|
||||
import { hour12to24, hour24to12, hoursToMs, inSameDay, minutesToMs } from '../../utils/time';
|
||||
|
||||
type TimePickerProps = {
|
||||
min: number;
|
||||
max: number;
|
||||
value: number;
|
||||
onChange: (value: number) => void;
|
||||
};
|
||||
export const TimePicker = forwardRef<HTMLDivElement, TimePickerProps>(
|
||||
({ min, max, value, onChange }, ref) => {
|
||||
const hour24 = dayjs(value).hour();
|
||||
|
||||
const selectedHour = hour24to12(hour24);
|
||||
const selectedMinute = dayjs(value).minute();
|
||||
const selectedPM = hour24 >= 12;
|
||||
|
||||
const handleSubmit = (newValue: number) => {
|
||||
onChange(Math.min(Math.max(min, newValue), max));
|
||||
};
|
||||
|
||||
const handleHour = (hour: number) => {
|
||||
const seconds = hoursToMs(hour12to24(hour, selectedPM));
|
||||
const lastSeconds = hoursToMs(hour24);
|
||||
const newValue = value + (seconds - lastSeconds);
|
||||
handleSubmit(newValue);
|
||||
};
|
||||
|
||||
const handleMinute = (minute: number) => {
|
||||
const seconds = minutesToMs(minute);
|
||||
const lastSeconds = minutesToMs(selectedMinute);
|
||||
const newValue = value + (seconds - lastSeconds);
|
||||
handleSubmit(newValue);
|
||||
};
|
||||
|
||||
const handlePeriod = (pm: boolean) => {
|
||||
const seconds = hoursToMs(hour12to24(selectedHour, pm));
|
||||
const lastSeconds = hoursToMs(hour24);
|
||||
const newValue = value + (seconds - lastSeconds);
|
||||
handleSubmit(newValue);
|
||||
};
|
||||
|
||||
const minHour24 = dayjs(min).hour();
|
||||
const maxHour24 = dayjs(max).hour();
|
||||
|
||||
const minMinute = dayjs(min).minute();
|
||||
const maxMinute = dayjs(max).minute();
|
||||
const minPM = minHour24 >= 12;
|
||||
const maxPM = maxHour24 >= 12;
|
||||
|
||||
const minDay = inSameDay(min, value);
|
||||
const maxDay = inSameDay(max, value);
|
||||
|
||||
return (
|
||||
<Menu className={css.PickerMenu} ref={ref}>
|
||||
<Box direction="Row" gap="200" className={css.PickerContainer}>
|
||||
<PickerColumn title="Hour">
|
||||
{Array.from(Array(12).keys())
|
||||
.map((i) => {
|
||||
if (i === 0) return 12;
|
||||
return i;
|
||||
})
|
||||
.map((hour) => (
|
||||
<Chip
|
||||
key={hour}
|
||||
size="500"
|
||||
variant={hour === selectedHour ? 'Primary' : 'Background'}
|
||||
fill="None"
|
||||
radii="300"
|
||||
aria-selected={hour === selectedHour}
|
||||
onClick={() => handleHour(hour)}
|
||||
disabled={
|
||||
(minDay && hour12to24(hour, selectedPM) < minHour24) ||
|
||||
(maxDay && hour12to24(hour, selectedPM) > maxHour24)
|
||||
}
|
||||
>
|
||||
<Text size="T300">{hour < 10 ? `0${hour}` : hour}</Text>
|
||||
</Chip>
|
||||
))}
|
||||
</PickerColumn>
|
||||
<PickerColumn title="Minutes">
|
||||
{Array.from(Array(60).keys()).map((minute) => (
|
||||
<Chip
|
||||
key={minute}
|
||||
size="500"
|
||||
variant={minute === selectedMinute ? 'Primary' : 'Background'}
|
||||
fill="None"
|
||||
radii="300"
|
||||
aria-selected={minute === selectedMinute}
|
||||
onClick={() => handleMinute(minute)}
|
||||
disabled={
|
||||
(minDay && hour24 === minHour24 && minute < minMinute) ||
|
||||
(maxDay && hour24 === maxHour24 && minute > maxMinute)
|
||||
}
|
||||
>
|
||||
<Text size="T300">{minute < 10 ? `0${minute}` : minute}</Text>
|
||||
</Chip>
|
||||
))}
|
||||
</PickerColumn>
|
||||
<PickerColumn title="Period">
|
||||
<Chip
|
||||
size="500"
|
||||
variant={!selectedPM ? 'Primary' : 'SurfaceVariant'}
|
||||
fill="None"
|
||||
radii="300"
|
||||
aria-selected={!selectedPM}
|
||||
onClick={() => handlePeriod(false)}
|
||||
disabled={minDay && minPM}
|
||||
>
|
||||
<Text size="T300">AM</Text>
|
||||
</Chip>
|
||||
<Chip
|
||||
size="500"
|
||||
variant={selectedPM ? 'Primary' : 'SurfaceVariant'}
|
||||
fill="None"
|
||||
radii="300"
|
||||
aria-selected={selectedPM}
|
||||
onClick={() => handlePeriod(true)}
|
||||
disabled={maxDay && !maxPM}
|
||||
>
|
||||
<Text size="T300">PM</Text>
|
||||
</Chip>
|
||||
</PickerColumn>
|
||||
</Box>
|
||||
</Menu>
|
||||
);
|
||||
}
|
||||
);
|
2
src/app/components/time-date/index.ts
Normal file
2
src/app/components/time-date/index.ts
Normal file
|
@ -0,0 +1,2 @@
|
|||
export * from './TimePicker';
|
||||
export * from './DatePicker';
|
16
src/app/components/time-date/styles.css.ts
Normal file
16
src/app/components/time-date/styles.css.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
import { style } from '@vanilla-extract/css';
|
||||
import { config, toRem } from 'folds';
|
||||
|
||||
export const PickerMenu = style({
|
||||
padding: config.space.S200,
|
||||
});
|
||||
export const PickerContainer = style({
|
||||
maxHeight: toRem(250),
|
||||
});
|
||||
export const PickerColumnLabel = style({
|
||||
padding: config.space.S200,
|
||||
});
|
||||
export const PickerColumnContent = style({
|
||||
padding: config.space.S200,
|
||||
paddingRight: 0,
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue