diff --git a/pages/date-input/simple.page.tsx b/pages/date-input/simple.page.tsx index ab73e7fc8a..11582900af 100644 --- a/pages/date-input/simple.page.tsx +++ b/pages/date-input/simple.page.tsx @@ -1,24 +1,54 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import React, { useState } from 'react'; +import React, { useContext, useEffect, useState } from 'react'; -import { Box } from '~components'; -import DateInput from '~components/date-input'; +import { Box, Checkbox, SpaceBetween } from '~components'; +import DateInput, { DateInputProps } from '~components/date-input'; + +import { AppContextType } from '../app/app-context'; +import AppContext from '../app/app-context'; + +export type DateInputDemoContext = React.Context< + AppContextType<{ + monthOnly?: boolean; + hasValue?: boolean; + }> +>; export default function DateInputScenario() { - const [value, setValue] = useState(''); + const { urlParams, setUrlParams } = useContext(AppContext as DateInputDemoContext); + const initValue = '2025-02-14'; + const hasValue = urlParams.hasValue ?? false; + const [value, setValue] = useState(''); + + useEffect(() => { + if (hasValue) { + setValue(initValue); + } else { + setValue(''); + } + }, [hasValue]); return ( -

Date input

- setValue(event.detail.value)} - value={value} - /> + +

Date input

+ + setUrlParams({ hasValue: detail.checked })}> + Has initial value + + + setValue(e.detail.value)} + value={value} + /> + Raw value +
{JSON.stringify(value, undefined, 2)}
+
); } diff --git a/pages/date-picker/simple.page.tsx b/pages/date-picker/simple.page.tsx index b4cf7d1e5b..81f430cae0 100644 --- a/pages/date-picker/simple.page.tsx +++ b/pages/date-picker/simple.page.tsx @@ -1,35 +1,75 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import React, { useState } from 'react'; +import React, { useContext, useEffect, useState } from 'react'; -import { Box, DatePicker, FormField, Link } from '~components'; +import { Box, Checkbox, DatePicker, DatePickerProps, FormField, Link, SpaceBetween } from '~components'; +import { AppContextType } from '../app/app-context'; +import AppContext from '../app/app-context'; import i18nStrings from './i18n-strings'; -export default function DatePickerScenario() { - const [value, setValue] = useState(''); +export type DatePickerDemoContext = React.Context< + AppContextType<{ + monthOnly?: boolean; + hasValue?: boolean; + }> +>; + +export const dateRangePickerDemoDefaults = { + monthOnly: false, + hasValue: false, +}; + +export default function DateInputScenario() { + const { urlParams, setUrlParams } = useContext(AppContext as DatePickerDemoContext); + const monthOnly = urlParams.monthOnly ?? dateRangePickerDemoDefaults.monthOnly; + const hasValue = urlParams.hasValue ?? dateRangePickerDemoDefaults.hasValue; + + const [value, setValue] = useState(''); + + useEffect(() => { + const initValue = monthOnly ? '2025-02' : '2025-02-00'; + if (hasValue) { + setValue(initValue); + } else { + setValue(''); + } + }, [hasValue, monthOnly]); return ( -

Date picker simple version

- Focusable element before the date picker -
- - - 'Choose certificate expiry date' + (selectedDate ? `, selected date is ${selectedDate}` : '') - } - placeholder={'YYYY/MM/DD'} - onChange={event => setValue(event.detail.value)} - /> - -
-
- Focusable element after the date picker + +

Date picker simple version

+ + setUrlParams({ monthOnly: detail.checked })}> + Month-only + + setUrlParams({ hasValue: detail.checked })}> + Has initial value + + + Focusable element before the date picker +
+ + + 'Choose certificate expiry date' + (selectedDate ? `, selected date is ${selectedDate}` : '') + } + placeholder={monthOnly ? 'YYYY/MM' : 'YYYY/MM/DD'} + onChange={e => setValue(e.detail.value)} + /> + +
+
+ Focusable element after the date picker + Raw value +
{JSON.stringify(value, undefined, 2)}
+
); }