-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
467996b
commit f2a5fb5
Showing
4 changed files
with
96 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react' | ||
import { DateRangeInput, DateRangeInputProps } from '.' | ||
import { Meta, Story } from '@storybook/react/types-6-0' | ||
|
||
export default { | ||
component: DateRangeInput, | ||
title: 'DateRangeInput' | ||
} as Meta | ||
|
||
const Template: Story<DateRangeInputProps> = args => ( | ||
<DateRangeInput {...args} /> | ||
) | ||
|
||
export const Default = Template.bind({}) | ||
Default.args = {} |
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,78 @@ | ||
import cn from 'classnames' | ||
import { DatePicker } from 'antd' | ||
import { MomentInputObject } from 'moment' | ||
import { formatTime, parseTime } from '../TimeInput/utils' | ||
import React, { FC } from 'react' | ||
|
||
const { RangePicker: AntDRangePicker } = DatePicker | ||
|
||
export interface DateRangeInputValue { | ||
end: number | ||
start: number | ||
} | ||
|
||
interface TimeProps { | ||
displayFormat?: string | ||
} | ||
|
||
export interface DateRangeInputProps { | ||
classes?: string[] | ||
displayFormat?: string | ||
onChange?: (value: DateRangeInputValue) => void | ||
/** | ||
* Whether to include time or not. Can either be boolean or an object | ||
*/ | ||
includeTime?: boolean | TimeProps | ||
value?: DateRangeInputValue | ||
} | ||
|
||
export const DateRangeInput: FC<DateRangeInputProps> = ({ | ||
classes = [], | ||
displayFormat = 'YYYY-MM-DD hh:mm A', | ||
onChange, | ||
includeTime = { displayFormat: 'hh:mm A' }, | ||
value | ||
}: DateRangeInputProps) => { | ||
if (value && !onChange) { | ||
throw new Error('Controlled inputs require an onChange prop') | ||
} | ||
|
||
let controlledCmpProps = {} | ||
|
||
if (onChange) { | ||
controlledCmpProps = { | ||
onChange: ( | ||
momentObjArr: [MomentInputObject, MomentInputObject] | ||
) => { | ||
onChange({ | ||
end: parseTime(momentObjArr[1], 'unix'), | ||
start: parseTime(momentObjArr[0], 'unix') | ||
}) | ||
} | ||
} | ||
|
||
if (value) { | ||
controlledCmpProps = { | ||
...controlledCmpProps, | ||
value: [ | ||
formatTime('unix', value.start), | ||
formatTime('unix', value.end) | ||
] | ||
} | ||
} | ||
} | ||
|
||
return ( | ||
<AntDRangePicker | ||
allowClear={false} | ||
className={cn(classes)} | ||
format={displayFormat} | ||
showTime={ | ||
typeof includeTime === 'boolean' | ||
? includeTime | ||
: { format: includeTime.displayFormat } | ||
} | ||
{...controlledCmpProps} | ||
/> | ||
) | ||
} |
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