|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import * as React from 'react'; |
| 4 | +import { useEffect, useState } from 'react'; |
| 5 | + |
| 6 | +import Button from '~components/button'; |
| 7 | +import Checkbox from '~components/checkbox'; |
| 8 | +import Dropdown from '~components/dropdown'; |
| 9 | +import FormField from '~components/form-field'; |
| 10 | +import Select from '~components/select'; |
| 11 | +import SpaceBetween from '~components/space-between'; |
| 12 | + |
| 13 | +import ListContent from './list-content'; |
| 14 | + |
| 15 | +interface OptionType { |
| 16 | + label: string; |
| 17 | + value: string; |
| 18 | +} |
| 19 | + |
| 20 | +export default function DropdownBugbash() { |
| 21 | + const [open, setOpen] = useState(false); |
| 22 | + const [expandToViewport, setExpandToViewport] = useState(false); |
| 23 | + const [hasHeader, setHasHeader] = useState(false); |
| 24 | + const [hasFooter, setHasFooter] = useState(false); |
| 25 | + |
| 26 | + const [minWidthOption, setMinWidthOption] = useState<OptionType>({ label: 'None', value: 'none' }); |
| 27 | + const [maxWidthOption, setMaxWidthOption] = useState<OptionType>({ label: 'None', value: 'none' }); |
| 28 | + const [contentSize, setContentSize] = useState<OptionType>({ label: 'Medium (20 items)', value: '20' }); |
| 29 | + const [contentWidth, setContentWidth] = useState<OptionType>({ label: 'Short', value: 'short' }); |
| 30 | + |
| 31 | + const widthOptions: OptionType[] = [ |
| 32 | + { label: 'None', value: 'none' }, |
| 33 | + { label: '200px', value: '200' }, |
| 34 | + { label: '400px', value: '400' }, |
| 35 | + { label: '600px', value: '600' }, |
| 36 | + ]; |
| 37 | + |
| 38 | + const contentSizeOptions: OptionType[] = [ |
| 39 | + { label: 'Small (5 items)', value: '5' }, |
| 40 | + { label: 'Medium (20 items)', value: '20' }, |
| 41 | + { label: 'Large (50 items)', value: '50' }, |
| 42 | + { label: 'Extra Large (100 items)', value: '100' }, |
| 43 | + ]; |
| 44 | + |
| 45 | + const contentWidthOptions: OptionType[] = [ |
| 46 | + { label: 'Short', value: 'short' }, |
| 47 | + { label: 'Medium', value: 'medium' }, |
| 48 | + { label: 'Long', value: 'long' }, |
| 49 | + { label: 'Extra Long', value: 'extra-long' }, |
| 50 | + ]; |
| 51 | + |
| 52 | + const getContentRepeat = (width: string) => { |
| 53 | + const repeatMap = { short: 1, medium: 5, long: 15, 'extra-long': 30 }; |
| 54 | + return repeatMap[width as keyof typeof repeatMap] || 1; |
| 55 | + }; |
| 56 | + |
| 57 | + const getWidthValue = (option: OptionType) => (option.value === 'none' ? undefined : parseInt(option.value, 10)); |
| 58 | + |
| 59 | + const minWidth = getWidthValue(minWidthOption); |
| 60 | + const maxWidth = getWidthValue(maxWidthOption); |
| 61 | + const itemCount = parseInt(contentSize.value, 10); |
| 62 | + const contentRepeat = getContentRepeat(contentWidth.value); |
| 63 | + |
| 64 | + useEffect(() => { |
| 65 | + setOpen(false); |
| 66 | + }, [expandToViewport, minWidth, maxWidth, itemCount, contentRepeat, hasHeader, hasFooter]); |
| 67 | + |
| 68 | + return ( |
| 69 | + <div style={{ padding: '20px' }}> |
| 70 | + <h1>Dropdown Props Test Page</h1> |
| 71 | + <p>Use this page to test dropdown behavior with different prop combinations.</p> |
| 72 | + |
| 73 | + <div style={{ display: 'flex', gap: '40px', marginTop: '30px', flexWrap: 'wrap' }}> |
| 74 | + <div style={{ minWidth: '300px', flexShrink: 0 }}> |
| 75 | + <SpaceBetween size="m"> |
| 76 | + <h3>Dropdown Props</h3> |
| 77 | + |
| 78 | + <FormField label="Content Size"> |
| 79 | + <Select |
| 80 | + selectedOption={contentSize} |
| 81 | + onChange={({ detail }) => setContentSize(detail.selectedOption as OptionType)} |
| 82 | + options={contentSizeOptions} |
| 83 | + /> |
| 84 | + </FormField> |
| 85 | + |
| 86 | + <FormField label="Content Width"> |
| 87 | + <Select |
| 88 | + selectedOption={contentWidth} |
| 89 | + onChange={({ detail }) => setContentWidth(detail.selectedOption as OptionType)} |
| 90 | + options={contentWidthOptions} |
| 91 | + /> |
| 92 | + </FormField> |
| 93 | + |
| 94 | + <FormField label="Min Width"> |
| 95 | + <Select |
| 96 | + selectedOption={minWidthOption} |
| 97 | + onChange={({ detail }) => setMinWidthOption(detail.selectedOption as OptionType)} |
| 98 | + options={widthOptions} |
| 99 | + /> |
| 100 | + </FormField> |
| 101 | + |
| 102 | + <FormField label="Max Width"> |
| 103 | + <Select |
| 104 | + selectedOption={maxWidthOption} |
| 105 | + onChange={({ detail }) => setMaxWidthOption(detail.selectedOption as OptionType)} |
| 106 | + options={widthOptions} |
| 107 | + /> |
| 108 | + </FormField> |
| 109 | + |
| 110 | + <Checkbox checked={expandToViewport} onChange={({ detail }) => setExpandToViewport(detail.checked)}> |
| 111 | + expandToViewport |
| 112 | + </Checkbox> |
| 113 | + |
| 114 | + <Checkbox checked={hasHeader} onChange={({ detail }) => setHasHeader(detail.checked)}> |
| 115 | + Show Header |
| 116 | + </Checkbox> |
| 117 | + |
| 118 | + <Checkbox checked={hasFooter} onChange={({ detail }) => setHasFooter(detail.checked)}> |
| 119 | + Show Footer |
| 120 | + </Checkbox> |
| 121 | + </SpaceBetween> |
| 122 | + </div> |
| 123 | + |
| 124 | + <div style={{ flex: 1 }}> |
| 125 | + <h3>Test Area</h3> |
| 126 | + <div style={{ marginTop: '100px', display: 'inline-block' }}> |
| 127 | + <Dropdown |
| 128 | + trigger={<Button onClick={() => setOpen(!open)}>{open ? 'Close' : 'Open'} Dropdown</Button>} |
| 129 | + open={open} |
| 130 | + expandToViewport={expandToViewport} |
| 131 | + minWidth={minWidth} |
| 132 | + maxWidth={maxWidth} |
| 133 | + header={hasHeader ? <div style={{ padding: '10px', background: '#f0f0f0' }}>Header</div> : undefined} |
| 134 | + footer={hasFooter ? <div style={{ padding: '10px', background: '#f0f0f0' }}>Footer</div> : undefined} |
| 135 | + content={<ListContent n={itemCount} repeat={contentRepeat} withSpaces={true} />} |
| 136 | + /> |
| 137 | + </div> |
| 138 | + |
| 139 | + <div style={{ height: '600px' }} /> |
| 140 | + <div style={{ padding: '20px', background: '#f5f5f5' }}> |
| 141 | + Scroll down to test dropdown positioning at bottom of viewport |
| 142 | + </div> |
| 143 | + <div style={{ height: '600px' }} /> |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + ); |
| 148 | +} |
0 commit comments