Skip to content

Commit

Permalink
feat(inputs): ✨ Add URL input
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jan 9, 2022
1 parent 8391bcc commit ce1b23a
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 9 deletions.
4 changes: 4 additions & 0 deletions apps/builder/components/board/StepTypesList/StepIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ChatIcon,
EmailIcon,
FlagIcon,
GlobeIcon,
NumberIcon,
TextIcon,
} from 'assets/icons'
Expand All @@ -24,6 +25,9 @@ export const StepIcon = ({ type }: StepIconProps) => {
case InputStepType.EMAIL: {
return <EmailIcon />
}
case InputStepType.URL: {
return <GlobeIcon />
}
case 'start': {
return <FlagIcon />
}
Expand Down
3 changes: 3 additions & 0 deletions apps/builder/components/board/StepTypesList/StepTypeLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export const StepTypeLabel = ({ type }: Props) => {
case InputStepType.EMAIL: {
return <Text>Email</Text>
}
case InputStepType.URL: {
return <Text>Website</Text>
}
default: {
return <></>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { InputStepType, Step, TextInputOptions } from 'models'
import { EmailInputSettingsBody } from './EmailInputSettingsBody'
import { NumberInputSettingsBody } from './NumberInputSettingsBody'
import { TextInputSettingsBody } from './TextInputSettingsBody'
import { UrlInputSettingsBody } from './UrlInputSettingsBody'

type Props = {
step: Step
Expand Down Expand Up @@ -51,6 +52,14 @@ const SettingsPopoverBodyContent = ({ step }: Props) => {
/>
)
}
case InputStepType.URL: {
return (
<UrlInputSettingsBody
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
default: {
return <></>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { FormLabel, Stack } from '@chakra-ui/react'
import { DebouncedInput } from 'components/shared/DebouncedInput'
import { UrlInputOptions } from 'models'
import React from 'react'

type UrlInputSettingsBodyProps = {
options?: UrlInputOptions
onOptionsChange: (options: UrlInputOptions) => void
}

export const UrlInputSettingsBody = ({
options,
onOptionsChange,
}: UrlInputSettingsBodyProps) => {
const handlePlaceholderChange = (placeholder: string) =>
onOptionsChange({ ...options, labels: { ...options?.labels, placeholder } })
const handleButtonLabelChange = (button: string) =>
onOptionsChange({ ...options, labels: { ...options?.labels, button } })

return (
<Stack spacing={4}>
<Stack>
<FormLabel mb="0" htmlFor="placeholder">
Placeholder:
</FormLabel>
<DebouncedInput
id="placeholder"
initialValue={options?.labels?.placeholder ?? 'Type your URL...'}
delay={100}
onChange={handlePlaceholderChange}
/>
</Stack>
<Stack>
<FormLabel mb="0" htmlFor="button">
Button label:
</FormLabel>
<DebouncedInput
id="button"
initialValue={options?.labels?.button ?? 'Send'}
delay={100}
onChange={handleButtonLabelChange}
/>
</Stack>
</Stack>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export const StepNodeLabel = (props: Step | StartStep) => {
</Text>
)
}
case InputStepType.URL: {
return (
<Text color={'gray.500'}>
{props.options?.labels?.placeholder ?? 'Type your URL...'}
</Text>
)
}
case 'start': {
return <Text>{props.label}</Text>
}
Expand Down
49 changes: 44 additions & 5 deletions apps/builder/cypress/tests/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ describe('Text input', () => {
cy.signIn('test2@gmail.com')
cy.visit('/typebots/typebot3/edit')
cy.findByRole('button', { name: 'Preview' }).click()
getIframeBody().findByPlaceholderText('Type your answer...').should('exist')
getIframeBody().findByRole('button', { name: 'Send' }).should('exist')
getIframeBody()
.findByPlaceholderText('Type your answer...')
.should('have.attr', 'type')
.should('equal', 'text')
getIframeBody().findByRole('button', { name: 'Send' }).should('be.disabled')
cy.findByTestId('step-step1').click({ force: true })
cy.findByRole('textbox', { name: 'Placeholder:' })
.clear()
Expand Down Expand Up @@ -50,8 +53,11 @@ describe('Number input', () => {
cy.signIn('test2@gmail.com')
cy.visit('/typebots/typebot3/edit')
cy.findByRole('button', { name: 'Preview' }).click()
getIframeBody().findByPlaceholderText('Type your answer...').should('exist')
getIframeBody().findByRole('button', { name: 'Send' }).should('exist')
getIframeBody()
.findByPlaceholderText('Type your answer...')
.should('have.attr', 'type')
.should('equal', 'number')
getIframeBody().findByRole('button', { name: 'Send' }).should('be.disabled')
cy.findByTestId('step-step1').click({ force: true })
cy.findByRole('textbox', { name: 'Placeholder:' })
.clear()
Expand Down Expand Up @@ -84,8 +90,13 @@ describe('Email input', () => {
cy.signIn('test2@gmail.com')
cy.visit('/typebots/typebot3/edit')
cy.findByRole('button', { name: 'Preview' }).click()
getIframeBody().findByPlaceholderText('Type your email...').should('exist')
getIframeBody()
.findByPlaceholderText('Type your email...')
.should('have.attr', 'type')
.should('equal', 'email')
getIframeBody().findByRole('button', { name: 'Send' })
getIframeBody().findByPlaceholderText('Type your email...').should('exist')
getIframeBody().findByRole('button', { name: 'Send' }).should('be.disabled')
cy.findByTestId('step-step1').click({ force: true })
cy.findByRole('textbox', { name: 'Placeholder:' })
.clear()
Expand All @@ -98,6 +109,34 @@ describe('Email input', () => {
})
})

describe('URL input', () => {
beforeEach(() => {
cy.task('seed')
createTypebotWithStep({ type: InputStepType.URL })
cy.signOut()
})

it('options should work', () => {
cy.signIn('test2@gmail.com')
cy.visit('/typebots/typebot3/edit')
cy.findByRole('button', { name: 'Preview' }).click()
getIframeBody()
.findByPlaceholderText('Type your URL...')
.should('have.attr', 'type')
.should('eq', 'url')
getIframeBody().findByRole('button', { name: 'Send' }).should('be.disabled')
cy.findByTestId('step-step1').click({ force: true })
cy.findByRole('textbox', { name: 'Placeholder:' })
.clear()
.type('Your URL...')
cy.findByRole('textbox', { name: 'Button label:' }).clear().type('Go')
cy.findByTestId('step-step1').should('contain.text', 'Your URL...')
cy.findByRole('button', { name: 'Restart' }).click()
getIframeBody().findByPlaceholderText('Your URL...').should('exist')
getIframeBody().findByRole('button', { name: 'Go' })
})
})

const createTypebotWithStep = (step: Omit<InputStep, 'id' | 'blockId'>) => {
cy.task(
'createTypebot',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const InputChatStep = ({
case InputStepType.TEXT:
case InputStepType.NUMBER:
case InputStepType.EMAIL:
case InputStepType.URL:
return <TextForm step={step} onSubmit={handleSubmit} />
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { EmailInputStep, NumberInputStep, TextInputStep } from 'models'
import {
EmailInputStep,
NumberInputStep,
TextInputStep,
UrlInputStep,
} from 'models'
import React, { FormEvent, useState } from 'react'
import { SendIcon } from '../../../../../assets/icons'
import { TextInput } from './TextInputContent'

type TextFormProps = {
step: TextInputStep | EmailInputStep | NumberInputStep
step: TextInputStep | EmailInputStep | NumberInputStep | UrlInputStep
onSubmit: (value: string) => void
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
EmailInputStep,
NumberInputStep,
InputStepType,
UrlInputStep,
} from 'models'
import React, {
ChangeEvent,
Expand All @@ -13,7 +14,7 @@ import React, {
} from 'react'

type TextInputProps = {
step: TextInputStep | EmailInputStep | NumberInputStep
step: TextInputStep | EmailInputStep | NumberInputStep | UrlInputStep
onChange: (value: string) => void
}

Expand Down Expand Up @@ -77,6 +78,16 @@ export const TextInput = ({ step, onChange }: TextInputProps) => {
/>
)
}
case InputStepType.URL: {
return (
<ShortTextInput
ref={inputRef}
placeholder={step.options?.labels?.placeholder ?? 'Type your URL...'}
onChange={handleInputChange}
type="url"
/>
)
}
}
}

Expand Down
14 changes: 13 additions & 1 deletion packages/models/src/typebot/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ export type Step = StartStep | BubbleStep | InputStep

export type BubbleStep = TextStep

export type InputStep = TextInputStep | NumberInputStep | EmailInputStep
export type InputStep =
| TextInputStep
| NumberInputStep
| EmailInputStep
| UrlInputStep

export type StepType = 'start' | BubbleStepType | InputStepType

Expand All @@ -14,6 +18,7 @@ export enum InputStepType {
TEXT = 'text input',
NUMBER = 'number input',
EMAIL = 'email input',
URL = 'url input',
}

export type StepBase = { id: string; blockId: string; target?: Target }
Expand Down Expand Up @@ -43,8 +48,15 @@ export type EmailInputStep = StepBase & {
options?: EmailInputOptions
}

export type UrlInputStep = StepBase & {
type: InputStepType.URL
options?: UrlInputOptions
}

export type EmailInputOptions = InputOptionsBase

export type UrlInputOptions = InputOptionsBase

type InputOptionsBase = {
labels?: { placeholder?: string; button?: string }
}
Expand Down

2 comments on commit ce1b23a

@vercel
Copy link

@vercel vercel bot commented on ce1b23a Jan 9, 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 – ./apps/viewer

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

@vercel
Copy link

@vercel vercel bot commented on ce1b23a Jan 9, 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

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

Please sign in to comment.