-
Notifications
You must be signed in to change notification settings - Fork 10
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
DatePicker, DateRangePickerをdayjsでやりとりするようにwrap #1080
Changes from 6 commits
2735e9f
112bf4f
ad4c49d
e88d160
ebebd98
c38ca39
8021b0c
6c5f737
4beb7e4
2f503db
f2bdb75
a6c606b
eb4ce2d
3a5e650
7ba31c9
fa67975
2e1b445
11c9b96
95dbf97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -26,6 +26,7 @@ | |||||
}, | ||||||
"dependencies": { | ||||||
"@popperjs/core": "^2.4.0", | ||||||
"dayjs": "^1.11.6", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
"moment": "^2.29.3", | ||||||
"react-dates": "^21.8.0", | ||||||
"react-popper": "^2.3.0", | ||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,7 +1,9 @@ | ||||
import * as React from "react"; | ||||
import * as Styled from "./styled"; | ||||
import "react-dates/initialize"; | ||||
import dayjs from "dayjs"; | ||||
import moment from "moment"; | ||||
import { dayjsToMoment, momentToDayjs } from "../../utils/time"; | ||||
import { | ||||
RenderMonthProps, | ||||
SingleDatePicker, | ||||
|
@@ -16,31 +18,63 @@ function isOutsideRange() { | |||
} | ||||
|
||||
export type DatePickerProps = Partial< | ||||
Omit<SingleDatePickerShape, "date" | "onFocusChange"> | ||||
Omit< | ||||
SingleDatePickerShape, | ||||
"date" | "onFocusChange" | "onDateChange" | "renderMonthText" | ||||
> | ||||
> & | ||||
// MEMO: Add RenderMonthProps to pass type check. | ||||
RenderMonthProps & { | ||||
date: moment.Moment | null; | ||||
onDateChange: (date: moment.Moment | null) => void; | ||||
Omit<RenderMonthProps, "renderMonthText"> & { | ||||
date: dayjs.Dayjs | null; | ||||
onDateChange: (date: dayjs.Dayjs | null) => void; | ||||
renderMonthText?: ((month: dayjs.Dayjs) => React.ReactNode) | null; | ||||
locale?: string; | ||||
weekdaysShort?: string[]; | ||||
error?: boolean; | ||||
}; | ||||
|
||||
const DatePicker = React.forwardRef<HTMLDivElement, DatePickerProps>( | ||||
(inProps, ref) => { | ||||
const props = useLocaleProps({ props: inProps, name: "DatePicker" }); | ||||
const { date, error = false, ...rest } = props; | ||||
const { | ||||
date, | ||||
error = false, | ||||
onDateChange, | ||||
renderMonthText: renderMonthTextProps, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
renderMonthElement, | ||||
locale = "en", | ||||
weekdaysShort, | ||||
...rest | ||||
} = props; | ||||
|
||||
const [focused, setFocused] = React.useState<boolean>(false); | ||||
const onFocusChange = ({ focused }: { focused: boolean }) => { | ||||
setFocused(focused); | ||||
}; | ||||
const handleDateChange = (date: moment.Moment | null) => { | ||||
const dayjsize = momentToDayjs(date); | ||||
onDateChange(dayjsize); | ||||
}; | ||||
const handleRenderMonthText = (month: moment.Moment) => { | ||||
const dayjsize = momentToDayjs(month); | ||||
if (!renderMonthTextProps || !dayjsize) return; | ||||
return renderMonthTextProps(dayjsize); | ||||
}; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nits There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そこまで気にすることでもないので falsy に関しては任せます |
||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SingleDatePickerはmomentなので、外側でdayjsでlocaleを設定しても影響しない。 |
||||
if (weekdaysShort) { | ||||
moment.locale(locale, { | ||||
weekdaysShort: weekdaysShort, | ||||
}); | ||||
} else { | ||||
moment.locale(locale); | ||||
} | ||||
|
||||
return ( | ||||
<Styled.Container ref={ref} error={error}> | ||||
<SingleDatePicker | ||||
id="datePicker" | ||||
focused={focused} | ||||
date={date} | ||||
date={dayjsToMoment(date)} | ||||
isOutsideRange={isOutsideRange} | ||||
numberOfMonths={1} | ||||
enableOutsideDays={true} | ||||
|
@@ -61,7 +95,13 @@ const DatePicker = React.forwardRef<HTMLDivElement, DatePickerProps>( | |||
</Spacer> | ||||
</Styled.NavNext> | ||||
} | ||||
// eslint-disable-next-line react/jsx-handler-names | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
renderMonthText={ | ||||
renderMonthTextProps ? handleRenderMonthText : renderMonthTextProps | ||||
} | ||||
renderMonthElement={renderMonthElement as never} | ||||
onFocusChange={onFocusChange} | ||||
onDateChange={handleDateChange} | ||||
{...rest} | ||||
/> | ||||
</Styled.Container> | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import dayjs from "dayjs"; | ||
import moment from "moment"; | ||
|
||
// moment.jsで動いているreact-datesと互換性を持たせるためのメソッド | ||
// DatePickerとDateRangePickerのリニューアルが完了したら除去する | ||
export function dayjsToMoment(date: dayjs.Dayjs | null): moment.Moment | null { | ||
if (!date) return null; | ||
const dateString = date.format(); | ||
return moment(dateString); | ||
} | ||
|
||
export function momentToDayjs(date: moment.Moment | null): dayjs.Dayjs | null { | ||
if (!date) return null; | ||
const dateString = date.format(); | ||
return dayjs(dateString); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6446,6 +6446,11 @@ dayjs@^1.10.4: | |
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.3.tgz#4754eb694a624057b9ad2224b67b15d552589258" | ||
integrity sha512-xxwlswWOlGhzgQ4TKzASQkUhqERI3egRNqgV4ScR8wlANA/A9tZ7miXa44vTTKEq5l7vWoL5G57bG3zA+Kow0A== | ||
|
||
dayjs@^1.11.6: | ||
version "1.11.6" | ||
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.6.tgz#2e79a226314ec3ec904e3ee1dd5a4f5e5b1c7afb" | ||
integrity sha512-zZbY5giJAinCG+7AGaw0wIhNZ6J8AhWuSXKvuc1KAyMiRsvGQWqh4L+MomvhdAYjN+lqvVCMq1I41e3YHvXkyQ== | ||
|
||
[email protected], debug@^2.2.0, debug@^2.3.3, debug@^2.6.0, debug@^2.6.9: | ||
version "2.6.9" | ||
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この差分はミスってるだけですかね?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ミスですね