Skip to content

Commit

Permalink
feat(daterange): allow clear on selected daterange (#466)
Browse files Browse the repository at this point in the history
* allow clearing selected date range
  • Loading branch information
Souravpakhira authored May 18, 2023
1 parent 6c2407a commit efe4800
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
.DS_Store
.DS_Store
storybook-static
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface DateRangePickerProps
disabled?: boolean;
color?: Color;
locale?: Locale;
enableClear?: boolean;
}

const DateRangePicker = React.forwardRef<HTMLDivElement, DateRangePickerProps>((props, ref) => {
Expand All @@ -67,6 +68,7 @@ const DateRangePicker = React.forwardRef<HTMLDivElement, DateRangePickerProps>((
color = BaseColors.Blue,
enableYearPagination = false,
locale = enUS,
enableClear = true,
className,
...other
} = props;
Expand Down Expand Up @@ -129,6 +131,10 @@ const DateRangePicker = React.forwardRef<HTMLDivElement, DateRangePickerProps>((
setShowDropdown(false);
};

const handleClear = () => {
setSelectedValue([null, null, null]);
onValueChange?.([null, null, null]);
};
const [hoveredDropdownValue, handleDropdownKeyDown] = useSelectOnKeyDown(
handleDropdownOptionClick,
dropdownOptions.map((option: DateRangePickerOption) => option.value),
Expand Down Expand Up @@ -160,6 +166,8 @@ const DateRangePicker = React.forwardRef<HTMLDivElement, DateRangePickerProps>((
onDropdownKeyDown={handleDropdownKeyDown}
locale={locale}
dropdownPlaceholder={dropdownPlaceholder}
enableClear={enableClear}
onClear={handleClear}
/>
{/* Calendar Modal */}
<Modal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Dispatch, Ref, SetStateAction } from "react";
import { twMerge } from "tailwind-merge";

import { ArrowDownHeadIcon, CalendarIcon } from "assets";
import { ArrowDownHeadIcon, CalendarIcon, XCircleIcon } from "assets";

import {
BaseColors,
Expand Down Expand Up @@ -36,6 +36,8 @@ interface DateRangePickerButtonProps {
onDropdownKeyDown: (e: React.KeyboardEvent<HTMLButtonElement>) => void;
locale?: Locale;
dropdownPlaceholder?: string;
enableClear?: boolean;
onClear: () => void;
}

const DateRangePickerButton = ({
Expand All @@ -54,6 +56,8 @@ const DateRangePickerButton = ({
onDropdownKeyDown,
locale,
dropdownPlaceholder = "Select",
enableClear,
onClear,
}: DateRangePickerButtonProps) => {
const [startDate, endDate, dropdownValue] = value;

Expand Down Expand Up @@ -110,13 +114,33 @@ const DateRangePickerButton = ({
<p
className={twMerge(
makeDateRangePickerClassName("calendarButtonText"),
"whitespace-nowrap truncate",
"whitespace-nowrap truncate w-full text-start",
fontSize.sm,
fontWeight.md,
)}
>
{calendarText}
</p>
{enableClear && hasDateSelection ? (
<div
role="button"
className={twMerge(makeDateRangePickerClassName("resetButton"), spacing.xs.marginRight)}
onClick={(e) => {
e.stopPropagation(); // prevent firing parent button
onClear();
}}
>
<XCircleIcon
className={twMerge(
"flex-none",
sizing.md.height,
sizing.md.width,
getColorClassNames(DEFAULT_COLOR, colorPalette.lightText).textColor,
)}
aria-hidden="true"
/>
</div>
) : null}
</button>
{enableDropdown ? (
<button
Expand Down
6 changes: 6 additions & 0 deletions src/stories/input-elements/DateRangePicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,9 @@ ControlledWithDropdownOptionsWithEndDate.args = {
},
],
};

export const UncontrolledWithoutAllowClear = UncontrolledTemplate.bind({});
UncontrolledWithoutAllowClear.args = {
defaultValue: [new Date(2022, 10, 1), new Date()],
enableClear: false,
};

0 comments on commit efe4800

Please sign in to comment.