-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from chenz24/feat/DatePicker
feat: Add DatePicker component
- Loading branch information
Showing
18 changed files
with
670 additions
and
3 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
3 changes: 3 additions & 0 deletions
3
packages/components/src/ConfigProvider/LocaleProvider/types.ts
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,5 +1,8 @@ | ||
import { ModalLocale } from '../../Modal/locale'; | ||
// eslint-disable-next-line import/no-cycle | ||
import { PickerLocale } from '../../DatePicker/generatePicker'; | ||
|
||
export interface ILocale { | ||
Modal?: ModalLocale; | ||
DatePicker?: PickerLocale; | ||
} |
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,11 +1,15 @@ | ||
// eslint-disable-next-line import/no-cycle | ||
import { ILocale } from '../LocaleProvider/types'; | ||
// eslint-disable-next-line import/no-cycle | ||
import PickerLocale from '../../DatePicker/locales/en_US'; | ||
|
||
const localeValues: ILocale = { | ||
Modal: { | ||
okText: 'OK', | ||
cancelText: 'Cancel', | ||
justOkText: 'OK', | ||
}, | ||
DatePicker: PickerLocale, | ||
}; | ||
|
||
export default localeValues; |
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,10 @@ | ||
import * as React from 'react'; | ||
import DatePicker from './DatePicker'; | ||
|
||
// const { MonthPicker } = DatePicker; | ||
|
||
export default { | ||
title: 'Components/DatePicker', | ||
}; | ||
|
||
export const Basic = () => <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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// https://github.com/ant-design/ant-design/blob/master/components/date-picker/index.tsx | ||
import { Dayjs } from 'dayjs'; | ||
import dayjsGenerateConfig from 'rc-picker/es/generate/dayjs'; | ||
// eslint-disable-next-line import/no-cycle | ||
import generatePicker, { | ||
PickerProps, | ||
PickerDateProps, | ||
RangePickerProps as BaseRangePickerProps, | ||
} from './generatePicker'; | ||
|
||
export type DatePickerProps = PickerProps<Dayjs>; | ||
export type MonthPickerProps = Omit<PickerDateProps<Dayjs>, 'picker'>; | ||
export type WeekPickerProps = Omit<PickerDateProps<Dayjs>, 'picker'>; | ||
export type RangePickerProps = BaseRangePickerProps<Dayjs>; | ||
|
||
const DatePicker = generatePicker<Dayjs>(dayjsGenerateConfig); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import React from 'react'; | ||
import { Button, ButtonProps } from '../Button/Button'; | ||
|
||
export default function PickerButton(props: ButtonProps) { | ||
return <Button size="sm" {...props} />; | ||
} |
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,6 @@ | ||
import React from 'react'; | ||
import { Tag, TagProps } from '../Tag/Tag'; | ||
|
||
export default function PickerTag(props: TagProps) { | ||
return <Tag {...props} />; | ||
} |
190 changes: 190 additions & 0 deletions
190
packages/components/src/DatePicker/generatePicker/generateRangePicker.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,190 @@ | ||
import React, { FC, useImperativeHandle, useRef } from 'react'; | ||
import classNames from 'classnames'; | ||
import Calendar from '@kubed/icons/dist/calendar'; | ||
import Clock from '@kubed/icons/dist/clock'; | ||
import Close from '@kubed/icons/dist/close'; | ||
import Next from '@kubed/icons/dist/next'; | ||
import { RangePicker as RCRangePicker } from 'rc-picker'; | ||
import { GenerateConfig } from 'rc-picker/lib/generate'; | ||
import { getRangePlaceholder } from '../util'; | ||
import { RangePickerProps, getTimeProps, Components } from '.'; | ||
// import { CommonPickerMethods } from './interface'; | ||
import forwardRef from '../../utils/forwardRef'; | ||
import { useLocales } from '../../ConfigProvider/LocaleProvider/LocaleContext'; | ||
|
||
export default function generateRangePicker<DateType>( | ||
generateConfig: GenerateConfig<DateType> | ||
): FC<RangePickerProps<DateType>> { | ||
// class RangePicker2 extends React.Component<RangePickerProps<DateType>> { | ||
// static contextType = ConfigContext; | ||
// | ||
// context: ConfigConsumerProps; | ||
// | ||
// pickerRef = React.createRef<RCRangePicker<DateType>>(); | ||
// | ||
// focus = () => { | ||
// if (this.pickerRef.current) { | ||
// this.pickerRef.current.focus(); | ||
// } | ||
// }; | ||
// | ||
// blur = () => { | ||
// if (this.pickerRef.current) { | ||
// this.pickerRef.current.blur(); | ||
// } | ||
// }; | ||
// | ||
// renderPicker = (contextLocale: PickerLocale) => { | ||
// const locale = { ...contextLocale, ...this.props.locale }; | ||
// const { getPrefixCls, direction, getPopupContainer } = this.context; | ||
// const { | ||
// prefixCls: customizePrefixCls, | ||
// getPopupContainer: customGetPopupContainer, | ||
// className, | ||
// size: customizeSize, | ||
// bordered = true, | ||
// placeholder, | ||
// ...restProps | ||
// } = this.props; | ||
// const { format, showTime, picker } = this.props as any; | ||
// const prefixCls = getPrefixCls('picker', customizePrefixCls); | ||
// | ||
// let additionalOverrideProps: any = {}; | ||
// | ||
// additionalOverrideProps = { | ||
// ...additionalOverrideProps, | ||
// ...(showTime ? getTimeProps({ format, picker, ...showTime }) : {}), | ||
// ...(picker === 'time' ? getTimeProps({ format, ...this.props, picker }) : {}), | ||
// }; | ||
// const rootPrefixCls = getPrefixCls(); | ||
// | ||
// return ( | ||
// <SizeContext.Consumer> | ||
// {(size) => { | ||
// const mergedSize = customizeSize || size; | ||
// | ||
// return ( | ||
// <RCRangePicker<DateType> | ||
// separator={ | ||
// <span aria-label="to" className={`${prefixCls}-separator`}> | ||
// <Next /> | ||
// </span> | ||
// } | ||
// ref={this.pickerRef} | ||
// placeholder={getRangePlaceholder(picker, locale, placeholder)} | ||
// suffixIcon={picker === 'time' ? <Clock /> : <Calendar />} | ||
// clearIcon={<Close />} | ||
// prevIcon={<span className={`${prefixCls}-prev-icon`} />} | ||
// nextIcon={<span className={`${prefixCls}-next-icon`} />} | ||
// superPrevIcon={<span className={`${prefixCls}-super-prev-icon`} />} | ||
// superNextIcon={<span className={`${prefixCls}-super-next-icon`} />} | ||
// allowClear | ||
// transitionName={`${rootPrefixCls}-slide-up`} | ||
// {...restProps} | ||
// {...additionalOverrideProps} | ||
// className={classNames( | ||
// { | ||
// [`${prefixCls}-${mergedSize}`]: mergedSize, | ||
// [`${prefixCls}-borderless`]: !bordered, | ||
// }, | ||
// className | ||
// )} | ||
// locale={locale!.lang} | ||
// prefixCls={prefixCls} | ||
// getPopupContainer={customGetPopupContainer || getPopupContainer} | ||
// generateConfig={generateConfig} | ||
// components={Components} | ||
// direction={direction} | ||
// /> | ||
// ); | ||
// }} | ||
// </SizeContext.Consumer> | ||
// ); | ||
// }; | ||
// | ||
// render() { | ||
// return ( | ||
// <LocaleReceiver componentName="DatePicker" defaultLocale={enUS}> | ||
// {this.renderPicker} | ||
// </LocaleReceiver> | ||
// ); | ||
// } | ||
// } | ||
|
||
const RangePicker = forwardRef<RangePickerProps<DateType>, any>((props, ref) => { | ||
const { | ||
prefixCls: customizePrefixCls, | ||
getPopupContainer: customGetPopupContainer, | ||
className, | ||
size: customizeSize, | ||
bordered = true, | ||
placeholder, | ||
...restProps | ||
} = props; | ||
const { format, showTime, picker } = props as any; | ||
const prefixCls = 'kubed-picker'; | ||
const rootPrefixCls = 'kubed-picker-root'; | ||
|
||
let additionalOverrideProps: any = {}; | ||
|
||
additionalOverrideProps = { | ||
...additionalOverrideProps, | ||
...(showTime ? getTimeProps({ format, picker, ...showTime }) : {}), | ||
...(picker === 'time' ? getTimeProps({ format, ...this.props, picker }) : {}), | ||
}; | ||
|
||
const pickerRef = useRef<RCRangePicker<DateType>>(); | ||
useImperativeHandle(ref, () => ({ | ||
focus: () => { | ||
if (pickerRef.current) { | ||
pickerRef.current.focus(); | ||
} | ||
}, | ||
blur: () => { | ||
if (pickerRef.current) { | ||
pickerRef.current.blur(); | ||
} | ||
}, | ||
})); | ||
|
||
const locale = useLocales(); | ||
const { DatePicker: locales } = locale; | ||
|
||
return ( | ||
<RCRangePicker<DateType> | ||
separator={ | ||
<span aria-label="to" className={`${prefixCls}-separator`}> | ||
<Next /> | ||
</span> | ||
} | ||
ref={pickerRef} | ||
placeholder={getRangePlaceholder(picker, locales, placeholder)} | ||
suffixIcon={picker === 'time' ? <Clock /> : <Calendar />} | ||
clearIcon={<Close />} | ||
prevIcon={<span className={`${prefixCls}-prev-icon`} />} | ||
nextIcon={<span className={`${prefixCls}-next-icon`} />} | ||
superPrevIcon={<span className={`${prefixCls}-super-prev-icon`} />} | ||
superNextIcon={<span className={`${prefixCls}-super-next-icon`} />} | ||
allowClear | ||
transitionName={`${rootPrefixCls}-slide-up`} | ||
{...restProps} | ||
{...additionalOverrideProps} | ||
className={classNames( | ||
{ | ||
// [`${prefixCls}-${mergedSize}`]: mergedSize, | ||
[`${prefixCls}-borderless`]: !bordered, | ||
}, | ||
className | ||
)} | ||
locale={locales!.lang} | ||
prefixCls={prefixCls} | ||
// getPopupContainer={customGetPopupContainer || getPopupContainer} | ||
generateConfig={generateConfig} | ||
components={Components} | ||
// direction={direction} | ||
/> | ||
); | ||
}); | ||
|
||
return RangePicker; | ||
} |
Oops, something went wrong.