Skip to content

Commit

Permalink
resolved conflict from main
Browse files Browse the repository at this point in the history
  • Loading branch information
aboveyunhai committed Jan 22, 2024
2 parents 4d446a3 + eb09a11 commit 966dd73
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
22 changes: 19 additions & 3 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const offsets: FirstDayOfWeek[] = [0, 1, 2, 3, 4, 5, 6];
const App = () => {
const { colorMode, toggleColorMode } = useColorMode();
const demoDate = new Date();
const [date, setDate] = useState(demoDate);
const [date, setDate] = useState<Date | undefined>(demoDate);
const [selectedDates, setSelectedDates] = useState<Date[]>([
new Date(),
new Date(),
Expand Down Expand Up @@ -138,12 +138,20 @@ const App = () => {
<TabPanel>
<Panel>
<Section title="Single:">
<Flex alignItems={'center'}>
<Flex alignItems={'center'} gap={2}>
<Box marginRight={'1rem'}>closeOnSelect:</Box>
<Switch
isChecked={isSingleChecked}
onChange={(e) => setSingleCheck(e.currentTarget.checked)}
/>
<Button
size={'sm'}
onClick={() => {
setDate(undefined);
}}
>
Set Empty (undefined)
</Button>
</Flex>
{/* chakra ui add prefix for the trigger for some reasons? */}
<Flex style={{ gap: '1rem' }} alignItems="center">
Expand Down Expand Up @@ -186,12 +194,20 @@ const App = () => {
</Flex>
</Section>
<Section title="Range:">
<Flex alignItems={'center'}>
<Flex alignItems={'center'} gap={2}>
<Box marginRight={'1rem'}>closeOnSelect:</Box>
<Switch
isChecked={isRangeChecked}
onChange={(e) => setRangeCheck(e.currentTarget.checked)}
/>
<Button
size={'sm'}
onClick={() => {
setSelectedDates([]);
}}
>
Set Empty (Empty Array: "[]")
</Button>
</Flex>
<Flex style={{ gap: '1rem' }} alignItems="center">
<label htmlFor={`popover-trigger-default-range`}>
Expand Down
20 changes: 11 additions & 9 deletions src/range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ interface RangeProps extends DatepickerProps {

export type RangeDatepickerProps = RangeProps & VariantProps;

const DefaultConfigs: CalendarConfigs = {
const DefaultConfigs: Required<DatepickerConfigs> = {
dateFormat: 'MM/dd/yyyy',
monthNames: Month_Names_Short,
dayNames: Weekday_Names_Short,
firstDayOfWeek: 0,
monthsToDisplay: 2,
};

const defaultProps = {
Expand Down Expand Up @@ -144,7 +145,7 @@ export const RangeDatepicker: React.FC<RangeDatepickerProps> = (props) => {
<CalendarIcon />
);

const calendarConfigs: CalendarConfigs = {
const datepickerConfigs = {
...DefaultConfigs,
...configs,
};
Expand Down Expand Up @@ -186,11 +187,11 @@ export const RangeDatepicker: React.FC<RangeDatepickerProps> = (props) => {

// eventually we want to allow user to freely type their own input and parse the input
let intVal = selectedDates[0]
? `${format(selectedDates[0], calendarConfigs.dateFormat)}`
: '';
? `${format(selectedDates[0], datepickerConfigs.dateFormat)}`
: `${datepickerConfigs.dateFormat}`;
intVal += selectedDates[1]
? ` - ${format(selectedDates[1], calendarConfigs.dateFormat)}`
: ` - ${calendarConfigs.dateFormat}`;
? ` - ${format(selectedDates[1], datepickerConfigs.dateFormat)}`
: ` - ${datepickerConfigs.dateFormat}`;

const PopoverContentWrapper = usePortal ? Portal : React.Fragment;

Expand All @@ -211,6 +212,7 @@ export const RangeDatepicker: React.FC<RangeDatepickerProps> = (props) => {
variant={'outline'}
lineHeight={1}
paddingX="1rem"
fontSize={'sm'}
disabled={disabled}
{...propsConfigs?.triggerBtnProps}
>
Expand Down Expand Up @@ -267,15 +269,15 @@ export const RangeDatepicker: React.FC<RangeDatepickerProps> = (props) => {
dayzedHookProps={{
onDateSelected: handleOnDateSelected,
selected: selectedDates,
monthsToDisplay: 2,
monthsToDisplay: datepickerConfigs.monthsToDisplay,
date: dateInView,
minDate: minDate,
maxDate: maxDate,
offset: offset,
onOffsetChanged: setOffset,
firstDayOfWeek: calendarConfigs.firstDayOfWeek,
firstDayOfWeek: datepickerConfigs.firstDayOfWeek,
}}
configs={calendarConfigs}
configs={datepickerConfigs}
propsConfigs={propsConfigs}
selected={selectedDates}
/>
Expand Down
24 changes: 14 additions & 10 deletions src/single.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import FocusLock from 'react-focus-lock';
import { Month_Names_Short, Weekday_Names_Short } from './utils/calanderUtils';
import { CalendarPanel } from './components/calendarPanel';
import {
CalendarConfigs,
DatepickerConfigs,
DatepickerProps,
OnDateSelected,
Expand Down Expand Up @@ -63,11 +62,12 @@ export type VariantProps =

export type SingleDatepickerProps = SingleProps & VariantProps;

const DefaultConfigs: CalendarConfigs = {
const DefaultConfigs: Required<DatepickerConfigs> = {
dateFormat: 'yyyy-MM-dd',
monthNames: Month_Names_Short,
dayNames: Weekday_Names_Short,
firstDayOfWeek: 0,
monthsToDisplay: 1,
};

const defaultProps = {
Expand Down Expand Up @@ -108,12 +108,13 @@ export const SingleDatepicker: React.FC<SingleDatepickerProps> = (props) => {
<CalendarIcon />
);

const calendarConfigs: CalendarConfigs = {
const datepickerConfigs = {
...DefaultConfigs,
...configs,
};

const [tempInput, setInputVal] = useState(
selectedDate ? format(selectedDate, calendarConfigs.dateFormat) : ''
selectedDate ? format(selectedDate, datepickerConfigs.dateFormat) : ''
);

const onPopoverClose = () => {
Expand All @@ -136,7 +137,7 @@ export const SingleDatepicker: React.FC<SingleDatepickerProps> = (props) => {
setInputVal(event.target.value);
const newDate = parse(
event.target.value,
calendarConfigs.dateFormat,
datepickerConfigs.dateFormat,
new Date()
);
if (!(newDate instanceof Date && !isNaN(newDate.getTime()))) {
Expand All @@ -151,9 +152,9 @@ export const SingleDatepicker: React.FC<SingleDatepickerProps> = (props) => {

useEffect(() => {
if (selectedDate) {
setInputVal(format(selectedDate, calendarConfigs.dateFormat));
setInputVal(format(selectedDate, datepickerConfigs.dateFormat));
}
}, [selectedDate, calendarConfigs.dateFormat]);
}, [selectedDate, datepickerConfigs.dateFormat]);

return (
<Popover
Expand All @@ -173,10 +174,11 @@ export const SingleDatepicker: React.FC<SingleDatepickerProps> = (props) => {
lineHeight={1}
paddingX="1rem"
disabled={disabled}
fontSize={'sm'}
{...propsConfigs?.triggerBtnProps}
>
{selectedDate
? format(selectedDate, calendarConfigs.dateFormat)
? format(selectedDate, datepickerConfigs.dateFormat)
: ''}
</Button>
</PopoverTrigger>
Expand All @@ -193,6 +195,7 @@ export const SingleDatepicker: React.FC<SingleDatepickerProps> = (props) => {
}}
id={id}
autoComplete="off"
width={'10rem'}
disabled={disabled}
isDisabled={disabled}
name={name}
Expand Down Expand Up @@ -230,16 +233,17 @@ export const SingleDatepicker: React.FC<SingleDatepickerProps> = (props) => {
<CalendarPanel
dayzedHookProps={{
showOutsideDays: true,
monthsToDisplay: datepickerConfigs.monthsToDisplay,
onDateSelected: handleOnDateSelected,
selected: selectedDate,
date: dateInView,
minDate: minDate,
maxDate: maxDate,
offset: offset,
onOffsetChanged: setOffset,
firstDayOfWeek: calendarConfigs.firstDayOfWeek,
firstDayOfWeek: datepickerConfigs.firstDayOfWeek,
}}
configs={calendarConfigs}
configs={datepickerConfigs}
propsConfigs={propsConfigs}
disabledDates={disabledDates}
/>
Expand Down
1 change: 1 addition & 0 deletions src/utils/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface DatepickerConfigs {
monthNames?: string[];
dayNames?: string[];
firstDayOfWeek?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
monthsToDisplay?: number;
}

export interface CalendarConfigs {
Expand Down

0 comments on commit 966dd73

Please sign in to comment.