|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
| 3 | + |
3 | 4 | import React, { useContext, useEffect, useState } from 'react';
|
4 | 5 |
|
5 |
| -import { Box, Checkbox, DatePicker, DatePickerProps, FormField, Link, SpaceBetween } from '~components'; |
| 6 | +import { |
| 7 | + Box, |
| 8 | + DatePicker, |
| 9 | + DatePickerProps, |
| 10 | + FormField, |
| 11 | + Link, |
| 12 | + Select, |
| 13 | + SelectProps, |
| 14 | + SpaceBetween, |
| 15 | + Toggle, |
| 16 | +} from '~components'; |
6 | 17 |
|
7 | 18 | import { AppContextType } from '../app/app-context';
|
8 | 19 | import AppContext from '../app/app-context';
|
| 20 | +import { getPlaceholder, locales } from '../date-input/common'; |
9 | 21 | import i18nStrings from './i18n-strings';
|
10 | 22 |
|
11 |
| -export type DatePickerDemoContext = React.Context< |
| 23 | +export type PageContext = React.Context< |
12 | 24 | AppContextType<{
|
13 |
| - monthOnly?: boolean; |
| 25 | + granularity?: DatePickerProps.Granularity; |
| 26 | + readOnly?: boolean; |
| 27 | + disabled?: boolean; |
| 28 | + inputFormat?: DatePickerProps.InputFormat; |
| 29 | + format?: DatePickerProps.Format; |
| 30 | + locale?: string; |
14 | 31 | hasValue?: boolean;
|
15 | 32 | }>
|
16 | 33 | >;
|
17 | 34 |
|
18 |
| -export const dateRangePickerDemoDefaults = { |
19 |
| - monthOnly: false, |
20 |
| - hasValue: false, |
21 |
| -}; |
| 35 | +const inputFormatOptions: SelectProps.Option[] = [{ value: 'iso' }, { value: 'slashed' }]; |
22 | 36 |
|
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; |
| 37 | +const formatOptions: SelectProps.Option[] = [...inputFormatOptions, { value: 'long-localized' }]; |
| 38 | + |
| 39 | +const localeOptions: SelectProps.Option[] = locales.map(locale => ({ value: locale })); |
27 | 40 |
|
| 41 | +export default function DateInputScenario() { |
| 42 | + const { urlParams, setUrlParams } = useContext(AppContext as PageContext); |
| 43 | + const initValue = '2018-01-02'; |
| 44 | + const hasValue = urlParams.hasValue ?? false; |
28 | 45 | const [value, setValue] = useState<DatePickerProps['value']>('');
|
29 | 46 |
|
30 | 47 | useEffect(() => {
|
31 |
| - const initValue = monthOnly ? '2025-02' : '2025-02-00'; |
32 | 48 | if (hasValue) {
|
33 | 49 | setValue(initValue);
|
34 | 50 | } else {
|
35 | 51 | setValue('');
|
36 | 52 | }
|
37 |
| - }, [hasValue, monthOnly]); |
| 53 | + }, [hasValue]); |
38 | 54 |
|
39 | 55 | return (
|
40 |
| - <Box padding="s"> |
| 56 | + <Box padding="l"> |
41 | 57 | <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> |
| 58 | + <h1>Date picker</h1> |
| 59 | + |
| 60 | + <SpaceBetween size="m"> |
| 61 | + <SpaceBetween direction="horizontal" size="s"> |
| 62 | + <Toggle |
| 63 | + checked={urlParams.granularity === 'month'} |
| 64 | + onChange={({ detail }) => setUrlParams({ granularity: detail.checked ? 'month' : 'day' })} |
| 65 | + > |
| 66 | + Month only |
| 67 | + </Toggle> |
| 68 | + <Toggle |
| 69 | + checked={!!urlParams.readOnly} |
| 70 | + onChange={({ detail }) => setUrlParams({ readOnly: detail.checked })} |
| 71 | + > |
| 72 | + Read-only |
| 73 | + </Toggle> |
| 74 | + <Toggle |
| 75 | + checked={!!urlParams.disabled} |
| 76 | + onChange={({ detail }) => setUrlParams({ disabled: detail.checked })} |
| 77 | + > |
| 78 | + Disabled |
| 79 | + </Toggle> |
| 80 | + <Toggle checked={hasValue} onChange={({ detail }) => setUrlParams({ hasValue: detail.checked })}> |
| 81 | + Has initial value |
| 82 | + </Toggle> |
| 83 | + </SpaceBetween> |
| 84 | + |
| 85 | + <SpaceBetween direction="horizontal" size="s"> |
| 86 | + <div style={{ minWidth: 200 }}> |
| 87 | + <Select |
| 88 | + inlineLabelText="Input format" |
| 89 | + options={inputFormatOptions} |
| 90 | + selectedOption={inputFormatOptions.find(o => o.value === urlParams.inputFormat) ?? null} |
| 91 | + onChange={({ detail }) => |
| 92 | + setUrlParams({ inputFormat: detail.selectedOption.value as DatePickerProps.InputFormat }) |
| 93 | + } |
| 94 | + /> |
| 95 | + </div> |
| 96 | + |
| 97 | + <div style={{ minWidth: 200 }}> |
| 98 | + <Select |
| 99 | + inlineLabelText="Format" |
| 100 | + options={formatOptions} |
| 101 | + selectedOption={formatOptions.find(o => o.value === urlParams.format) ?? null} |
| 102 | + onChange={({ detail }) => |
| 103 | + setUrlParams({ format: detail.selectedOption.value as DatePickerProps.Format }) |
| 104 | + } |
| 105 | + /> |
| 106 | + </div> |
| 107 | + |
| 108 | + <div style={{ minWidth: 200 }}> |
| 109 | + <Select |
| 110 | + inlineLabelText="Locale" |
| 111 | + options={localeOptions} |
| 112 | + selectedOption={localeOptions.find(o => o.value === urlParams.locale) ?? null} |
| 113 | + onChange={({ detail }) => setUrlParams({ locale: detail.selectedOption.value })} |
| 114 | + /> |
| 115 | + </div> |
| 116 | + </SpaceBetween> |
50 | 117 | </SpaceBetween>
|
| 118 | + |
51 | 119 | <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.`}> |
| 120 | + |
| 121 | + <FormField |
| 122 | + label="Certificate expiry date" |
| 123 | + constraintText={`Use YYYY/MM${urlParams.granularity === 'month' ? '' : '/DD'} format.`} |
| 124 | + > |
54 | 125 | <DatePicker
|
55 | 126 | value={value}
|
56 |
| - name={'date-picker-name'} |
57 |
| - granularity={monthOnly ? 'month' : 'day'} |
58 |
| - locale="en-GB" |
| 127 | + name="date-picker-name" |
| 128 | + placeholder={getPlaceholder({ |
| 129 | + granularity: urlParams.granularity ?? 'day', |
| 130 | + inputFormat: urlParams.inputFormat, |
| 131 | + format: urlParams.format, |
| 132 | + })} |
59 | 133 | i18nStrings={i18nStrings}
|
60 | 134 | openCalendarAriaLabel={selectedDate =>
|
61 | 135 | 'Choose certificate expiry date' + (selectedDate ? `, selected date is ${selectedDate}` : '')
|
62 | 136 | }
|
63 |
| - placeholder={monthOnly ? 'YYYY/MM' : 'YYYY/MM/DD'} |
64 | 137 | onChange={e => setValue(e.detail.value)}
|
| 138 | + {...urlParams} |
65 | 139 | />
|
66 | 140 | </FormField>
|
67 |
| - <br /> |
68 |
| - <br /> |
| 141 | + |
69 | 142 | <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> |
| 143 | + |
| 144 | + <Box variant="code">Raw value (iso): {JSON.stringify(value, undefined, 2)}</Box> |
72 | 145 | </SpaceBetween>
|
73 | 146 | </Box>
|
74 | 147 | );
|
|
0 commit comments