-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate DatePicker and DatePickerRange
- Loading branch information
Showing
5 changed files
with
142 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/components/forms/controls/DatePicker/DatePickerRange.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* Copyright (c) Fortanix, Inc. | ||
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of | ||
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import * as React from 'react'; | ||
|
||
import { DatePickerRange } from './DatePickerRange.tsx'; | ||
|
||
|
||
type DatePickerRangeArgs = React.ComponentProps<typeof DatePickerRange>; | ||
type Story = StoryObj<DatePickerRangeArgs>; | ||
|
||
export default { | ||
component: DatePickerRange, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
argTypes: { | ||
}, | ||
args: {}, | ||
decorators: [ | ||
Story => <form onSubmit={event => { event.preventDefault(); }}><Story/></form>, | ||
], | ||
} satisfies Meta<DatePickerRangeArgs>; | ||
|
||
export const DatePickerRangeStory: Story = { | ||
name: 'Date Picker with Dates Range', | ||
render: (args) => { | ||
const [startDate, setStartDate] = React.useState(new Date()); | ||
const [endDate, setEndDate] = React.useState(new Date()); | ||
const onChange = (dates: [Date, Date]) => { | ||
const [start, end] = dates; | ||
setStartDate(start); | ||
setEndDate(end); | ||
}; | ||
|
||
return ( | ||
<div style={{height: '500px'}}> | ||
<DatePickerRange | ||
{...args} | ||
selected={startDate} | ||
onChange={onChange} | ||
startDate={startDate} | ||
endDate={endDate} | ||
inline={true} | ||
/> | ||
</div> | ||
); | ||
} | ||
}; |
72 changes: 72 additions & 0 deletions
72
src/components/forms/controls/DatePicker/DatePickerRange.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* Copyright (c) Fortanix, Inc. | ||
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of | ||
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import * as React from 'react'; | ||
import ReactDatePicker from 'react-datepicker'; | ||
|
||
import { classNames as cx, type ClassNameArgument, type ComponentProps } from '../../../../util/componentUtil.ts'; | ||
|
||
import 'react-datepicker/dist/react-datepicker.css'; | ||
import cl from './DatePicker.module.scss'; | ||
|
||
import { Input } from '../Input/Input.tsx'; | ||
|
||
|
||
// TODO: eventually move this to a separate file, as this tends to be repeated on every component? | ||
type GenericProps = { | ||
/** Whether this component should be unstyled. */ | ||
unstyled?: undefined | boolean, | ||
|
||
/** An optional class name to be appended to the class list. */ | ||
className?: ClassNameArgument, | ||
} | ||
|
||
// copying props from react-datepicker and restricting them to specific versions | ||
// TODO: I would like to reuse this from DatePicker.tsx, how can I reuse it from there without exporting it? | ||
// Or maybe the solution is to define all variants from the same file? | ||
type GenericReactDatePickerOmittedProps = Omit<ComponentProps<typeof ReactDatePicker>, 'selectsRange' | 'selectsMultiple' | 'onChange'>; | ||
|
||
type ReactDatePickerRangeProps = GenericReactDatePickerOmittedProps & { | ||
// not including selectsRange because we will pass that manually to react-datepicker | ||
// selectsMultiple?: never, | ||
// TODO: it seems like react-datepicker has a bug? the onChange accepts always "Date | null" or variations - | ||
// in this case, an array of exactly two elements of Date | null) | ||
// but then the startDate and endDate parameters do NOT take null. | ||
// therefore I think it'd make sense to handle them as [Date, Date] and | ||
// somehow make it accept (as our more strict variant is within what they accept) | ||
onChange?: (date: [Date, Date], event?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>) => void, | ||
}; | ||
|
||
export type DatePickerRangeProps = GenericProps & ReactDatePickerRangeProps; | ||
|
||
export const DatePickerRange = (props: DatePickerRangeProps) => { | ||
// TODO how could I reuse DatePicker component only passing the props that I want? Something as simple as | ||
// <DatePicker selectsRange={true} {...propsRest} /> | ||
|
||
const { | ||
unstyled = false, | ||
className, | ||
...propsRest | ||
} = props; | ||
|
||
return ( | ||
<div className={cx( | ||
'bk', | ||
{ [cl['bk-datepicker']]: !unstyled }, | ||
className, | ||
)}> | ||
<ReactDatePicker | ||
Check failure on line 59 in src/components/forms/controls/DatePicker/DatePickerRange.tsx GitHub Actions / build (22.x)
|
||
selectsRange={true} | ||
// everything else is the same | ||
className={cx( | ||
cl['bk-date-picker__date'], | ||
)} | ||
dateFormat="MM/dd/yyyy" | ||
placeholderText="MM/DD/YYYY" | ||
customInput={<Input />} | ||
{...propsRest} | ||
/> | ||
</div> | ||
); | ||
}; |