-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Onboarding: Intent survey #31944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Onboarding: Intent survey #31944
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0f8cd70
Refactor Form components and their stories. Moved Checkbox into Form …
ghengeveld b31f9e5
Update Select to use appearance: base-select and follow design spec
ghengeveld 0dfbe8e
Scaffold for intent survey
ghengeveld 7bb05f9
Send survey answers as new telemetry event
ghengeveld f4a2d14
Update code/addons/onboarding/src/features/IntentSurvey/IntentSurvey.tsx
ghengeveld 63887d8
Fix story hierarchy
ghengeveld 5972de3
Move components to separate files and tweak a few things
ghengeveld c57099c
Rework data structure
ghengeveld edcf2d8
Update stories
ghengeveld a200d4d
Clean up stories
ghengeveld 9ad50b0
Merge branch 'next' into onboarding-intent-survey
ghengeveld a54bde4
Restore React imports
ghengeveld bc279ad
Field value type is irrelevant here
ghengeveld fb90c85
Use controlled components
ghengeveld 54862bb
Don't shuffle in visual tests
ghengeveld 8aa1159
Merge branch 'next' into onboarding-intent-survey
ghengeveld 2bb0999
Merge branch 'next' into onboarding-intent-survey
ghengeveld 67e98f4
Drop old cruft
ghengeveld 54a4322
Avoid "invalid" nesting in test environments, to circumvent validateD…
ghengeveld c160816
Reference React validateDOMNesting issue
ghengeveld 31b3574
Update e2e test
ghengeveld a207776
Fix selectors
ghengeveld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
code/addons/onboarding/src/features/IntentSurvey/IntentSurvey.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite'; | ||
|
|
||
| import { expect, fn, screen, userEvent, waitFor } from 'storybook/test'; | ||
|
|
||
| import { IntentSurvey } from './IntentSurvey'; | ||
|
|
||
| const meta = { | ||
| component: IntentSurvey, | ||
| args: { | ||
| onComplete: fn(), | ||
| onDismiss: fn(), | ||
| }, | ||
| } as Meta<typeof IntentSurvey>; | ||
|
|
||
| type Story = StoryObj<typeof meta>; | ||
| export default meta; | ||
|
|
||
| export const Default: Story = {}; | ||
|
|
||
| export const Submitting: Story = { | ||
| play: async ({ args }) => { | ||
| const button = await screen.findByRole('button', { name: 'Submit' }); | ||
| await expect(button).toBeDisabled(); | ||
|
|
||
| await userEvent.click(await screen.findByText('Design system')); | ||
| await expect(button).toBeDisabled(); | ||
|
|
||
| await userEvent.click(await screen.findByText('Functional testing')); | ||
| await userEvent.click(await screen.findByText('Accessibility testing')); | ||
| await userEvent.click(await screen.findByText('Visual testing')); | ||
| await expect(button).toBeDisabled(); | ||
|
|
||
| await userEvent.selectOptions(screen.getByRole('combobox'), ['We use it at work']); | ||
| await expect(button).not.toBeDisabled(); | ||
|
|
||
| await userEvent.click(button); | ||
|
|
||
| await waitFor(async () => { | ||
| await expect(button).toBeDisabled(); | ||
| await expect(args.onComplete).toHaveBeenCalledWith({ | ||
| building: { | ||
| 'application-ui': false, | ||
| 'design-system': true, | ||
| }, | ||
| interest: { | ||
| 'accessibility-testing': true, | ||
| 'ai-augmented-development': false, | ||
| 'design-handoff': false, | ||
| 'functional-testing': true, | ||
| 'team-collaboration': false, | ||
| 'ui-documentation': false, | ||
| 'visual-testing': true, | ||
| }, | ||
| referrer: { | ||
| 'ai-agent': false, | ||
| 'via-friend-or-colleague': false, | ||
| 'via-social-media': false, | ||
| 'we-use-it-at-work': true, | ||
| 'web-search': false, | ||
| youtube: false, | ||
| }, | ||
| }); | ||
| }); | ||
| }, | ||
| }; |
252 changes: 252 additions & 0 deletions
252
code/addons/onboarding/src/features/IntentSurvey/IntentSurvey.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,252 @@ | ||
| import React, { useState } from 'react'; | ||
|
|
||
| import { Button, Form, Modal } from 'storybook/internal/components'; | ||
|
|
||
| import { styled } from 'storybook/theming'; | ||
|
|
||
| import { isChromatic } from '../../../../../.storybook/isChromatic'; | ||
|
|
||
| interface BaseField { | ||
| label: string; | ||
| options: Record<string, { label: string }>; | ||
| required?: boolean; | ||
| } | ||
|
|
||
| interface CheckboxField extends BaseField { | ||
| type: 'checkbox'; | ||
| values: Record<keyof BaseField['options'], boolean>; | ||
| } | ||
|
|
||
| interface SelectField extends BaseField { | ||
| type: 'select'; | ||
| values: Record<keyof BaseField['options'], boolean>; | ||
| } | ||
|
|
||
| type FormFields = { | ||
| building: CheckboxField; | ||
| interest: CheckboxField; | ||
| referrer: SelectField; | ||
| }; | ||
|
|
||
| const Content = styled(Modal.Content)(({ theme }) => ({ | ||
| fontSize: theme.typography.size.s2, | ||
| color: theme.color.defaultText, | ||
| gap: 8, | ||
| })); | ||
|
|
||
| const Row = styled.div({ | ||
| display: 'grid', | ||
| gridTemplateColumns: '1fr 1fr', | ||
| gap: 14, | ||
| marginBottom: 8, | ||
| }); | ||
|
|
||
| const Question = styled.div(({ theme }) => ({ | ||
| marginTop: 8, | ||
| marginBottom: 2, | ||
| fontWeight: theme.typography.weight.bold, | ||
| })); | ||
|
|
||
| const Label = styled.label({ | ||
| display: 'flex', | ||
| gap: 8, | ||
|
|
||
| '&:has(input[type="checkbox"]:not(:disabled), input[type="radio"]:not(:disabled))': { | ||
| cursor: 'pointer', | ||
| }, | ||
| }); | ||
|
|
||
| const Actions = styled(Modal.Actions)({ | ||
| marginTop: 8, | ||
| }); | ||
|
|
||
| const Checkbox = styled(Form.Checkbox)({ | ||
| margin: 2, | ||
| }); | ||
|
|
||
| export const IntentSurvey = ({ | ||
| onComplete, | ||
| onDismiss, | ||
| }: { | ||
| onComplete: (formData: Record<string, Record<string, boolean>>) => void; | ||
| onDismiss: () => void; | ||
| }) => { | ||
| const [isSubmitting, setIsSubmitting] = useState(false); | ||
|
|
||
| const [formFields, setFormFields] = useState<FormFields>({ | ||
| building: { | ||
| label: 'What are you building?', | ||
| type: 'checkbox', | ||
| required: true, | ||
| options: shuffleObject({ | ||
| 'design-system': { label: 'Design system' }, | ||
| 'application-ui': { label: 'Application UI' }, | ||
| }), | ||
| values: { | ||
| 'design-system': false, | ||
| 'application-ui': false, | ||
| }, | ||
| }, | ||
| interest: { | ||
| label: 'Which of these are you interested in?', | ||
ghengeveld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| type: 'checkbox', | ||
| required: true, | ||
| options: shuffleObject({ | ||
| 'ui-documentation': { label: 'Generating UI docs' }, | ||
| 'functional-testing': { label: 'Functional testing' }, | ||
| 'accessibility-testing': { label: 'Accessibility testing' }, | ||
| 'visual-testing': { label: 'Visual testing' }, | ||
| 'ai-augmented-development': { label: 'Building UI with AI' }, | ||
| 'team-collaboration': { label: 'Team collaboration' }, | ||
| 'design-handoff': { label: 'Design handoff' }, | ||
| }), | ||
| values: { | ||
| 'ui-documentation': false, | ||
| 'functional-testing': false, | ||
| 'accessibility-testing': false, | ||
| 'visual-testing': false, | ||
| 'ai-augmented-development': false, | ||
| 'team-collaboration': false, | ||
| 'design-handoff': false, | ||
| }, | ||
| }, | ||
| referrer: { | ||
| label: 'How did you learn about Storybook?', | ||
| type: 'select', | ||
| required: true, | ||
| options: shuffleObject({ | ||
| 'we-use-it-at-work': { label: 'We use it at work' }, | ||
| 'via-friend-or-colleague': { label: 'Via friend or colleague' }, | ||
| 'via-social-media': { label: 'Via social media' }, | ||
| youtube: { label: 'YouTube' }, | ||
| 'web-search': { label: 'Web Search' }, | ||
| 'ai-agent': { label: 'AI Agent (e.g. ChatGPT)' }, | ||
| }), | ||
| values: { | ||
| 'we-use-it-at-work': false, | ||
| 'via-friend-or-colleague': false, | ||
| 'via-social-media': false, | ||
| youtube: false, | ||
| 'web-search': false, | ||
| 'ai-agent': false, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const updateFormData = (key: keyof FormFields, optionOrValue: string, value?: boolean) => { | ||
| const field = formFields[key]; | ||
| setFormFields((fields) => { | ||
| if (field.type === 'checkbox') { | ||
| const values = { ...field.values, [optionOrValue]: !!value }; | ||
| return { ...fields, [key]: { ...field, values } }; | ||
| } | ||
| if (field.type === 'select') { | ||
| const values = Object.fromEntries( | ||
| Object.entries(field.values).map(([opt]) => [opt, opt === optionOrValue]) | ||
| ); | ||
| return { ...fields, [key]: { ...field, values } }; | ||
| } | ||
| return fields; | ||
| }); | ||
| }; | ||
|
|
||
| const isValid = Object.values(formFields).every((field) => { | ||
| if (!field.required) { | ||
| return true; | ||
| } | ||
| // Check if at least one option is selected (true) | ||
| return Object.values(field.values).some((value) => value === true); | ||
| }); | ||
|
|
||
| const onSubmitForm = (e: React.FormEvent<HTMLFormElement>) => { | ||
| if (!isValid) { | ||
| return; | ||
| } | ||
| e.preventDefault(); | ||
| setIsSubmitting(true); | ||
| onComplete( | ||
| Object.fromEntries(Object.entries(formFields).map(([key, field]) => [key, field.values])) | ||
| ); | ||
| }; | ||
|
|
||
| return ( | ||
| <Modal defaultOpen width={420} onEscapeKeyDown={onDismiss}> | ||
| <Form onSubmit={onSubmitForm} id="intent-survey-form"> | ||
| <Content> | ||
| <Modal.Header> | ||
| <Modal.Title>Help improve Storybook</Modal.Title> | ||
| </Modal.Header> | ||
|
|
||
| {(Object.keys(formFields) as Array<keyof FormFields>).map((key) => { | ||
| const field = formFields[key]; | ||
| return ( | ||
| <React.Fragment key={key}> | ||
| <Question>{field.label}</Question> | ||
| {field.type === 'checkbox' && ( | ||
| <Row> | ||
| {Object.entries(field.options).map(([opt, option]) => { | ||
| const id = `${key}:${opt}`; | ||
| return ( | ||
| <div key={id}> | ||
| <Label htmlFor={id}> | ||
| <Checkbox | ||
| name={id} | ||
| id={id} | ||
| checked={field.values[opt]} | ||
| disabled={isSubmitting} | ||
| onChange={(e) => updateFormData(key, opt, e.target.checked)} | ||
| /> | ||
| {option.label} | ||
| </Label> | ||
| </div> | ||
| ); | ||
| })} | ||
| </Row> | ||
| )} | ||
| {field.type === 'select' && ( | ||
| <Form.Select | ||
| name={key} | ||
| id={key} | ||
| value={ | ||
| Object.entries(field.values).find(([, isSelected]) => isSelected)?.[0] || '' | ||
| } | ||
| required={field.required} | ||
| disabled={isSubmitting} | ||
| onChange={(e) => updateFormData(key, e.target.value)} | ||
| > | ||
| <option disabled hidden value=""> | ||
| Select an option... | ||
| </option> | ||
| {Object.entries(field.options).map(([opt, option]) => ( | ||
| <option key={opt} value={opt}> | ||
| {option.label} | ||
| </option> | ||
| ))} | ||
| </Form.Select> | ||
| )} | ||
| </React.Fragment> | ||
| ); | ||
| })} | ||
|
|
||
| <Actions> | ||
| <Button disabled={isSubmitting || !isValid} size="medium" type="submit" variant="solid"> | ||
| Submit | ||
| </Button> | ||
| </Actions> | ||
| </Content> | ||
| </Form> | ||
| </Modal> | ||
| ); | ||
| }; | ||
|
|
||
| function shuffle<T>(array: T[]): T[] { | ||
| for (let i = array.length - 1; i > 0; i--) { | ||
| const j = Math.floor(Math.random() * (i + 1)); | ||
| [array[i], array[j]] = [array[j], array[i]]; | ||
| } | ||
| return array; | ||
| } | ||
|
|
||
| function shuffleObject<T extends object>(object: T): T { | ||
| return isChromatic() ? object : (Object.fromEntries(shuffle(Object.entries(object))) as T); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.