-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADM-171; (Feat) DateRangePicker (#104)
* ADM-171; (Feat) Add DateRangePicker and DateRangePickerInput --------- Co-authored-by: Viachaslau Biziukin <[email protected]> Co-authored-by: Arthur Volokhin <[email protected]>
- Loading branch information
1 parent
d9a1b58
commit 622289c
Showing
37 changed files
with
21,501 additions
and
19,550 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
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,61 @@ | ||
import React, { useCallback } from 'react' | ||
import { useForm } from '../FormContext' | ||
import { Form } from '../Form' | ||
import { RangePicker } from '../../ui' | ||
import { PickerRangeProps } from '../../ui/DatePicker/generatePicker/interfaces' | ||
import { FormItemProps } from '../Item' | ||
import parseISO from 'date-fns/parseISO' | ||
import { InputComponentWithName } from '../interfaces' | ||
import { usePopupContainer } from '../../crud/PopupContainerContext' | ||
import { RangeValue } from 'rc-picker/lib/interface' | ||
|
||
export type DateRangePickerInputProps = FormItemProps & { | ||
name: string | ||
onChange?: (value: any) => void | ||
showTime?: boolean | ||
} & PickerRangeProps<Date> | ||
|
||
export const DateRangePickerInput: InputComponentWithName<React.FC<DateRangePickerInputProps>> = ({ | ||
name, | ||
label, | ||
required, | ||
columnSpan, | ||
onChange, | ||
...pickerProps | ||
}) => { | ||
const getPopupContainer = usePopupContainer() | ||
|
||
const { values, errors, setValues, locale: formLocale } = useForm() | ||
const locale = formLocale.fields.datePicker | ||
|
||
let value = values[name] | ||
? values[name].map((rangeValue: string | Date) => | ||
typeof rangeValue === 'string' ? parseISO(rangeValue) : rangeValue, | ||
) | ||
: undefined | ||
|
||
const error = errors[name]?.[0] | ||
|
||
const _onChange = useCallback( | ||
(changeValues: RangeValue<Date>, formatString: [string, string]) => { | ||
setValues((prevValues: any) => ({ ...prevValues, [name]: changeValues })) | ||
onChange?.(changeValues) | ||
}, | ||
[onChange], | ||
) | ||
|
||
return ( | ||
<Form.Item label={label} required={required} error={error} columnSpan={columnSpan}> | ||
<RangePicker | ||
getPopupContainer={getPopupContainer} | ||
locale={locale} | ||
{...pickerProps} | ||
value={value} | ||
onChange={_onChange} | ||
alert={!!error} | ||
/> | ||
</Form.Item> | ||
) | ||
} | ||
|
||
DateRangePickerInput.inputName = 'DateRangePickerInput' |
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
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
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
admiral/ui/DatePicker/generatePicker/generateRangePicker.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { GenerateConfig } from 'rc-picker/lib/generate/index' | ||
import '../DatePicker.scss' | ||
import generateSingleRangePicker from './generateSingleRangePicker' | ||
|
||
function generateRangePicker<DateType>(generateConfig: GenerateConfig<DateType>) { | ||
const { DateRangePicker } = generateSingleRangePicker(generateConfig) | ||
return DateRangePicker | ||
} | ||
|
||
export default generateRangePicker |
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
163 changes: 163 additions & 0 deletions
163
admiral/ui/DatePicker/generatePicker/generateSingleRangePicker.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,163 @@ | ||
import React from 'react' | ||
import cn from 'classnames' | ||
import { Button } from '../..' | ||
import { | ||
FiCalendar, | ||
FiClock, | ||
FiChevronLeft, | ||
FiChevronRight, | ||
FiChevronsLeft, | ||
FiChevronsRight, | ||
} from 'react-icons/fi' | ||
import { AiFillCloseCircle } from 'react-icons/ai' | ||
import { RangePicker } from 'rc-picker' | ||
import { PickerMode } from 'rc-picker/lib/interface' | ||
import { GenerateConfig } from 'rc-picker/lib/generate/index' | ||
import enUs from '../locale/en_US' | ||
import { getRangeTimeProps } from './getRangeTimeProps' | ||
import PickerButton from '../PickerButton' | ||
import PickerTag from '../PickerTag' | ||
import { PickerComponentClass, PickerRangeProps } from './interfaces' | ||
|
||
const defaultLocale = enUs | ||
|
||
export default function generateSingleRangePicker<DateType>( | ||
generateConfig: GenerateConfig<DateType>, | ||
) { | ||
type DateRangePickerProps = PickerRangeProps<DateType> | ||
|
||
function getPicker<InnerPickerProps extends DateRangePickerProps>( | ||
picker?: PickerMode, | ||
displayName?: string, | ||
) { | ||
class Picker extends React.Component<InnerPickerProps> { | ||
static displayName: string | ||
pickerRef = React.createRef<RangePicker<DateType>>() | ||
|
||
focus = () => { | ||
if (this.pickerRef.current) { | ||
this.pickerRef.current.focus() | ||
} | ||
} | ||
|
||
blur = () => { | ||
if (this.pickerRef.current) { | ||
this.pickerRef.current.blur() | ||
} | ||
} | ||
|
||
renderPicker = () => { | ||
const { | ||
getPopupContainer: customizeGetPopupContainer, | ||
className, | ||
size, | ||
borderless = false, | ||
alert, | ||
locale, | ||
separator = '', | ||
...restProps | ||
} = this.props | ||
const pickerLocale = { ...defaultLocale, ...locale } | ||
const { format, showTime } = this.props as any | ||
const prefixCls = cn('admiral-picker') | ||
const getPopupContainer = () => | ||
document.querySelector('#root > .Theme') as HTMLDivElement | ||
|
||
const additionalProps = { | ||
showToday: true, | ||
} | ||
|
||
let additionalOverrideProps: any = {} | ||
if (picker) { | ||
additionalOverrideProps.picker = picker | ||
} | ||
const mergedPicker = picker || this.props.picker | ||
|
||
additionalOverrideProps = { | ||
...additionalOverrideProps, | ||
...(showTime | ||
? getRangeTimeProps({ format, picker: mergedPicker, ...showTime }) | ||
: {}), | ||
...(mergedPicker === 'time' | ||
? getRangeTimeProps({ format, ...this.props, picker: mergedPicker }) | ||
: {}), | ||
} | ||
|
||
return ( | ||
<RangePicker<DateType> | ||
ref={this.pickerRef} | ||
placeholder={this.props.placeholder || ['from', 'to']} | ||
suffixIcon={mergedPicker === 'time' ? <FiClock /> : <FiCalendar />} | ||
clearIcon={<AiFillCloseCircle />} | ||
separator={separator} | ||
prevIcon={ | ||
<Button | ||
component="span" | ||
view="clear" | ||
size="S" | ||
iconLeft={<FiChevronLeft />} | ||
/> | ||
} | ||
nextIcon={ | ||
<Button | ||
component="span" | ||
view="clear" | ||
size="S" | ||
iconLeft={<FiChevronRight />} | ||
/> | ||
} | ||
superPrevIcon={ | ||
<Button | ||
component="span" | ||
view="clear" | ||
size="S" | ||
iconLeft={<FiChevronsLeft />} | ||
/> | ||
} | ||
superNextIcon={ | ||
<Button | ||
component="span" | ||
view="clear" | ||
size="S" | ||
iconLeft={<FiChevronsRight />} | ||
/> | ||
} | ||
allowClear | ||
// transitionName="admiral-picker-dropdown-slide-up" | ||
{...additionalProps} | ||
{...restProps} | ||
{...additionalOverrideProps} | ||
locale={pickerLocale.lang} | ||
className={cn( | ||
{ | ||
[`${prefixCls}__SizeL`]: size === 'L', | ||
[`${prefixCls}__SizeS`]: size === 'S', | ||
[`${prefixCls}__SizeXS`]: size === 'XS', | ||
[`${prefixCls}__Alert`]: alert, | ||
[`${prefixCls}__Borderless`]: borderless, | ||
}, | ||
className, | ||
)} | ||
prefixCls={prefixCls} | ||
getPopupContainer={customizeGetPopupContainer || getPopupContainer} | ||
generateConfig={generateConfig} | ||
components={{ button: PickerButton, rangeItem: PickerTag }} | ||
/> | ||
) | ||
} | ||
|
||
render() { | ||
return <>{this.renderPicker()}</> | ||
} | ||
} | ||
|
||
if (displayName) { | ||
Picker.displayName = displayName | ||
} | ||
|
||
return Picker as PickerComponentClass<InnerPickerProps> | ||
} | ||
|
||
const DateRangePicker = getPicker<DateRangePickerProps>() | ||
return { 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,44 @@ | ||
import { PickerMode } from 'rc-picker/lib/interface' | ||
import { SharedTimeProps } from 'rc-picker/lib/panels/TimePanel' | ||
import { toArray } from './interfaces' | ||
|
||
export function getRangeTimeProps<DateType>( | ||
props: { | ||
format?: string | ||
picker?: PickerMode | ||
} & Omit<SharedTimeProps<DateType>, 'defaultValue' | 'disabledTime'>, | ||
) { | ||
const { format, picker, showHour, showMinute, showSecond, use12Hours } = props | ||
|
||
const firstFormat = toArray(format)[0] | ||
const showTimeObj: SharedTimeProps<DateType> = { ...props } | ||
|
||
if (firstFormat && typeof firstFormat === 'string') { | ||
if (!firstFormat.includes('s') && showSecond === undefined) { | ||
showTimeObj.showSecond = false | ||
} | ||
if (!firstFormat.includes('m') && showMinute === undefined) { | ||
showTimeObj.showMinute = false | ||
} | ||
if (!firstFormat.includes('H') && !firstFormat.includes('h') && showHour === undefined) { | ||
showTimeObj.showHour = false | ||
} | ||
|
||
if ((firstFormat.includes('a') || firstFormat.includes('A')) && use12Hours === undefined) { | ||
showTimeObj.use12Hours = true | ||
} | ||
} | ||
|
||
if (picker === 'time') { | ||
return showTimeObj | ||
} | ||
|
||
if (typeof firstFormat === 'function') { | ||
// format of showTime should use default when format is custom format function | ||
delete showTimeObj.format | ||
} | ||
|
||
return { | ||
showTime: showTimeObj, | ||
} | ||
} |
Oops, something went wrong.