Skip to content

Commit

Permalink
feat(engine): ♿️ Smarter website input
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Feb 28, 2022
1 parent 8552cc2 commit c5b378d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
2 changes: 0 additions & 2 deletions apps/builder/playwright/tests/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ test.describe.parallel('Editor', () => {
await expect(page.locator('[data-testid="step"] >> nth=2')).toHaveText(
'Hello!'
)
await page.dragAndDrop('text=Hello', '[data-testid="step"] >> text=Start')
await expect(page.locator('text=Block #4')).toBeVisible()
await page.dragAndDrop('text=Hello', 'text=Block #2')
await expect(page.locator('[data-testid="step"] >> nth=3')).toHaveText(
'Hello!'
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/playwright/tests/inputs/url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test.describe('Url input step', () => {
await page.click('text=Restart')
await typebotViewer(page)
.locator(`input[placeholder="Your URL..."]`)
.fill('gg://test.com')
.fill('https://https://test')
await typebotViewer(page).locator('button >> text="Go"').click()
await expect(
typebotViewer(page).locator('text=Try again bro')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
EmailInputStep,
InputStepType,
NumberInputStep,
PhoneNumberInputStep,
TextInputStep,
Expand All @@ -23,7 +24,14 @@ type TextFormProps = {
export const TextForm = ({ step, onSubmit, defaultValue }: TextFormProps) => {
const [inputValue, setInputValue] = useState(defaultValue ?? '')

const handleChange = (inputValue: string) => setInputValue(inputValue)
const handleChange = (inputValue: string) => {
if (step.type === InputStepType.URL && !inputValue.startsWith('https://'))
return inputValue === 'https:/'
? undefined
: setInputValue(`https://${inputValue}`)

setInputValue(inputValue)
}

const handleSubmit = (e: FormEvent) => {
e.preventDefault()
Expand All @@ -39,11 +47,7 @@ export const TextForm = ({ step, onSubmit, defaultValue }: TextFormProps) => {
onSubmit={handleSubmit}
data-testid="input"
>
<TextInput
step={step}
onChange={handleChange}
defaultValue={defaultValue ?? ''}
/>
<TextInput step={step} onChange={handleChange} value={inputValue} />
<SendButton
label={step.options?.labels?.button ?? 'Send'}
isDisabled={inputValue === ''}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ type TextInputProps = {
| NumberInputStep
| UrlInputStep
| PhoneNumberInputStep
defaultValue: string
value: string
onChange: (value: string) => void
}

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

useEffect(() => {
Expand All @@ -47,19 +47,19 @@ export const TextInput = ({ step, defaultValue, onChange }: TextInputProps) => {
return step.options?.isLong ? (
<LongTextInput
ref={inputRef as unknown as RefObject<HTMLTextAreaElement>}
value={value}
placeholder={
step.options?.labels?.placeholder ?? 'Type your answer...'
}
defaultValue={defaultValue}
onChange={handleInputChange}
/>
) : (
<ShortTextInput
ref={inputRef}
value={value}
placeholder={
step.options?.labels?.placeholder ?? 'Type your answer...'
}
defaultValue={defaultValue}
onChange={handleInputChange}
/>
)
Expand All @@ -68,10 +68,10 @@ export const TextInput = ({ step, defaultValue, onChange }: TextInputProps) => {
return (
<ShortTextInput
ref={inputRef}
value={value}
placeholder={
step.options?.labels?.placeholder ?? 'Type your email...'
}
defaultValue={defaultValue}
onChange={handleInputChange}
type="email"
/>
Expand All @@ -81,10 +81,10 @@ export const TextInput = ({ step, defaultValue, onChange }: TextInputProps) => {
return (
<ShortTextInput
ref={inputRef}
value={value}
placeholder={
step.options?.labels?.placeholder ?? 'Type your answer...'
}
defaultValue={defaultValue}
onChange={handleInputChange}
type="number"
style={{ appearance: 'auto' }}
Expand All @@ -98,8 +98,8 @@ export const TextInput = ({ step, defaultValue, onChange }: TextInputProps) => {
return (
<ShortTextInput
ref={inputRef}
value={value}
placeholder={step.options?.labels?.placeholder ?? 'Type your URL...'}
defaultValue={defaultValue}
onChange={handleInputChange}
type="url"
/>
Expand All @@ -110,8 +110,8 @@ export const TextInput = ({ step, defaultValue, onChange }: TextInputProps) => {
<PhoneInput
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ref={inputRef as any}
value={value}
onChange={handlePhoneNumberChange}
defaultValue={defaultValue}
placeholder={
step.options.labels.placeholder ?? 'Your phone number...'
}
Expand Down Expand Up @@ -141,7 +141,7 @@ const LongTextInput = React.forwardRef(
(
props: {
placeholder: string
defaultValue: string
value: string
onChange: ChangeEventHandler
},
ref: React.ForwardedRef<HTMLTextAreaElement>
Expand Down
2 changes: 1 addition & 1 deletion packages/bot-engine/src/services/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { parseVariables } from './variable'
const emailRegex =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
const urlRegex =
/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/
/^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)$/

export const isInputValid = (
inputValue: string,
Expand Down

0 comments on commit c5b378d

Please sign in to comment.