Skip to content

Commit

Permalink
feat #314 - Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-dassana committed May 17, 2021
1 parent 467996b commit f2a5fb5
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/components/DateRangeInput/DateRangeInput.stories.tsx
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 = {}
78 changes: 78 additions & 0 deletions src/components/DateRangeInput/index.tsx
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}
/>
)
}
3 changes: 1 addition & 2 deletions src/components/TimeInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ export interface TimeInputProps
onChange?: (value: number) => void
onFocus?: (event: FocusEvent<HTMLInputElement>) => void
/**
* format of time input value. Either a unix timestamp in seconds or hour integer (0 - 24)
* format of time input value. Either a unix timestamp in milliseconds or hour integer (0 - 24)
* @default 'unix'
*/
format?: TimeFormat
/**
* Interval between minutes in dropdown
* @default 1
* */

minuteStep?: number
/**
* Selector of HTML element inside which to render the dropdown
Expand Down
4 changes: 2 additions & 2 deletions src/components/TimeInput/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const formatTime: FormatTime = (format, value) => {
if (isUndefined(value)) return value

if (format === 'unix') {
return moment.unix(value)
return moment(value)
}

return moment(value, hourIntegerFormat)
Expand All @@ -46,7 +46,7 @@ export const parseTime: ParseTime = (
format: TimeFormat
) =>
format === 'unix'
? moment(momentObj).unix()
? moment(momentObj).valueOf()
: parseInt(moment(momentObj).format(hourIntegerFormat))

// -x-x-x-x-x-x-x-x- Styles Related -x-x-x-x-x-x-x-x-
Expand Down

0 comments on commit f2a5fb5

Please sign in to comment.