|
| 1 | +# B1 — Create a dev page for the badge component using app context to control color |
| 2 | + |
| 3 | +```tsx |
| 4 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 |
| 6 | +import React, { useContext } from 'react'; |
| 7 | + |
| 8 | +import Badge, { BadgeProps } from '~components/badge'; |
| 9 | +import FormField from '~components/form-field'; |
| 10 | +import Select from '~components/select'; |
| 11 | +import SpaceBetween from '~components/space-between'; |
| 12 | + |
| 13 | +import AppContext, { AppContextType } from '../app/app-context'; |
| 14 | +import { SimplePage } from '../app/templates'; |
| 15 | + |
| 16 | +type PageContext = React.Context<AppContextType<{ color: BadgeProps['color'] }>>; |
| 17 | + |
| 18 | +const colorOptions: Array<{ value: NonNullable<BadgeProps['color']>; label: string }> = [ |
| 19 | + { value: 'grey', label: 'Grey' }, |
| 20 | + { value: 'blue', label: 'Blue' }, |
| 21 | + { value: 'green', label: 'Green' }, |
| 22 | + { value: 'red', label: 'Red' }, |
| 23 | + { value: 'severity-critical', label: 'Severity critical' }, |
| 24 | + { value: 'severity-high', label: 'Severity high' }, |
| 25 | + { value: 'severity-medium', label: 'Severity medium' }, |
| 26 | + { value: 'severity-low', label: 'Severity low' }, |
| 27 | + { value: 'severity-neutral', label: 'Severity neutral' }, |
| 28 | +]; |
| 29 | + |
| 30 | +export default function BadgeAppContext() { |
| 31 | + const { urlParams, setUrlParams } = useContext(AppContext as PageContext); |
| 32 | + const color = urlParams.color ?? 'grey'; |
| 33 | + |
| 34 | + return ( |
| 35 | + <SimplePage |
| 36 | + title="Badge - App Context" |
| 37 | + screenshotArea={{}} |
| 38 | + settings={ |
| 39 | + <FormField label="Color"> |
| 40 | + <Select |
| 41 | + selectedOption={colorOptions.find(o => o.value === color) ?? colorOptions[0]} |
| 42 | + options={colorOptions} |
| 43 | + onChange={({ detail }) => setUrlParams({ color: detail.selectedOption.value as BadgeProps['color'] })} |
| 44 | + /> |
| 45 | + </FormField> |
| 46 | + } |
| 47 | + > |
| 48 | + <SpaceBetween direction="horizontal" size="m"> |
| 49 | + <Badge color={color}>Badge</Badge> |
| 50 | + <Badge color={color}>Status: Active</Badge> |
| 51 | + <Badge color={color}>42</Badge> |
| 52 | + </SpaceBetween> |
| 53 | + </SimplePage> |
| 54 | + ); |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +*Files read: ~8 (badge/interfaces.ts, badge/index.tsx, badge/permutations.page.tsx, badge/style-custom-types.page.tsx, tooltip/simple.page.tsx, app/app-context.tsx, app/templates.tsx, grep for AppContext usage)* |
| 61 | +*Credits: 4.77 · Time: 1m 22s* |
0 commit comments