|
| 1 | +import React, { useState } from 'react'; |
| 2 | + |
| 3 | +import { Button, Form, Modal } from 'storybook/internal/components'; |
| 4 | + |
| 5 | +import { styled } from 'storybook/theming'; |
| 6 | + |
| 7 | +import { isChromatic } from '../../../../../.storybook/isChromatic'; |
| 8 | + |
| 9 | +interface BaseField { |
| 10 | + label: string; |
| 11 | + options: Record<string, { label: string }>; |
| 12 | + required?: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +interface CheckboxField extends BaseField { |
| 16 | + type: 'checkbox'; |
| 17 | + values: Record<keyof BaseField['options'], boolean>; |
| 18 | +} |
| 19 | + |
| 20 | +interface SelectField extends BaseField { |
| 21 | + type: 'select'; |
| 22 | + values: Record<keyof BaseField['options'], boolean>; |
| 23 | +} |
| 24 | + |
| 25 | +type FormFields = { |
| 26 | + building: CheckboxField; |
| 27 | + interest: CheckboxField; |
| 28 | + referrer: SelectField; |
| 29 | +}; |
| 30 | + |
| 31 | +const Content = styled(Modal.Content)(({ theme }) => ({ |
| 32 | + fontSize: theme.typography.size.s2, |
| 33 | + color: theme.color.defaultText, |
| 34 | + gap: 8, |
| 35 | +})); |
| 36 | + |
| 37 | +const Row = styled.div({ |
| 38 | + display: 'grid', |
| 39 | + gridTemplateColumns: '1fr 1fr', |
| 40 | + gap: 14, |
| 41 | + marginBottom: 8, |
| 42 | +}); |
| 43 | + |
| 44 | +const Question = styled.div(({ theme }) => ({ |
| 45 | + marginTop: 8, |
| 46 | + marginBottom: 2, |
| 47 | + fontWeight: theme.typography.weight.bold, |
| 48 | +})); |
| 49 | + |
| 50 | +const Label = styled.label({ |
| 51 | + display: 'flex', |
| 52 | + gap: 8, |
| 53 | + |
| 54 | + '&:has(input[type="checkbox"]:not(:disabled), input[type="radio"]:not(:disabled))': { |
| 55 | + cursor: 'pointer', |
| 56 | + }, |
| 57 | +}); |
| 58 | + |
| 59 | +const Actions = styled(Modal.Actions)({ |
| 60 | + marginTop: 8, |
| 61 | +}); |
| 62 | + |
| 63 | +const Checkbox = styled(Form.Checkbox)({ |
| 64 | + margin: 2, |
| 65 | +}); |
| 66 | + |
| 67 | +export const IntentSurvey = ({ |
| 68 | + onComplete, |
| 69 | + onDismiss, |
| 70 | +}: { |
| 71 | + onComplete: (formData: Record<string, Record<string, boolean>>) => void; |
| 72 | + onDismiss: () => void; |
| 73 | +}) => { |
| 74 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 75 | + |
| 76 | + const [formFields, setFormFields] = useState<FormFields>({ |
| 77 | + building: { |
| 78 | + label: 'What are you building?', |
| 79 | + type: 'checkbox', |
| 80 | + required: true, |
| 81 | + options: shuffleObject({ |
| 82 | + 'design-system': { label: 'Design system' }, |
| 83 | + 'application-ui': { label: 'Application UI' }, |
| 84 | + }), |
| 85 | + values: { |
| 86 | + 'design-system': false, |
| 87 | + 'application-ui': false, |
| 88 | + }, |
| 89 | + }, |
| 90 | + interest: { |
| 91 | + label: 'Which of these are you interested in?', |
| 92 | + type: 'checkbox', |
| 93 | + required: true, |
| 94 | + options: shuffleObject({ |
| 95 | + 'ui-documentation': { label: 'Generating UI docs' }, |
| 96 | + 'functional-testing': { label: 'Functional testing' }, |
| 97 | + 'accessibility-testing': { label: 'Accessibility testing' }, |
| 98 | + 'visual-testing': { label: 'Visual testing' }, |
| 99 | + 'ai-augmented-development': { label: 'Building UI with AI' }, |
| 100 | + 'team-collaboration': { label: 'Team collaboration' }, |
| 101 | + 'design-handoff': { label: 'Design handoff' }, |
| 102 | + }), |
| 103 | + values: { |
| 104 | + 'ui-documentation': false, |
| 105 | + 'functional-testing': false, |
| 106 | + 'accessibility-testing': false, |
| 107 | + 'visual-testing': false, |
| 108 | + 'ai-augmented-development': false, |
| 109 | + 'team-collaboration': false, |
| 110 | + 'design-handoff': false, |
| 111 | + }, |
| 112 | + }, |
| 113 | + referrer: { |
| 114 | + label: 'How did you learn about Storybook?', |
| 115 | + type: 'select', |
| 116 | + required: true, |
| 117 | + options: shuffleObject({ |
| 118 | + 'we-use-it-at-work': { label: 'We use it at work' }, |
| 119 | + 'via-friend-or-colleague': { label: 'Via friend or colleague' }, |
| 120 | + 'via-social-media': { label: 'Via social media' }, |
| 121 | + youtube: { label: 'YouTube' }, |
| 122 | + 'web-search': { label: 'Web Search' }, |
| 123 | + 'ai-agent': { label: 'AI Agent (e.g. ChatGPT)' }, |
| 124 | + }), |
| 125 | + values: { |
| 126 | + 'we-use-it-at-work': false, |
| 127 | + 'via-friend-or-colleague': false, |
| 128 | + 'via-social-media': false, |
| 129 | + youtube: false, |
| 130 | + 'web-search': false, |
| 131 | + 'ai-agent': false, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }); |
| 135 | + |
| 136 | + const updateFormData = (key: keyof FormFields, optionOrValue: string, value?: boolean) => { |
| 137 | + const field = formFields[key]; |
| 138 | + setFormFields((fields) => { |
| 139 | + if (field.type === 'checkbox') { |
| 140 | + const values = { ...field.values, [optionOrValue]: !!value }; |
| 141 | + return { ...fields, [key]: { ...field, values } }; |
| 142 | + } |
| 143 | + if (field.type === 'select') { |
| 144 | + const values = Object.fromEntries( |
| 145 | + Object.entries(field.values).map(([opt]) => [opt, opt === optionOrValue]) |
| 146 | + ); |
| 147 | + return { ...fields, [key]: { ...field, values } }; |
| 148 | + } |
| 149 | + return fields; |
| 150 | + }); |
| 151 | + }; |
| 152 | + |
| 153 | + const isValid = Object.values(formFields).every((field) => { |
| 154 | + if (!field.required) { |
| 155 | + return true; |
| 156 | + } |
| 157 | + // Check if at least one option is selected (true) |
| 158 | + return Object.values(field.values).some((value) => value === true); |
| 159 | + }); |
| 160 | + |
| 161 | + const onSubmitForm = (e: React.FormEvent<HTMLFormElement>) => { |
| 162 | + if (!isValid) { |
| 163 | + return; |
| 164 | + } |
| 165 | + e.preventDefault(); |
| 166 | + setIsSubmitting(true); |
| 167 | + onComplete( |
| 168 | + Object.fromEntries(Object.entries(formFields).map(([key, field]) => [key, field.values])) |
| 169 | + ); |
| 170 | + }; |
| 171 | + |
| 172 | + return ( |
| 173 | + <Modal defaultOpen width={420} onEscapeKeyDown={onDismiss}> |
| 174 | + <Form onSubmit={onSubmitForm} id="intent-survey-form"> |
| 175 | + <Content> |
| 176 | + <Modal.Header> |
| 177 | + <Modal.Title>Help improve Storybook</Modal.Title> |
| 178 | + </Modal.Header> |
| 179 | + |
| 180 | + {(Object.keys(formFields) as Array<keyof FormFields>).map((key) => { |
| 181 | + const field = formFields[key]; |
| 182 | + return ( |
| 183 | + <React.Fragment key={key}> |
| 184 | + <Question>{field.label}</Question> |
| 185 | + {field.type === 'checkbox' && ( |
| 186 | + <Row> |
| 187 | + {Object.entries(field.options).map(([opt, option]) => { |
| 188 | + const id = `${key}:${opt}`; |
| 189 | + return ( |
| 190 | + <div key={id}> |
| 191 | + <Label htmlFor={id}> |
| 192 | + <Checkbox |
| 193 | + name={id} |
| 194 | + id={id} |
| 195 | + checked={field.values[opt]} |
| 196 | + disabled={isSubmitting} |
| 197 | + onChange={(e) => updateFormData(key, opt, e.target.checked)} |
| 198 | + /> |
| 199 | + {option.label} |
| 200 | + </Label> |
| 201 | + </div> |
| 202 | + ); |
| 203 | + })} |
| 204 | + </Row> |
| 205 | + )} |
| 206 | + {field.type === 'select' && ( |
| 207 | + <Form.Select |
| 208 | + name={key} |
| 209 | + id={key} |
| 210 | + value={ |
| 211 | + Object.entries(field.values).find(([, isSelected]) => isSelected)?.[0] || '' |
| 212 | + } |
| 213 | + required={field.required} |
| 214 | + disabled={isSubmitting} |
| 215 | + onChange={(e) => updateFormData(key, e.target.value)} |
| 216 | + > |
| 217 | + <option disabled hidden value=""> |
| 218 | + Select an option... |
| 219 | + </option> |
| 220 | + {Object.entries(field.options).map(([opt, option]) => ( |
| 221 | + <option key={opt} value={opt}> |
| 222 | + {option.label} |
| 223 | + </option> |
| 224 | + ))} |
| 225 | + </Form.Select> |
| 226 | + )} |
| 227 | + </React.Fragment> |
| 228 | + ); |
| 229 | + })} |
| 230 | + |
| 231 | + <Actions> |
| 232 | + <Button disabled={isSubmitting || !isValid} size="medium" type="submit" variant="solid"> |
| 233 | + Submit |
| 234 | + </Button> |
| 235 | + </Actions> |
| 236 | + </Content> |
| 237 | + </Form> |
| 238 | + </Modal> |
| 239 | + ); |
| 240 | +}; |
| 241 | + |
| 242 | +function shuffle<T>(array: T[]): T[] { |
| 243 | + for (let i = array.length - 1; i > 0; i--) { |
| 244 | + const j = Math.floor(Math.random() * (i + 1)); |
| 245 | + [array[i], array[j]] = [array[j], array[i]]; |
| 246 | + } |
| 247 | + return array; |
| 248 | +} |
| 249 | + |
| 250 | +function shuffleObject<T extends object>(object: T): T { |
| 251 | + return isChromatic() ? object : (Object.fromEntries(shuffle(Object.entries(object))) as T); |
| 252 | +} |
0 commit comments