-
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.
Merge pull request #42 from fortanix/datepicker
Datepicker
- Loading branch information
Showing
19 changed files
with
862 additions
and
5 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
37 changes: 37 additions & 0 deletions
37
src/components/forms/controls/DatePicker/DatePicker.module.scss
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,37 @@ | ||
/* 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/. */ | ||
|
||
@use '../../../../styling/defs.scss' as bk; | ||
@use '../../../../styling/lib/react-datepicker.scss'; | ||
|
||
@layer baklava.components { | ||
$caret-width: bk.rem-from-px(18); | ||
|
||
.bk-date-picker { | ||
@include bk.component-base(bk-date-picker); | ||
display: flex; | ||
flex-direction: row; | ||
align-items: center; | ||
margin-right: -$caret-width; | ||
} | ||
|
||
.bk-date-picker--input { | ||
width: bk.rem-from-px(134); | ||
color: bk.$theme-form-text-default; | ||
} | ||
|
||
.bk-date-picker--caret { | ||
width: $caret-width; | ||
height: $caret-width; | ||
position: relative; | ||
left: -$caret-width; | ||
top: bk.rem-from-px(-1); | ||
} | ||
|
||
.bk-date-picker--caret__down { | ||
// only when the date picker is closed, allow click on caret to pass through the input behind it; | ||
// if it is open, then we don't, so it counts as a click outside of the input and date picker, which closes it. | ||
pointer-events: none; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/components/forms/controls/DatePicker/DatePicker.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,63 @@ | ||
/* 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 { DatePicker } from './DatePicker.tsx'; | ||
|
||
|
||
type DatePickerArgs = React.ComponentProps<typeof DatePicker>; | ||
type Story = StoryObj<DatePickerArgs>; | ||
|
||
export default { | ||
component: DatePicker, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
argTypes: { | ||
}, | ||
args: {}, | ||
decorators: [ | ||
Story => <form onSubmit={event => { event.preventDefault(); }}><Story/></form>, | ||
], | ||
} satisfies Meta<DatePickerArgs>; | ||
|
||
export const Standard: Story = { | ||
render: (args) => { | ||
const [startDate, setStartDate] = React.useState<Date | null>(new Date(2024, 11, 12, 9, 41)); | ||
|
||
return ( | ||
// with a fixed height to avoid hiding the bottom part of the calendar | ||
<div style={{height: '500px'}}> | ||
<DatePicker | ||
{...args} | ||
selected={startDate} | ||
onChange={(date: Date | null) => { setStartDate(date) }} | ||
/> | ||
<p>Date selected: {startDate?.toDateString()}</p> | ||
</div> | ||
); | ||
} | ||
}; | ||
|
||
export const DateNotSet: Story = { | ||
render: (args) => { | ||
const [startDate, setStartDate] = React.useState<Date | null>(null); | ||
|
||
return ( | ||
// with a fixed height to avoid hiding the bottom part of the calendar | ||
<div style={{height: '500px'}}> | ||
<DatePicker | ||
{...args} | ||
selected={startDate} | ||
onChange={(date: Date | null) => { setStartDate(date) }} | ||
/> | ||
<p>Date selected: {startDate?.toDateString()}</p> | ||
</div> | ||
); | ||
} | ||
}; |
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,78 @@ | ||
/* 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 { Icon } from '../../../graphics/Icon/Icon.tsx'; | ||
import { Input } from '../Input/Input.tsx'; | ||
|
||
import 'react-datepicker/dist/react-datepicker.css'; | ||
import cl from './DatePicker.module.scss'; | ||
|
||
|
||
type GenericProps = { | ||
/** Whether this component should be unstyled. */ | ||
unstyled?: undefined | boolean, | ||
|
||
/** An optional class name to be appended to the class list. */ | ||
className?: undefined | ClassNameArgument, | ||
}; | ||
|
||
type ReactDatePickerProps = Omit<ComponentProps<typeof ReactDatePicker>, 'onChange'> & { | ||
// Note: need to disable the following features in order to use `ReactDatePicker` as a plain date picker. | ||
selectsRange?: never, | ||
selectsMultiple?: never, | ||
showMonthYearDropdown?: never, | ||
onChange: ((date: Date | null, event?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>) => void), | ||
selected: Date | null, | ||
}; | ||
|
||
export type DatePickerProps = GenericProps & ReactDatePickerProps; | ||
|
||
export const DatePicker = (props: DatePickerProps) => { | ||
const { | ||
unstyled = false, | ||
className, | ||
dateFormat = 'MM/dd/yyyy', | ||
placeholderText = 'MM/DD/YYYY', | ||
...propsRest | ||
} = props; | ||
|
||
const [isOpen, setIsOpen] = React.useState<boolean>(false); | ||
|
||
return ( | ||
<div className={cx( | ||
'bk', | ||
{ [cl['bk-date-picker']]: !unstyled }, | ||
className, | ||
)}> | ||
<ReactDatePicker | ||
dateFormat={dateFormat} | ||
placeholderText={placeholderText} | ||
showIcon | ||
icon={<Icon icon="calendar"/>} | ||
customInput={ | ||
<Input className={cx( | ||
{ [cl['bk-date-picker--input']]: !unstyled }, | ||
)}/> | ||
} | ||
onCalendarClose={() => setIsOpen(false)} | ||
onCalendarOpen={() => setIsOpen(true)} | ||
{...propsRest} | ||
selected={props.selected} | ||
onChange={props.onChange} | ||
/> | ||
<Icon | ||
className={cx( | ||
cl['bk-date-picker--caret'], | ||
{ [cl['bk-date-picker--caret__down']]: !isOpen }, | ||
)} | ||
icon={`caret-${isOpen ? 'up' : 'down'}`} | ||
/> | ||
</div> | ||
); | ||
}; |
20 changes: 20 additions & 0 deletions
20
src/components/forms/controls/DatePickerRange/DatePickerRange.module.scss
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,20 @@ | ||
/* 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/. */ | ||
|
||
@use '../../../../styling/defs.scss' as bk; | ||
@use '../../../../styling/lib/react-datepicker.scss'; | ||
|
||
@layer baklava.components { | ||
.bk-date-picker-range { | ||
@include bk.component-base(bk-date-picker-range); | ||
display: flex; | ||
flex-direction: row; | ||
align-items: center; | ||
} | ||
|
||
.bk-date-picker-range--input { | ||
width: bk.rem-from-px(134); | ||
color: bk.$theme-form-text-default; | ||
} | ||
} |
Oops, something went wrong.