-
Notifications
You must be signed in to change notification settings - Fork 10
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
7302143
commit 8ec9cc7
Showing
6 changed files
with
189 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"ingred-ui": minor | ||
--- | ||
|
||
feat `<DateRangePicker />` component |
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
80 changes: 80 additions & 0 deletions
80
src/components/NewDateRangePicker/NewDateRangePicker.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,80 @@ | ||
import { StoryObj } from "@storybook/react"; | ||
import NewDateRangePicker, { | ||
NewDateRangePickerProps, | ||
} from "./NewDateRangePicker"; | ||
import dayjs from "dayjs"; | ||
import React, { useState } from "react"; | ||
|
||
export default { | ||
title: "Components/Inputs/NewDateRangePicker", | ||
component: NewDateRangePicker, | ||
args: { | ||
format: "YYYY-MM-DD", | ||
}, | ||
}; | ||
|
||
export const Example: StoryObj<NewDateRangePickerProps> = { | ||
render: (args) => { | ||
const [date, setDate] = useState({ | ||
startDate: dayjs(), | ||
endDate: dayjs().add(1, "week"), | ||
}); | ||
|
||
return ( | ||
<NewDateRangePicker | ||
{...args} | ||
startDate={date.startDate} | ||
endDate={date.endDate} | ||
onDatesChange={setDate} | ||
/> | ||
); | ||
}, | ||
}; | ||
|
||
export const WithActions: StoryObj<NewDateRangePickerProps> = { | ||
render: (args) => { | ||
const [date, setDate] = useState({ | ||
startDate: dayjs(), | ||
endDate: dayjs().add(1, "week"), | ||
}); | ||
const actions = [ | ||
{ | ||
text: "今日", | ||
onClick: () => { | ||
setDate({ | ||
startDate: dayjs(), | ||
endDate: dayjs(), | ||
}); | ||
}, | ||
}, | ||
{ | ||
text: "明日", | ||
onClick: () => { | ||
setDate({ | ||
startDate: dayjs(), | ||
endDate: dayjs().add(1, "day"), | ||
}); | ||
}, | ||
}, | ||
{ | ||
text: "来週", | ||
onClick: () => { | ||
setDate({ | ||
startDate: dayjs(), | ||
endDate: dayjs().add(1, "week"), | ||
}); | ||
}, | ||
}, | ||
]; | ||
|
||
return ( | ||
<NewDateRangePicker | ||
{...args} | ||
actions={actions} | ||
startDate={date.startDate} | ||
endDate={date.endDate} | ||
onDatesChange={setDate} | ||
/> | ||
); | ||
}, | ||
}; |
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,93 @@ | ||
import * as React from "react"; | ||
import { Flex, DateRangeField, CalendarRange, Card } from ".."; | ||
import { forwardRef, useRef, useState } from "react"; | ||
import { | ||
flip, | ||
offset, | ||
useDismiss, | ||
useFloating, | ||
useInteractions, | ||
useRole, | ||
} from "@floating-ui/react"; | ||
import { Dayjs } from "dayjs"; | ||
import { Action, Actions } from "../Calendar/internal/Actions"; | ||
|
||
export type NewDateRangePickerProps = { | ||
startDate: Dayjs; | ||
endDate: Dayjs; | ||
actions?: Action[]; | ||
onDatesChange: (date: { startDate: Dayjs; endDate: Dayjs }) => void; | ||
}; | ||
|
||
export const DateRangePicker = forwardRef< | ||
HTMLDivElement, | ||
NewDateRangePickerProps | ||
>(function DateRangePicker({ startDate, endDate, actions, onDatesChange }) { | ||
const [open, setOpen] = useState(false); | ||
const { context, refs, strategy, x, y } = useFloating({ | ||
placement: "right-start", | ||
open, | ||
onOpenChange: setOpen, | ||
middleware: [offset(10), flip()], | ||
}); | ||
const ref = useRef<HTMLDivElement>(null); | ||
const dismiss = useDismiss(context); | ||
const role = useRole(context); | ||
|
||
const { getReferenceProps, getFloatingProps } = useInteractions([ | ||
dismiss, | ||
role, | ||
]); | ||
|
||
const handleClickCalendarIcon = () => { | ||
setOpen((prev) => !prev); | ||
}; | ||
|
||
const handleClose = (clickState: "start" | "end") => { | ||
if (clickState === "end") { | ||
setOpen(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<Flex ref={ref}> | ||
<div | ||
ref={refs.setReference} | ||
{...getReferenceProps()} | ||
style={{ width: "fit-content" }} | ||
> | ||
<DateRangeField | ||
date={{ | ||
startDate, | ||
endDate, | ||
}} | ||
onDatesChange={onDatesChange} | ||
onClickCalendarIcon={handleClickCalendarIcon} | ||
/> | ||
</div> | ||
{open && ( | ||
<Card | ||
ref={refs.setFloating} | ||
display="flex" | ||
style={{ | ||
position: strategy, | ||
top: y ?? 0, | ||
left: x ?? 0, | ||
zIndex: 1, | ||
}} | ||
{...getFloatingProps()} | ||
> | ||
<Actions actions={actions} /> | ||
<CalendarRange | ||
startDate={startDate} | ||
endDate={endDate} | ||
onClose={handleClose} | ||
onDatesChange={onDatesChange} | ||
/> | ||
</Card> | ||
)} | ||
</Flex> | ||
); | ||
}); | ||
|
||
export default React.memo(DateRangePicker); |
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 @@ | ||
export { default } from "./NewDateRangePicker"; |
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