Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add close icon for DatePicker #1370

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/blue-houses-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ingred-ui": patch
---

add calendar close icon
15 changes: 13 additions & 2 deletions src/components/Calendar/Calendar/Calendar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs, { Dayjs } from "dayjs";
import { Card, ScrollArea, Typography } from "../..";
import { Card, Icon, ScrollArea, Typography } from "../..";
import React, { forwardRef, memo, useRef } from "react";
import { Day } from "./internal/Day";
import { weekList, HEIGHT } from "../constants";
Expand All @@ -9,18 +9,24 @@ import {
DatePickerContainer,
DayStyle,
CalendarMonth,
IconContainer,
} from "../styled";
import { useScroll } from "../hooks/useScroll";
import { Action, Actions } from "../internal/Actions";

export type CalendarProps = React.HTMLAttributes<HTMLDivElement> & {
date: Dayjs;
actions?: Action[];
/**
* 閉じるボタンを押したときの振る舞い
* この関数が渡されてないときは、閉じるボタンが表示されない
*/
onClickCloseButton?: () => void;
Comment on lines +20 to +24
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

閉じるボタンを押したときの callback を追加した

onDateChange: (value: Dayjs) => void;
};

const Calendar = forwardRef<HTMLDivElement, CalendarProps>(function Calendar(
{ date, actions, onDateChange, ...rest },
{ date, actions, onClickCloseButton, onDateChange, ...rest },
ref,
) {
const scrollRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -88,6 +94,11 @@ const Calendar = forwardRef<HTMLDivElement, CalendarProps>(function Calendar(
</>
</ScrollArea>
</Container>
{onClickCloseButton && (
<IconContainer onClick={onClickCloseButton}>
<Icon name="close" />
</IconContainer>
)}
</Card>
);
});
Expand Down
18 changes: 16 additions & 2 deletions src/components/Calendar/CalendarRange/CalendarRange.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs, { Dayjs } from "dayjs";
import { Card, ScrollArea, Typography } from "../..";
import { Card, Icon, ScrollArea, Typography } from "../..";
import React, { forwardRef, memo, useCallback, useRef, useState } from "react";
import { Day } from "./internal/Day";
import { HEIGHT, weekList } from "../constants";
Expand All @@ -9,6 +9,7 @@ import {
Container,
DatePickerContainer,
DayStyle,
IconContainer,
} from "../styled";
import { useScroll } from "../hooks/useScroll";
import { getDayState } from "./utils";
Expand All @@ -22,6 +23,11 @@ export type CalendarRangeProps = {
* 親コンポーネントで calendar を任意のタイミングで閉じたい場合に使用する
*/
onClose?: (clickState: "start" | "end") => void;
/**
* 閉じるボタンを押したときの振る舞い
* この関数が渡されてないときは、閉じるボタンが表示されない
*/
onClickCloseButton?: () => void;
onDatesChange: ({
startDate,
endDate,
Expand All @@ -37,7 +43,10 @@ export type CalendarRangeProps = {
* Currently, one year from the currently selected date is displayed.
*/
export const CalendarRange = forwardRef<HTMLDivElement, CalendarRangeProps>(
function ({ startDate, endDate, actions, onClose, onDatesChange }, ref) {
function (
{ startDate, endDate, actions, onClose, onClickCloseButton, onDatesChange },
ref,
) {
const scrollRef = useRef<HTMLDivElement>(null);
const { monthList } = useScroll(startDate ?? dayjs(), scrollRef);
const [clickState, setClickState] = useState<"start" | "end">("start");
Expand Down Expand Up @@ -123,6 +132,11 @@ export const CalendarRange = forwardRef<HTMLDivElement, CalendarRangeProps>(
</>
</ScrollArea>
</Container>
{onClickCloseButton && (
<IconContainer onClick={onClickCloseButton}>
<Icon name="close" />
</IconContainer>
)}
</Card>
);
},
Expand Down
9 changes: 9 additions & 0 deletions src/components/Calendar/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ export const CalendarMonth = styled.div`
justify-content: center;
padding: ${({ theme }) => theme.spacing * 2}px 0;
`;

export const IconContainer = styled.button`
cursor: pointer;
position: absolute;
top: ${({ theme }) => theme.spacing}px;
right: ${({ theme }) => theme.spacing}px;
border: none;
background: none;
`;
5 changes: 5 additions & 0 deletions src/components/NewDatePicker/NewDatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export const NewDatePicker = forwardRef<HTMLDivElement, NewDatePickerProps>(
[onDateChange],
);

const handleClose = useCallback(() => {
setOpen(false);
}, []);

return (
<Flex ref={ref} style={{ width: "fit-content" }}>
<div ref={refs.setReference} {...getReferenceProps()}>
Expand All @@ -83,6 +87,7 @@ export const NewDatePicker = forwardRef<HTMLDivElement, NewDatePickerProps>(
left: x ?? 0,
zIndex: 100,
}}
onClickCloseButton={handleClose}
onDateChange={handleClickDate}
{...getFloatingProps()}
/>
Expand Down
10 changes: 10 additions & 0 deletions src/components/NewDatePicker/styled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import styled from "styled-components";

export const IconContainer = styled.button`
cursor: pointer;
position: absolute;
top: ${({ theme }) => theme.spacing}px;
right: ${({ theme }) => theme.spacing}px;
border: none;
background: none;
`;
5 changes: 5 additions & 0 deletions src/components/NewDateRangePicker/NewDateRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export const DateRangePicker = forwardRef<
}
};

const handleClickCloseButton = React.useCallback(() => {
setOpen(false);
}, []);

return (
<Flex ref={ref}>
<div
Expand Down Expand Up @@ -82,6 +86,7 @@ export const DateRangePicker = forwardRef<
startDate={startDate}
endDate={endDate}
onClose={handleClose}
onClickCloseButton={handleClickCloseButton}
onDatesChange={onDatesChange}
/>
</Card>
Expand Down