2
0

🐛 (js) Enable prefill for date input

This commit is contained in:
Baptiste Arnaud
2023-03-13 10:17:16 +01:00
parent f9aef907e3
commit a66a1e8226
4 changed files with 16 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/js", "name": "@typebot.io/js",
"version": "0.0.25", "version": "0.0.26",
"description": "Javascript library to display typebots on your website", "description": "Javascript library to display typebots on your website",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -152,6 +152,7 @@ const Input = (props: {
<Match when={props.block.type === InputBlockType.DATE}> <Match when={props.block.type === InputBlockType.DATE}>
<DateForm <DateForm
options={props.block.options as DateInputOptions} options={props.block.options as DateInputOptions}
defaultValue={getPrefilledValue()}
onSubmit={onSubmit} onSubmit={onSubmit}
/> />
</Match> </Match>

View File

@ -7,10 +7,14 @@ import { parseReadableDate } from '../utils/parseReadableDate'
type Props = { type Props = {
onSubmit: (inputValue: InputSubmitContent) => void onSubmit: (inputValue: InputSubmitContent) => void
options?: DateInputOptions options?: DateInputOptions
defaultValue?: string
} }
export const DateForm = (props: Props) => { export const DateForm = (props: Props) => {
const [inputValues, setInputValues] = createSignal({ from: '', to: '' }) const [inputValues, setInputValues] = createSignal(
parseDefaultValue(props.defaultValue ?? '')
)
return ( return (
<div class="flex flex-col"> <div class="flex flex-col">
<div class="flex items-center"> <div class="flex items-center">
@ -50,6 +54,7 @@ export const DateForm = (props: Props) => {
'min-width': '100px', 'min-width': '100px',
'font-size': '16px', 'font-size': '16px',
}} }}
value={inputValues().from}
type={props.options?.hasTime ? 'datetime-local' : 'date'} type={props.options?.hasTime ? 'datetime-local' : 'date'}
onChange={(e) => onChange={(e) =>
setInputValues({ setInputValues({
@ -74,6 +79,7 @@ export const DateForm = (props: Props) => {
'min-width': '100px', 'min-width': '100px',
'font-size': '16px', 'font-size': '16px',
}} }}
value={inputValues().to}
type={props.options.hasTime ? 'datetime-local' : 'date'} type={props.options.hasTime ? 'datetime-local' : 'date'}
onChange={(e) => onChange={(e) =>
setInputValues({ setInputValues({
@ -98,3 +104,9 @@ export const DateForm = (props: Props) => {
</div> </div>
) )
} }
const parseDefaultValue = (defaultValue: string) => {
if (!defaultValue.includes('to')) return { from: defaultValue, to: '' }
const [from, to] = defaultValue.split(' to ')
return { from, to }
}

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/react", "name": "@typebot.io/react",
"version": "0.0.25", "version": "0.0.26",
"description": "React library to display typebots on your website", "description": "React library to display typebots on your website",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",