-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(static-date-picker): hide non-current month's dates (#1863)
* fix(date-range-picker): each select will trigger onSelect * feat(static-date-picker): hide non-current month's dates Co-authored-by: maxin <maxin@growingio.com>
- Loading branch information
Showing
3 changed files
with
64 additions
and
10 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
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 |
---|---|---|
@@ -1,31 +1,82 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
import React from 'react'; | ||
import { useLocale, usePrefixCls } from '@gio-design/utils'; | ||
import React, { useLayoutEffect, useRef } from 'react'; | ||
import { useControlledState, useLocale, usePrefixCls } from '@gio-design/utils'; | ||
import { LeftDoubleOutlined, LeftOutlined, RightOutlined, RightDoubleOutlined } from '@gio-design/icons'; | ||
import PickerPanel from 'rc-picker/lib/PickerPanel'; | ||
import generateDateFns from 'rc-picker/lib/generate/dateFns'; | ||
import { Locale } from 'rc-picker/lib/interface'; | ||
import { isEqual } from 'date-fns'; | ||
import defaultLocale from './locales/zh-CN'; | ||
import { StaticDatePickerProps } from './interfaces'; | ||
|
||
function DatePicker({ viewDate, ...restProps }: StaticDatePickerProps) { | ||
const OmittedCell: React.FC = () => { | ||
const spanRef = useRef<HTMLSpanElement>(null); | ||
useLayoutEffect(() => { | ||
// Remove title prop | ||
if (spanRef.current) { | ||
const parent = spanRef.current.parentElement; | ||
if (parent?.hasAttribute('title')) { | ||
parent.removeAttribute('title'); | ||
} | ||
} | ||
}, []); | ||
return <span ref={spanRef} />; | ||
}; | ||
|
||
const DatePicker: React.FC<StaticDatePickerProps> = ({ | ||
viewDate: viewDateProp, | ||
disabledDate: disabledDateProp, | ||
...restProps | ||
}) => { | ||
const locale = useLocale<Locale>('DatePicker') || defaultLocale; | ||
const [viewDate, setViewDate] = useControlledState(viewDateProp, new Date()); | ||
|
||
const prefixCls = usePrefixCls('picker'); | ||
|
||
const isSameYearAndDay = (currentDate: Date) => | ||
isEqual( | ||
new Date(currentDate.getFullYear(), currentDate.getMonth()), | ||
new Date(viewDate.getFullYear(), viewDate.getMonth()) | ||
); | ||
|
||
const omitOtherDate = (currentDate: Date) => { | ||
if (isSameYearAndDay(currentDate)) { | ||
return <div className={`${prefixCls}-cell-inner`}>{currentDate.getDate()}</div>; | ||
} | ||
return <OmittedCell />; | ||
}; | ||
|
||
const disabledDate = (currentDate: Date) => { | ||
if (!isSameYearAndDay(currentDate)) { | ||
return true; | ||
} | ||
if (typeof disabledDateProp === 'function') { | ||
return disabledDateProp(currentDate); | ||
} | ||
return false; | ||
}; | ||
|
||
return ( | ||
<PickerPanel<Date> | ||
data-testid="static-date-picker" | ||
dateRender={omitOtherDate} | ||
disabledDate={disabledDate} | ||
{...restProps} | ||
pickerValue={viewDate} | ||
onPickerValueChange={(date) => setViewDate(date)} | ||
locale={locale} | ||
prefixCls={usePrefixCls('picker')} | ||
prefixCls={prefixCls} | ||
picker="date" | ||
generateConfig={generateDateFns} | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
superPrevIcon={<LeftDoubleOutlined />} | ||
prevIcon={<LeftOutlined />} | ||
nextIcon={<RightOutlined />} | ||
superNextIcon={<RightDoubleOutlined />} | ||
/> | ||
); | ||
} | ||
}; | ||
|
||
DatePicker.displayName = 'DatePicker'; | ||
|
||
export default DatePicker; |
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
1fbcf75
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.
Successfully deployed to the following URLs: