|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | | -import React, { useState } from 'react'; |
| 3 | +import React, { useContext, useEffect, useState } from 'react'; |
4 | 4 |
|
5 | | -import { Box, DatePicker, FormField, Link } from '~components'; |
| 5 | +import { Box, Checkbox, DatePicker, DatePickerProps, FormField, Link, SpaceBetween } from '~components'; |
6 | 6 |
|
| 7 | +import { AppContextType } from '../app/app-context'; |
| 8 | +import AppContext from '../app/app-context'; |
7 | 9 | import i18nStrings from './i18n-strings'; |
8 | 10 |
|
9 | | -export default function DatePickerScenario() { |
10 | | - const [value, setValue] = useState(''); |
| 11 | +export type DatePickerDemoContext = React.Context< |
| 12 | + AppContextType<{ |
| 13 | + monthOnly?: boolean; |
| 14 | + hasValue?: boolean; |
| 15 | + }> |
| 16 | +>; |
| 17 | + |
| 18 | +export const dateRangePickerDemoDefaults = { |
| 19 | + monthOnly: false, |
| 20 | + hasValue: false, |
| 21 | +}; |
| 22 | + |
| 23 | +export default function DateInputScenario() { |
| 24 | + const { urlParams, setUrlParams } = useContext(AppContext as DatePickerDemoContext); |
| 25 | + const monthOnly = urlParams.monthOnly ?? dateRangePickerDemoDefaults.monthOnly; |
| 26 | + const hasValue = urlParams.hasValue ?? dateRangePickerDemoDefaults.hasValue; |
| 27 | + |
| 28 | + const [value, setValue] = useState<DatePickerProps['value']>(''); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + const initValue = monthOnly ? '2025-02' : '2025-02-00'; |
| 32 | + if (hasValue) { |
| 33 | + setValue(initValue); |
| 34 | + } else { |
| 35 | + setValue(''); |
| 36 | + } |
| 37 | + }, [hasValue, monthOnly]); |
11 | 38 |
|
12 | 39 | return ( |
13 | 40 | <Box padding="s"> |
14 | | - <h1>Date picker simple version</h1> |
15 | | - <Link id="focus-dismiss-helper">Focusable element before the date picker</Link> |
16 | | - <br /> |
17 | | - <FormField label="Certificate expiry date" constraintText="Use YYYY/MM/DD format."> |
18 | | - <DatePicker |
19 | | - value={value} |
20 | | - name={'date-picker-name'} |
21 | | - locale="en-GB" |
22 | | - i18nStrings={i18nStrings} |
23 | | - openCalendarAriaLabel={selectedDate => |
24 | | - 'Choose certificate expiry date' + (selectedDate ? `, selected date is ${selectedDate}` : '') |
25 | | - } |
26 | | - placeholder={'YYYY/MM/DD'} |
27 | | - onChange={event => setValue(event.detail.value)} |
28 | | - /> |
29 | | - </FormField> |
30 | | - <br /> |
31 | | - <br /> |
32 | | - <Link id="focusable-element-after-date-picker">Focusable element after the date picker</Link> |
| 41 | + <SpaceBetween direction="vertical" size="m"> |
| 42 | + <h1>Date picker simple version</h1> |
| 43 | + <SpaceBetween direction="horizontal" size="s"> |
| 44 | + <Checkbox checked={monthOnly} onChange={({ detail }) => setUrlParams({ monthOnly: detail.checked })}> |
| 45 | + Month-only |
| 46 | + </Checkbox> |
| 47 | + <Checkbox checked={hasValue} onChange={({ detail }) => setUrlParams({ hasValue: detail.checked })}> |
| 48 | + Has initial value |
| 49 | + </Checkbox> |
| 50 | + </SpaceBetween> |
| 51 | + <Link id="focus-dismiss-helper">Focusable element before the date picker</Link> |
| 52 | + <br /> |
| 53 | + <FormField label="Certificate expiry date" constraintText={`Use YYYY/MM${monthOnly ? '' : '/DD'} format.`}> |
| 54 | + <DatePicker |
| 55 | + value={value} |
| 56 | + name={'date-picker-name'} |
| 57 | + granularity={monthOnly ? 'month' : 'day'} |
| 58 | + locale="en-GB" |
| 59 | + i18nStrings={i18nStrings} |
| 60 | + openCalendarAriaLabel={selectedDate => |
| 61 | + 'Choose certificate expiry date' + (selectedDate ? `, selected date is ${selectedDate}` : '') |
| 62 | + } |
| 63 | + placeholder={monthOnly ? 'YYYY/MM' : 'YYYY/MM/DD'} |
| 64 | + onChange={e => setValue(e.detail.value)} |
| 65 | + /> |
| 66 | + </FormField> |
| 67 | + <br /> |
| 68 | + <br /> |
| 69 | + <Link id="focusable-element-after-date-picker">Focusable element after the date picker</Link> |
| 70 | + <b>Raw value</b> |
| 71 | + <pre>{JSON.stringify(value, undefined, 2)}</pre> |
| 72 | + </SpaceBetween> |
33 | 73 | </Box> |
34 | 74 | ); |
35 | 75 | } |
0 commit comments