Skip to content

Commit

Permalink
feat(inputs): improve input focus behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Aug 14, 2022
1 parent 008724a commit fd01793
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const SendButton = ({
}: SendButtonProps) => {
return (
<button
type="submit"
disabled={isDisabled || isLoading}
{...props}
className={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
TextInputBlock,
UrlInputBlock,
} from 'models'
import React, { FormEvent, useState } from 'react'
import React, { useRef, useState } from 'react'
import { InputSubmitContent } from '../../InputChatBlock'
import { SendButton } from '../SendButton'
import { TextInput } from './TextInputContent'
import { TextInput } from './TextInput'

type TextFormProps = {
block:
Expand All @@ -30,6 +30,7 @@ export const TextForm = ({
hasGuestAvatar,
}: TextFormProps) => {
const [inputValue, setInputValue] = useState(defaultValue ?? '')
const inputRef = useRef<HTMLInputElement | null>(null)

const isLongText = block.type === InputBlockType.TEXT && block.options?.isLong

Expand All @@ -42,29 +43,41 @@ export const TextForm = ({
setInputValue(inputValue)
}

const handleSubmit = (e: FormEvent) => {
e.preventDefault()
if (inputValue === '') return
onSubmit({ value: inputValue })
const checkIfInputIsValid = () =>
inputValue !== '' && inputRef.current?.reportValidity()

const submit = () => {
if (checkIfInputIsValid()) onSubmit({ value: inputValue })
}

const submitWhenEnter = (e: React.KeyboardEvent) => {
if (e.key === 'Enter') submit()
}

return (
<form
className="flex items-end justify-between rounded-lg pr-2 typebot-input w-full"
onSubmit={handleSubmit}
<div
className={
'flex items-end justify-between rounded-lg pr-2 typebot-input w-full'
}
data-testid="input"
autoComplete={block.type === InputBlockType.TEXT ? 'off' : 'on'}
style={{
marginRight: hasGuestAvatar ? '50px' : '0.5rem',
maxWidth: isLongText ? undefined : '350px',
}}
onKeyDown={submitWhenEnter}
>
<TextInput block={block} onChange={handleChange} value={inputValue} />
<TextInput
inputRef={inputRef}
block={block}
onChange={handleChange}
value={inputValue}
/>
<SendButton
label={block.options?.labels?.button ?? 'Send'}
isDisabled={inputValue === ''}
className="my-2 ml-2"
onClick={submit}
/>
</form>
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ import {
UrlInputBlock,
PhoneNumberInputBlock,
} from 'models'
import React, {
ChangeEvent,
ChangeEventHandler,
RefObject,
useEffect,
useRef,
} from 'react'
import React, { ChangeEvent, ChangeEventHandler } from 'react'
import PhoneInput, { Value, Country } from 'react-phone-number-input'
import { isMobile } from 'services/utils'

type TextInputProps = {
inputRef: React.RefObject<any>
block:
| TextInputBlock
| EmailInputBlock
Expand All @@ -27,13 +21,12 @@ type TextInputProps = {
onChange: (value: string) => void
}

export const TextInput = ({ block, value, onChange }: TextInputProps) => {
const inputRef = useRef<HTMLInputElement & HTMLTextAreaElement>(null)

useEffect(() => {
setTimeout(() => inputRef.current?.focus(), isMobile ? 500 : 0)
}, [])

export const TextInput = ({
inputRef,
block,
value,
onChange,
}: TextInputProps) => {
const handleInputChange = (
e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
) => onChange(e.target.value)
Expand All @@ -46,7 +39,7 @@ export const TextInput = ({ block, value, onChange }: TextInputProps) => {
case InputBlockType.TEXT: {
return block.options?.isLong ? (
<LongTextInput
ref={inputRef as unknown as RefObject<HTMLTextAreaElement>}
ref={inputRef}
value={value}
placeholder={
block.options?.labels?.placeholder ?? 'Type your answer...'
Expand All @@ -61,7 +54,6 @@ export const TextInput = ({ block, value, onChange }: TextInputProps) => {
block.options?.labels?.placeholder ?? 'Type your answer...'
}
onChange={handleInputChange}
autoComplete="new-typebot-answer-value"
/>
)
}
Expand Down Expand Up @@ -111,14 +103,14 @@ export const TextInput = ({ block, value, onChange }: TextInputProps) => {
case InputBlockType.PHONE: {
return (
<PhoneInput
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ref={inputRef as any}
ref={inputRef}
value={value}
onChange={handlePhoneNumberChange}
placeholder={
block.options.labels.placeholder ?? 'Your phone number...'
}
defaultCountry={block.options.defaultCountryCode as Country}
autoFocus
/>
)
}
Expand All @@ -136,6 +128,8 @@ const ShortTextInput = React.forwardRef(
type="text"
required
style={{ fontSize: '16px' }}
autoComplete="do-not-autofill"
autoFocus
{...props}
/>
)
Expand All @@ -157,6 +151,7 @@ const LongTextInput = React.forwardRef(
data-testid="textarea"
required
style={{ fontSize: '16px' }}
autoFocus
{...props}
/>
)
Expand Down

5 comments on commit fd01793

@vercel
Copy link

@vercel vercel bot commented on fd01793 Aug 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2-alpha – ./apps/viewer

247987.com
8jours.top
finplex.be
bot.aipr.kr
yobot.me
am.nigerias.io
vhpage.cr8.ai
bot.aws.bj
bt.id8rs.com
sat.cr8.ai
bot.ageenda.com
bot.artiweb.app
bot.lalmon.com
an.nigerias.io
apo.nigerias.io
ticketfute.com
aso.nigerias.io
ar.nigerias.io
apr.nigerias.io
chat.sureb4.com
games.klujo.com
eventhub.com.au
sakuranembro.it
bot.piccinato.co
feedback.ofx.one
clo.closeer.work
bot.upfunnel.art
typebot.aloe.do
form.syncwin.com
faqs.nigerias.io
kw.wpwakanda.com
rdhs.digienge.io
stan.vselise.com
voicehelp.cr8.ai
app.chatforms.net
bot.agfunnel.tech
typebot.aloe.bot
bot.maitempah.com
bot.phuonghub.com
fmm.wpwakanda.com
gentleman-shop.fr
ov1.wpwakanda.com
ov3.wpwakanda.com
lb.ticketfute.com
k1.kandabrand.com
ov2.wpwakanda.com
andreimayer.com.br
1988.bouclidom.com
cares.urlabout.me
bot.megafox.com.br
bot.neferlopez.com
goalsettingbot.com
survey.digienge.io
dicanatural.online
this-is-a-test.com
zap.techadviser.in
bot.digitalbled.com
rdhs06.digienge.io
bot.eventhub.com.au
forms.webisharp.com
carsalesenquiry.com
order.maitempah.com
survey1.digienge.io
typebot.stillio.com
quest.wpwakanda.com
bot.ansuraniphone.my
bot.cotemeuplano.com
bium.gratirabbit.com
connect.growthguy.in
chat.hayurihijab.com
click.sevenoways.com
get.freebotoffer.xyz
aidigitalmarketing.kr
bot.meuesocial.com.br
bbutton.wpwakanda.com
abutton.wpwakanda.com
bot.incusservices.com
cdd.searchcube.com.sg
sbutton.wpwakanda.com
apply.ansuraniphone.my
chat.missarkansas.org
bbutton.wpwwakanda.com
felipewelington.com.br
gcase.barrettamario.it
c23111azqw.nigerias.io
form.searchcube.com.sg
info.clickasuransi.com
report.gratirabbit.com
kodawariab736.skeep.it
resume.gratirabbit.com
view.onlinebotdemo.xyz
83242573.actualizar.xyz
bot.blackboxtips.com.br
bot.upgradesolutions.eu
signup.hypemarketing.in
mainmenu.diddancing.com
subfooter.wpwakanda.com
survey.hypemarketing.in
91181264.your-access.one
form.sergiolimajr.com.br

@vercel
Copy link

@vercel vercel bot commented on fd01793 Aug 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on fd01793 Aug 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on fd01793 Aug 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs-typebot-io.vercel.app
docs.typebot.io
docs-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on fd01793 Aug 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

app.typebot.io
builder-v2-typebot-io.vercel.app
builder-v2-git-main-typebot-io.vercel.app

Please sign in to comment.