|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import React, { useContext, useRef, useState } from 'react'; |
| 4 | +import clsx from 'clsx'; |
| 5 | + |
| 6 | +import Box from '~components/box'; |
| 7 | +import Checkbox from '~components/checkbox'; |
| 8 | +import FormField from '~components/form-field'; |
| 9 | +import RadioButton, { RadioButtonProps } from '~components/radio-button'; |
| 10 | +import SpaceBetween from '~components/space-between'; |
| 11 | + |
| 12 | +import AppContext, { AppContextType } from '../app/app-context'; |
| 13 | +import ScreenshotArea from '../utils/screenshot-area'; |
| 14 | + |
| 15 | +import styles from './table.scss'; |
| 16 | + |
| 17 | +type RadioButtonDemoContext = React.Context< |
| 18 | + AppContextType<{ |
| 19 | + disabled?: boolean; |
| 20 | + readOnly?: boolean; |
| 21 | + }> |
| 22 | +>; |
| 23 | + |
| 24 | +const Row = ({ |
| 25 | + value, |
| 26 | + label, |
| 27 | + checked, |
| 28 | + setValue, |
| 29 | + description, |
| 30 | + disabled, |
| 31 | + readOnly, |
| 32 | +}: Omit<RadioButtonProps, 'name'> & { label: string; setValue: (_a: string) => void }) => { |
| 33 | + const ref = useRef<HTMLInputElement>(null); |
| 34 | + return ( |
| 35 | + <tr className={clsx(styles.row, checked && styles['row--selected'])}> |
| 36 | + <td className={styles.cell}> |
| 37 | + <label aria-label={`Select ${label}`}> |
| 38 | + <RadioButton |
| 39 | + value={value} |
| 40 | + name="quantity" |
| 41 | + checked={checked} |
| 42 | + disabled={disabled} |
| 43 | + readOnly={readOnly} |
| 44 | + onChange={({ detail }) => { |
| 45 | + setValue(detail.value); |
| 46 | + ref.current?.focus(); |
| 47 | + }} |
| 48 | + ref={ref} |
| 49 | + /> |
| 50 | + </label> |
| 51 | + </td> |
| 52 | + <td className={styles.cell}> |
| 53 | + <a href="#">{label}</a> |
| 54 | + </td> |
| 55 | + <td className={styles.cell}>{description}</td> |
| 56 | + </tr> |
| 57 | + ); |
| 58 | +}; |
| 59 | + |
| 60 | +export default function RadioButtonsTablePage() { |
| 61 | + const { urlParams, setUrlParams } = useContext(AppContext as RadioButtonDemoContext); |
| 62 | + |
| 63 | + const [value, setValue] = useState<string>(''); |
| 64 | + |
| 65 | + return ( |
| 66 | + <article> |
| 67 | + <h1>Radio button — custom table selection</h1> |
| 68 | + <Box margin={{ horizontal: 'm' }}> |
| 69 | + <SpaceBetween size="s"> |
| 70 | + <FormField label="Options"> |
| 71 | + <SpaceBetween size="s"> |
| 72 | + <Checkbox |
| 73 | + checked={!!urlParams.disabled} |
| 74 | + onChange={({ detail }) => setUrlParams({ ...urlParams, disabled: detail.checked })} |
| 75 | + description="Make one of the radio buttons disabled" |
| 76 | + > |
| 77 | + Disabled |
| 78 | + </Checkbox> |
| 79 | + |
| 80 | + <Checkbox |
| 81 | + checked={!!urlParams.readOnly} |
| 82 | + onChange={({ detail }) => setUrlParams({ ...urlParams, readOnly: detail.checked })} |
| 83 | + description="Make one of the radio buttons read-only" |
| 84 | + > |
| 85 | + Read-only |
| 86 | + </Checkbox> |
| 87 | + </SpaceBetween> |
| 88 | + </FormField> |
| 89 | + </SpaceBetween> |
| 90 | + </Box> |
| 91 | + <hr /> |
| 92 | + <ScreenshotArea disableAnimations={true}> |
| 93 | + <table className={styles.table}> |
| 94 | + <thead className={styles.head}> |
| 95 | + <tr className={styles.row}> |
| 96 | + <th className={styles['header-cell']}></th> |
| 97 | + <th className={styles['header-cell']}>Name</th> |
| 98 | + <th className={styles['header-cell']}>Description</th> |
| 99 | + </tr> |
| 100 | + </thead> |
| 101 | + <tbody> |
| 102 | + <Row value="one" checked={value === 'one'} setValue={setValue} label="One" description="First option"> |
| 103 | + One |
| 104 | + </Row> |
| 105 | + <Row |
| 106 | + value="two" |
| 107 | + checked={value === 'two'} |
| 108 | + disabled={urlParams.disabled} |
| 109 | + readOnly={urlParams.readOnly} |
| 110 | + setValue={setValue} |
| 111 | + label="Two" |
| 112 | + description="Second option" |
| 113 | + > |
| 114 | + Two |
| 115 | + </Row> |
| 116 | + <Row value="three" checked={value === 'three'} setValue={setValue} label="Three" description="Third option"> |
| 117 | + Three |
| 118 | + </Row> |
| 119 | + </tbody> |
| 120 | + </table> |
| 121 | + </ScreenshotArea> |
| 122 | + </article> |
| 123 | + ); |
| 124 | +} |
0 commit comments