|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import React, { useContext, useState } from 'react'; |
| 5 | + |
| 6 | +import { SpaceBetween } from '~components'; |
| 7 | +import Select, { SelectProps } from '~components/select'; |
| 8 | + |
| 9 | +import AppContext, { AppContextType } from '../app/app-context'; |
| 10 | +import ScreenshotArea from '../utils/screenshot-area'; |
| 11 | + |
| 12 | +const generateOptions = (count: number): SelectProps.Options => { |
| 13 | + return Array.from({ length: count }, (_, i) => ({ |
| 14 | + value: `option-${i + 1}`, |
| 15 | + label: `Option ${i + 1}`, |
| 16 | + })); |
| 17 | +}; |
| 18 | + |
| 19 | +type PageContext = React.Context< |
| 20 | + AppContextType<{ |
| 21 | + filterEnabled?: boolean; |
| 22 | + }> |
| 23 | +>; |
| 24 | + |
| 25 | +export default function () { |
| 26 | + const { urlParams, setUrlParams } = useContext(AppContext as PageContext); |
| 27 | + const filterEnabled = urlParams.filterEnabled ?? true; |
| 28 | + |
| 29 | + const options = generateOptions(50); |
| 30 | + const [selected, setSelected] = useState<SelectProps['selectedOption']>(options[25]); |
| 31 | + |
| 32 | + return ( |
| 33 | + <> |
| 34 | + <h1>Select scroll to selected option</h1> |
| 35 | + <SpaceBetween size="l"> |
| 36 | + <button id="toggle-virtual" onClick={() => setUrlParams({ filterEnabled: !filterEnabled })}> |
| 37 | + Toggle filter ({filterEnabled ? 'on' : 'off'}) |
| 38 | + </button> |
| 39 | + |
| 40 | + <ScreenshotArea |
| 41 | + style={{ |
| 42 | + // extra space to include dropdown in the screenshot area |
| 43 | + paddingBlockEnd: 300, |
| 44 | + }} |
| 45 | + > |
| 46 | + <div> |
| 47 | + <Select |
| 48 | + data-testid="select-demo" |
| 49 | + ariaLabel="select demo" |
| 50 | + selectedOption={selected} |
| 51 | + options={options} |
| 52 | + onChange={event => setSelected(event.detail.selectedOption)} |
| 53 | + filteringType={filterEnabled ? 'auto' : 'none'} |
| 54 | + /> |
| 55 | + </div> |
| 56 | + </ScreenshotArea> |
| 57 | + </SpaceBetween> |
| 58 | + </> |
| 59 | + ); |
| 60 | +} |
0 commit comments