generated from StanfordBDHG/NextJSTemplate
-
-
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.
- Loading branch information
1 parent
f1925f8
commit 929d4f8
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
packages/design-system/src/components/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,22 @@ | ||
// | ||
// This source file is part of the Stanford Biodesign Digital Health ENGAGE-HF open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
import { type Meta } from '@storybook/react' | ||
import { useState } from 'react' | ||
import { DatePicker } from './DatePicker' | ||
|
||
const meta: Meta<typeof DatePicker> = { | ||
title: 'Components/DatePicker', | ||
component: DatePicker, | ||
} | ||
|
||
export default meta | ||
|
||
export const Default = () => { | ||
const [date, setDate] = useState<Date | undefined>(new Date()) | ||
return <DatePicker mode="single" selected={date} onSelect={setDate} /> | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/design-system/src/components/DatePicker/DatePicker.test.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,31 @@ | ||
// | ||
// This source file is part of the Stanford Biodesign Digital Health ENGAGE-HF open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
import { fireEvent, render, screen } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import { DatePicker } from '.' | ||
|
||
describe('DatePicker', () => { | ||
it('renders date picker', () => { | ||
const onSelect = jest.fn() | ||
render( | ||
<DatePicker mode="single" selected={undefined} onSelect={onSelect} />, | ||
) | ||
|
||
const queryGoToPrevMonth = () => | ||
screen.queryByRole('button', { | ||
name: 'Go to previous month', | ||
}) | ||
|
||
expect(queryGoToPrevMonth()).not.toBeInTheDocument() | ||
|
||
const trigger = screen.getByRole('button', { name: 'Pick a date' }) | ||
fireEvent.click(trigger) | ||
|
||
expect(queryGoToPrevMonth()).toBeInTheDocument() | ||
}) | ||
}) |
38 changes: 38 additions & 0 deletions
38
packages/design-system/src/components/DatePicker/DatePicker.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,38 @@ | ||
// | ||
// This source file is part of the Stanford Biodesign Digital Health ENGAGE-HF open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
'use client' | ||
import { format, isDate } from 'date-fns' | ||
import { Calendar as CalendarIcon } from 'lucide-react' | ||
import { type ComponentProps } from 'react' | ||
import { type DayPicker } from 'react-day-picker' | ||
import { cn } from '../../utils/className' | ||
import { Button } from '../Button' | ||
import { Calendar } from '../Calendar' | ||
import { Popover, PopoverTrigger, PopoverContent } from '../Popover' | ||
|
||
export type DatePickerProps = ComponentProps<typeof DayPicker> | ||
|
||
export const DatePicker = ({ selected, ...props }: DatePickerProps) => ( | ||
<Popover> | ||
<PopoverTrigger asChild> | ||
<Button | ||
variant="outline" | ||
className={cn( | ||
'!w-[280px] !justify-start !text-left', | ||
!selected && '!text-muted-foreground', | ||
)} | ||
> | ||
<CalendarIcon className="size-4" /> | ||
{selected && isDate(selected) ? format(selected, 'PPP') : 'Pick a date'} | ||
</Button> | ||
</PopoverTrigger> | ||
<PopoverContent className="!w-auto !p-0"> | ||
<Calendar {...props} /> | ||
</PopoverContent> | ||
</Popover> | ||
) |
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,9 @@ | ||
// | ||
// This source file is part of the Stanford Biodesign Digital Health ENGAGE-HF open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
'use client' | ||
export * from './DatePicker' |