2
0

(embed) Add new command setInputValue

This commit is contained in:
Baptiste Arnaud
2023-07-15 17:38:12 +02:00
parent 521cb50782
commit be7be7bf7a
14 changed files with 93 additions and 13 deletions

View File

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

View File

@ -1,9 +1,10 @@
import { ShortTextInput } from '@/components'
import { SendButton } from '@/components/SendButton'
import { CommandData } from '@/features/commands/types'
import { InputSubmitContent } from '@/types'
import { isMobile } from '@/utils/isMobileSignal'
import type { EmailInputBlock } from '@typebot.io/schemas'
import { createSignal, onMount } from 'solid-js'
import { createSignal, onCleanup, onMount } from 'solid-js'
type Props = {
block: EmailInputBlock
@ -30,8 +31,19 @@ export const EmailInput = (props: Props) => {
onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
window.addEventListener('message', processIncomingEvent)
})
onCleanup(() => {
window.removeEventListener('message', processIncomingEvent)
})
const processIncomingEvent = (event: MessageEvent<CommandData>) => {
const { data } = event
if (!data.isFromTypebot) return
if (data.command === 'setInputValue') setInputValue(data.value)
}
return (
<div
class={'flex items-end justify-between pr-2 typebot-input w-full'}

View File

@ -1,9 +1,10 @@
import { ShortTextInput } from '@/components'
import { SendButton } from '@/components/SendButton'
import { CommandData } from '@/features/commands/types'
import { InputSubmitContent } from '@/types'
import { isMobile } from '@/utils/isMobileSignal'
import type { NumberInputBlock } from '@typebot.io/schemas'
import { createSignal, onMount } from 'solid-js'
import { createSignal, onCleanup, onMount } from 'solid-js'
type NumberInputProps = {
block: NumberInputBlock
@ -30,8 +31,19 @@ export const NumberInput = (props: NumberInputProps) => {
onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
window.addEventListener('message', processIncomingEvent)
})
onCleanup(() => {
window.removeEventListener('message', processIncomingEvent)
})
const processIncomingEvent = (event: MessageEvent<CommandData>) => {
const { data } = event
if (!data.isFromTypebot) return
if (data.command === 'setInputValue') setInputValue(data.value)
}
return (
<div
class={'flex items-end justify-between pr-2 typebot-input w-full'}

View File

@ -4,9 +4,10 @@ import { SendButton } from '@/components/SendButton'
import { InputSubmitContent } from '@/types'
import { isMobile } from '@/utils/isMobileSignal'
import type { PhoneNumberInputOptions } from '@typebot.io/schemas'
import { createSignal, For, onMount } from 'solid-js'
import { createSignal, For, onCleanup, onMount } from 'solid-js'
import { isEmpty } from '@typebot.io/lib'
import { phoneCountries } from '@typebot.io/lib/phoneCountries'
import { CommandData } from '@/features/commands/types'
type PhoneInputProps = Pick<
PhoneNumberInputOptions,
@ -33,7 +34,7 @@ export const PhoneInput = (props: PhoneInputProps) => {
const matchedCountry =
inputValue?.startsWith('+') &&
inputValue.length > 2 &&
phoneCountries.reduce<typeof phoneCountries[number] | null>(
phoneCountries.reduce<(typeof phoneCountries)[number] | null>(
(matchedCountry, country) => {
if (
!country?.dial_code ||
@ -87,8 +88,19 @@ export const PhoneInput = (props: PhoneInputProps) => {
onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
window.addEventListener('message', processIncomingEvent)
})
onCleanup(() => {
window.removeEventListener('message', processIncomingEvent)
})
const processIncomingEvent = (event: MessageEvent<CommandData>) => {
const { data } = event
if (!data.isFromTypebot) return
if (data.command === 'setInputValue') setInputValue(data.value)
}
return (
<div
class={'flex items-end justify-between pr-2 typebot-input'}

View File

@ -1,9 +1,10 @@
import { Textarea, ShortTextInput } from '@/components'
import { SendButton } from '@/components/SendButton'
import { CommandData } from '@/features/commands'
import { InputSubmitContent } from '@/types'
import { isMobile } from '@/utils/isMobileSignal'
import type { TextInputBlock } from '@typebot.io/schemas'
import { createSignal, onMount } from 'solid-js'
import { createSignal, onCleanup, onMount } from 'solid-js'
type Props = {
block: TextInputBlock
@ -36,8 +37,19 @@ export const TextInput = (props: Props) => {
onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
window.addEventListener('message', processIncomingEvent)
})
onCleanup(() => {
window.removeEventListener('message', processIncomingEvent)
})
const processIncomingEvent = (event: MessageEvent<CommandData>) => {
const { data } = event
if (!data.isFromTypebot) return
if (data.command === 'setInputValue') setInputValue(data.value)
}
return (
<div
class={'flex items-end justify-between pr-2 typebot-input w-full'}

View File

@ -1,9 +1,10 @@
import { ShortTextInput } from '@/components'
import { SendButton } from '@/components/SendButton'
import { CommandData } from '@/features/commands/types'
import { InputSubmitContent } from '@/types'
import { isMobile } from '@/utils/isMobileSignal'
import type { UrlInputBlock } from '@typebot.io/schemas'
import { createSignal, onMount } from 'solid-js'
import { createSignal, onCleanup, onMount } from 'solid-js'
type Props = {
block: UrlInputBlock
@ -36,8 +37,19 @@ export const UrlInput = (props: Props) => {
onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
window.addEventListener('message', processIncomingEvent)
})
onCleanup(() => {
window.removeEventListener('message', processIncomingEvent)
})
const processIncomingEvent = (event: MessageEvent<CommandData>) => {
const { data } = event
if (!data.isFromTypebot) return
if (data.command === 'setInputValue') setInputValue(data.value)
}
return (
<div
class={'flex items-end justify-between pr-2 typebot-input w-full'}

View File

@ -8,6 +8,7 @@ export type CommandData = {
}
| ShowMessageCommandData
| SetPrefilledVariablesCommandData
| SetInputValueCommandData
)
export type ShowMessageCommandData = {
@ -19,3 +20,8 @@ export type SetPrefilledVariablesCommandData = {
command: 'setPrefilledVariables'
variables: Record<string, string | number | boolean>
}
export type SetInputValueCommandData = {
command: 'setInputValue'
value: string
}

View File

@ -4,3 +4,4 @@ export * from './open'
export * from './setPrefilledVariables'
export * from './showPreviewMessage'
export * from './toggle'
export * from './setInputValue'

View File

@ -0,0 +1,10 @@
import { CommandData } from '../types'
export const setInputValue = (value: string) => {
const message: CommandData = {
isFromTypebot: true,
command: 'setInputValue',
value,
}
window.postMessage(message)
}

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/nextjs",
"version": "0.1.0",
"version": "0.1.1",
"description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js",
"types": "dist/index.d.ts",

View File

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

View File

@ -6,6 +6,7 @@ import {
showPreviewMessage,
hidePreviewMessage,
setPrefilledVariables,
setInputValue,
} from '@typebot.io/js'
import { useState } from 'react'
import { leadGenerationTypebot } from './assets/leadGenerationTypebot'
@ -23,6 +24,7 @@ export const Default = () => {
<button onClick={() => showPreviewMessage()}>
Show Preview Message
</button>
<button onClick={() => setInputValue('YOOOO!')}>Set input value</button>
<button onClick={hidePreviewMessage}>Close Preview Message</button>
<div>
<p>Predefined name:</p>