2022-01-10 08:05:03 +01:00
|
|
|
import { DateInputOptions } from 'models'
|
|
|
|
import React, { useState } from 'react'
|
|
|
|
import { SendButton } from './SendButton'
|
|
|
|
|
|
|
|
type DateInputProps = {
|
|
|
|
onSubmit: (inputValue: `${string} to ${string}` | string) => void
|
|
|
|
options?: DateInputOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
export const DateForm = ({
|
|
|
|
onSubmit,
|
|
|
|
options,
|
|
|
|
}: DateInputProps): JSX.Element => {
|
|
|
|
const { hasTime, isRange, labels } = options ?? {}
|
|
|
|
const [inputValues, setInputValues] = useState({ from: '', to: '' })
|
|
|
|
return (
|
|
|
|
<div className="flex flex-col w-full lg:w-4/6">
|
|
|
|
<div className="flex items-center">
|
|
|
|
<form
|
|
|
|
className={
|
|
|
|
'flex justify-between rounded-lg typebot-input pr-2 items-end'
|
|
|
|
}
|
|
|
|
onSubmit={(e) => {
|
|
|
|
if (inputValues.from === '' && inputValues.to === '') return
|
|
|
|
e.preventDefault()
|
|
|
|
onSubmit(
|
|
|
|
`${inputValues.from}${isRange ? ` to ${inputValues.to}` : ''}`
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className="flex flex-col">
|
|
|
|
<div className={'flex items-center p-4 ' + (isRange ? 'pb-0' : '')}>
|
|
|
|
{isRange && (
|
|
|
|
<p className="font-semibold mr-2">{labels?.from ?? 'From:'}</p>
|
|
|
|
)}
|
|
|
|
<input
|
|
|
|
className="focus:outline-none bg-transparent flex-1 w-full comp-input"
|
|
|
|
type={hasTime ? 'datetime-local' : 'date'}
|
|
|
|
onChange={(e) =>
|
|
|
|
setInputValues({ ...inputValues, from: e.target.value })
|
|
|
|
}
|
|
|
|
data-testid="from-date"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{isRange && (
|
|
|
|
<div className="flex items-center p-4">
|
|
|
|
{isRange && (
|
|
|
|
<p className="font-semibold">{labels?.to ?? 'To:'}</p>
|
|
|
|
)}
|
|
|
|
<input
|
|
|
|
className="focus:outline-none bg-transparent flex-1 w-full comp-input ml-2"
|
|
|
|
type={hasTime ? 'datetime-local' : 'date'}
|
|
|
|
onChange={(e) =>
|
|
|
|
setInputValues({ ...inputValues, to: e.target.value })
|
|
|
|
}
|
|
|
|
data-testid="to-date"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<SendButton
|
|
|
|
label={labels?.button ?? 'Send'}
|
|
|
|
isDisabled={inputValues.to === '' && inputValues.from === ''}
|
2022-01-12 09:10:59 +01:00
|
|
|
className="my-2 ml-2"
|
2022-01-10 08:05:03 +01:00
|
|
|
/>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|