From a69a8aa8101eb497606e68649533511548f53258 Mon Sep 17 00:00:00 2001 From: Joakim Bjerknes Date: Fri, 15 Nov 2024 08:41:43 +0100 Subject: [PATCH] refactor(DatePicker): convert properties to camel case and deprecate those with snake case (#4273) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tobias Høegh Co-authored-by: Anders --- .../uilib/components/date-picker/Examples.tsx | 114 +- .../uilib/components/date-picker/events.mdx | 15 +- .../components/date-picker/properties.mdx | 49 +- .../src/components/date-picker/DatePicker.tsx | 1007 ++++++++++------- .../date-picker/DatePickerAddon.tsx | 1 + .../components/date-picker/DatePickerCalc.ts | 10 +- .../date-picker/DatePickerCalendar.tsx | 20 +- .../date-picker/DatePickerContext.ts | 4 +- .../components/date-picker/DatePickerDocs.ts | 275 +++++ .../date-picker/DatePickerFooter.tsx | 40 +- .../date-picker/DatePickerInput.tsx | 50 +- .../date-picker/DatePickerProvider.tsx | 57 +- .../date-picker/__tests__/DatePicker.test.tsx | 485 ++++---- ...atepicker-have-to-match-the-sizes.snap.png | Bin 17326 -> 17545 bytes .../components/date-picker/hooks/useDates.ts | 14 +- .../stories/DatePicker.stories.tsx | 226 ++-- .../extensions/forms/Field/Expiry/Expiry.tsx | 2 +- packages/dnb-eufemia/src/shared/Context.tsx | 4 +- .../dnb-eufemia/src/shared/locales/en-GB.ts | 33 +- .../dnb-eufemia/src/shared/locales/en-US.ts | 8 +- .../dnb-eufemia/src/shared/locales/index.ts | 7 +- .../dnb-eufemia/src/shared/locales/nb-NO.ts | 33 +- 22 files changed, 1455 insertions(+), 999 deletions(-) create mode 100644 packages/dnb-eufemia/src/components/date-picker/DatePickerDocs.ts diff --git a/packages/dnb-design-system-portal/src/docs/uilib/components/date-picker/Examples.tsx b/packages/dnb-design-system-portal/src/docs/uilib/components/date-picker/Examples.tsx index b078ef05d86..e6310c04a42 100644 --- a/packages/dnb-design-system-portal/src/docs/uilib/components/date-picker/Examples.tsx +++ b/packages/dnb-design-system-portal/src/docs/uilib/components/date-picker/Examples.tsx @@ -38,18 +38,18 @@ export const DatePickerRange = () => ( > { - console.log('on_change', start_date, end_date) + showInput={true} + onChange={({ start_date, end_date }) => { + console.log('onChange', start_date, end_date) }} - on_submit={({ start_date, end_date }) => { - console.log('on_submit', start_date, end_date) + onSubmit={({ start_date, end_date }) => { + console.log('onSubmit', start_date, end_date) }} - on_cancel={({ start_date, end_date }) => { - console.log('on_cancel', start_date, end_date) + onCancel={({ start_date, end_date }) => { + console.log('onCancel', start_date, end_date) }} onBlur={({ start_date, @@ -96,14 +96,14 @@ export const DatePickerWithInput = () => ( { - console.log('on_change', date) + showInput={true} + showCancelButton={true} + showResetButton={true} + onChange={({ date }) => { + console.log('onChange', date) }} - on_cancel={({ date }) => { - console.log('on_cancel', date) + onCancel={({ date }) => { + console.log('onCancel', date) }} onBlur={({ date }) => { console.log('onBlur', date) @@ -118,12 +118,12 @@ export const DatePickerTrigger = () => ( { - console.log('on_change', date) + returnFormat="dd-MM-yyyy" + onChange={({ date }) => { + console.log('onChange', date) }} - on_show={({ date }) => { - console.log('on_show', date) + onShow={({ date }) => { + console.log('onShow', date) }} onBlur={({ start_date, end_date }) => { console.log('onBlur', start_date, end_date) @@ -138,17 +138,17 @@ export const DatePickerHiddenNav = () => ( { - console.log('on_change', date) + minDate="2022/05/01" + maxDate="2022/05/17" + dateFormat="yyyy/MM/dd" + returnFormat="dd/MM/yyyy" + hideNavigation={true} + hideDays={true} + onChange={({ date }) => { + console.log('onChange', date) }} - on_hide={({ date }) => { - console.log('on_hide', date) + onHide={({ date }) => { + console.log('onHide', date) }} onBlur={({ date }) => { console.log('onBlur', date) @@ -162,8 +162,8 @@ export const DatePickerMonthOnly = () => ( ) @@ -173,9 +173,9 @@ export const DatePickerStatusMessage = () => ( ) @@ -185,7 +185,7 @@ export const DatePickerSuffix = () => ( Modal content} /> @@ -194,7 +194,7 @@ export const DatePickerSuffix = () => ( export const DatePickerLinked = () => ( - + ) @@ -205,7 +205,7 @@ export const DatePickerNoInputStatus = () => ( @@ -218,8 +218,8 @@ export const DatePickerErrorMessage = () => ( @@ -236,7 +236,7 @@ export const DatePickerErrorStatus = () => ( @@ -247,11 +247,11 @@ export const DatePickerCalendar = () => ( @@ -261,30 +261,30 @@ export const DatePickerScreenshotTestSizes = () => { return ( - + @@ -297,10 +297,10 @@ export const DatePickerScreenshotTestDisabled = () => { return ( - + - + @@ -349,7 +349,7 @@ export const DatePickerDateFnsRange = () => ( export const DatePickerDateFnsRangeIsWeekend = () => ( { + onDaysRender={(days, calendarNumber = 0) => { return days.map((dayObject) => { if (isWeekend(dayObject.date)) { dayObject.isInactive = true @@ -365,10 +365,10 @@ export const DatePickerDateFnsRangeIsWeekend = () => ( export const DatePickerCorrectInvalidDate = () => ( ) diff --git a/packages/dnb-design-system-portal/src/docs/uilib/components/date-picker/events.mdx b/packages/dnb-design-system-portal/src/docs/uilib/components/date-picker/events.mdx index c920f51e946..1f45f7ec3c4 100644 --- a/packages/dnb-design-system-portal/src/docs/uilib/components/date-picker/events.mdx +++ b/packages/dnb-design-system-portal/src/docs/uilib/components/date-picker/events.mdx @@ -3,21 +3,12 @@ showTabs: true --- import { DatePickerDateFnsRangeIsWeekend } from 'Docs/uilib/components/date-picker/Examples' +import PropertiesTable from 'dnb-design-system-portal/src/shared/parts/PropertiesTable' +import { DatePickerEvents } from '@dnb/eufemia/src/components/date-picker/DatePickerDocs' ## Events -| Events | Description | -| ---------------- | ----------------------------------------------------------------------------------------------------------------------- | -| `on_change` | _(optional)_ will be called on a date change event. Returns an `object`. See Returned Object below. | -| `on_type` | _(optional)_ will be called on every input and date picker interaction. Returns an `object`. See Returned Object below. | -| `on_submit` | _(optional)_ will be called once a user presses the submit button. | -| `on_cancel` | _(optional)_ will be called once a user presses the cancel button. | -| `on_reset` | _(optional)_ will be called once a user presses the reset button. | -| `on_show` | _(optional)_ will be called once date-picker is visible. | -| `on_hide` | _(optional)_ will be called once date-picker is hidden. | -| `on_days_render` | _(optional)_ will be called right before every new calendar view gets rendered. See the example above. | -| `onFocus` | _(optional)_ will be called once the input gets focus. | -| `onBlur` | _(optional)_ will be called once the input lose focus. | + ## Returned Object diff --git a/packages/dnb-design-system-portal/src/docs/uilib/components/date-picker/properties.mdx b/packages/dnb-design-system-portal/src/docs/uilib/components/date-picker/properties.mdx index a5812a1694c..c866fc42886 100644 --- a/packages/dnb-design-system-portal/src/docs/uilib/components/date-picker/properties.mdx +++ b/packages/dnb-design-system-portal/src/docs/uilib/components/date-picker/properties.mdx @@ -7,55 +7,12 @@ import { DatePickerDateFns, DatePickerDateFnsRange, } from 'Docs/uilib/components/date-picker/Examples' +import PropertiesTable from 'dnb-design-system-portal/src/shared/parts/PropertiesTable' +import { DatePickerProperties } from '@dnb/eufemia/src/components/date-picker/DatePickerDocs' ## Properties -| Properties | Description | -| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `date` | _(optional)_ defines the pre-filled date by either a JavaScript DateInstance or (ISO 8601) like `date="2019-05-05"`. | -| `start_date` | _(optional)_ to set the pre-filled starting date. Is used if `range={true}` is set to `true`. Defaults to `null`, showing the `mask_placeholder`. | -| `end_date` | _(optional)_ to set the pre-filled ending date. Is used if `range={true}` is set to `true`. Defaults to `null`, showing the `mask_placeholder`. | -| `month` | _(optional)_ to display what month should be shown in the first calendar by default. Defaults to the `date` respective `start_date`. | -| `start_month` | _(optional)_ to display what month should be shown in the first calendar by default. Defaults to the `date` respective `start_date`. | -| `end_month` | _(optional)_ to display what month should be shown in the second calendar by default. Defaults to the `date` respective `start_date`. | -| `min_date` | _(optional)_ to limit a date range to a minimum `start_date`. Defaults to `null`. | -| `max_date` | _(optional)_ to limit a date range to a maximum `end_date`. Defaults to `null`. | -| `date_format` | _(optional)_ Defines how the property dates (`date`, `start_date` and `end_date`) should be parsed, e.g. `yyyy/MM/dd`. Defaults to `yyyy-MM-dd`. | -| `return_format` | _(optional)_ Defines how the returned date, as a string, should be formatted as. Defaults to `yyyy-MM-dd`. | -| `range` | _(optional)_ if the date picker should support a range of two dates (starting and ending date). Defaults to `false`. | -| `show_input` | _(optional)_ if the input fields with the mask should be visible. Defaults to `false`. | -| `mask_order` | _(optional)_ to define the order of the masked placeholder input fields. Defaults to `dd/mm/yyyy` | -| `opened` | _(optional)_ to open the date-picker by default. Defaults to `false`. | -| `mask_placeholder` | _(optional)_ to display the placeholder on input. Defaults to `dd/mm/åååå`. | -| `hide_navigation` | _(optional)_ if set to `true`, the navigation will be hidden. Defaults to `false`. | -| `hide_days` | _(optional)_ if set to `true`, the week days will be hidden. Defaults to `false`. | -| `show_submit_button` | _(optional)_ if set to `true`, a submit button will be shown. You can change the default text by using `submit_button_text="Ok"`. Defaults to `false`. If the `range` property is `true`, then the submit button is shown. | -| `show_cancel_button` | _(optional)_ if set to `true`, a cancel button will be shown. You can change the default text by using `cancel_button_text="Avbryt"` Defaults to `false`. If the `range` property is `true`, then the cancel button is shown. | -| `show_reset_button` | _(optional)_ if set to `true`, a reset button will be shown. You can change the default text by using `reset_button_text="Tilbakestill"` Defaults to `false`. | -| `link` | _(optional)_ link both calendars, once to the user is navigating between months. Only meant to use if the range is set to `true`. Defaults to `false`. | -| `sync` | _(optional)_ sync input values with the calendars views. Once the input values get changed, the calendar changes its views in sync. Defaults to `true`. | -| `first_day` | _(optional)_ to define the first day of the week. Defaults to `monday`. | -| `align_picker` | _(optional)_ use `right` to change the calendar alignment direction. Defaults to `left`. | -| `only_month` | _(optional)_ use `true` to only show the defined month. Disables the month navigation possibility. Defaults to `false`. | -| `hide_last_week` | _(optional)_ use `true` to only show the last week in the current month if it needs to be shown. The result is that there will mainly be shows five (5) weeks (rows) instead of six (6). Defaults to `false`. | -| `stretch` | _(optional)_ if set to `true`, then the date-picker input field will be 100% in `width`. | -| `label` | _(optional)_ a prepending label in sync with the date input field. | -| `label_direction` | _(optional)_ use `label_direction="vertical"` to change the label layout direction. Defaults to `horizontal`. | -| `suffix` | _(optional)_ text describing the content of the DatePicker more than the label. You can also send in a React component, so it gets wrapped inside the DatePicker component. | -| `label_sr_only` | _(optional)_ use `true` to make the label only readable by screen readers. | -| `shortcuts` | _(optional)_ gives you the possibility to set predefined dates and date ranges so the user can select these by one click. Define either a JSON or an object with the defined shortcuts. More info is below. | -| `addon_element` | _(optional)_ gives you the possibility to inject a React element showing up over the footer. Use it to customize `shortcuts`. | -| `input_element` | _(optional)_ gives you the possibility to use a plain/vanilla `` HTML element by defining it as a string `input_element="input"`, a React element, or a render function `input_element={(internalProps) => ()}`. Can also be used in circumstances where the `react-text-mask` should not be used, e.g. in testing environments. Defaults to custom masked input. | -| `status` | _(optional)_ text with a status message. The style defaults to an error message. You can use `true` to only get the status color, without a message. | -| `status_state` | _(optional)_ defines the state of the status. Currently, there are two statuses `[error, info]`. Defaults to `error`. | -| `status_props` | _(optional)_ use an object to define additional FormStatus properties. | -| `disable_autofocus` | _(optional)_ once the date picker gets opened, there is a focus handling to ensure good accessibility. This can be disabled with this property. Defaults to `false`. | -| `correct_invalid_date` | _(optional)_ corrects the input date value to be the same as either `min_date` or `max_date`, when the user types in a date that is either before or after one of these. Defaults to `false`. | -| `globalStatus` | _(optional)_ the [configuration](/uilib/components/global-status/properties/#configuration-object) used for the target [GlobalStatus](/uilib/components/global-status). | -| `tooltip` | _(optional)_ Provide a short Tooltip content that shows up on the picker button. | -| `skeleton` | _(optional)_ if set to `true`, an overlaying skeleton with animation will be shown. | -| `size` | _(optional)_ the sizes you can choose is `small` (1.5rem), `default` (2rem), `medium` (2.5rem) and `large` (3rem) are supported component sizes. Defaults to `default` / `null`. | -| [Space](/uilib/layout/space/properties) | _(optional)_ spacing properties like `top` or `bottom` are supported. | + ## Translations diff --git a/packages/dnb-eufemia/src/components/date-picker/DatePicker.tsx b/packages/dnb-eufemia/src/components/date-picker/DatePicker.tsx index 36f30909126..3e13b9d0aba 100644 --- a/packages/dnb-eufemia/src/components/date-picker/DatePicker.tsx +++ b/packages/dnb-eufemia/src/components/date-picker/DatePicker.tsx @@ -55,6 +55,12 @@ import { CalendarDay, DatePickerCalendarProps } from './DatePickerCalendar' import { DatePickerContextValues, DateType } from './DatePickerContext' import { DatePickerDates } from './hooks/useDates' import { useTranslation } from '../../shared' +import { convertSnakeCaseProps } from '../../shared/helpers/withSnakeCaseProps' +import { + TranslationsEnGB, + TranslationsEnUS, + TranslationsNbNO, +} from '../../shared/locales' export type DatePickerEventAttributes = { day?: string @@ -76,314 +82,529 @@ export type DisplayPickerEvent = ( event?: React.MouseEvent } -export type DatePickerProps = Omit< - React.HTMLProps, - 'ref' | 'children' | 'label' | 'size' | 'onBlur' | 'onFocus' | 'start' -> & - SpacingProps & { - /** - * Defines the pre-filled date by either a JavaScript DateInstance or (ISO 8601) like `date="2019-05-05"`. - */ - date?: DateType - /** - * To set the pre-filled starting date. Is used if `range={true}` is set to `true`. Defaults to `null`, showing the `mask_placeholder`. - */ - start_date?: DateType - /** - * To set the pre-filled ending date. Is used if `range={true}` is set to `true`. Defaults to `null`, showing the `mask_placeholder`. - */ - end_date?: DateType - /** - * To display what month should be shown in the first calendar by default. Defaults to the `date` respective `start_date`. - */ - month?: DateType - /** - * To display what month should be shown in the first calendar by default. Defaults to the `date` respective `start_date`. - */ - start_month?: DateType - /** - * To display what month should be shown in the second calendar by default. Defaults to the `date` respective `start_date`. - */ - end_month?: DateType - /** - * To limit a date range to a minimum `start_date`. Defaults to `null`. - */ - min_date?: DateType - /** - * To limit a date range to a maximum `end_date`. Defaults to `null`. - */ - max_date?: DateType - /** - * Corrects the input date value to be the same as either `min_date` or `max_date`, when the user types in a date that is either before or after one of these. Defaults to `false`. - */ - correct_invalid_date?: boolean - /** - * To define the order of the masked placeholder input fields. Defaults to `dd/mm/yyyy` - */ - mask_order?: string - /** - * To display the placeholder on input. Defaults to `dd/mm/åååå`. - */ - mask_placeholder?: string - /** - * Defines how the prop dates (`date`, `start_date` and `end_date`) should be parsed, e.g. `yyyy/MM/dd`. Defaults to `yyyy-MM-dd`. - */ - date_format?: string - /** - * Defines how the returned date, as a string, should be formatted as. Defaults to `yyyy-MM-dd`. - */ - return_format?: string - /** - * If set to `true`, the navigation will be hidden. Defaults to `false`. - */ - hide_navigation?: boolean - hide_navigation_buttons?: boolean - /** - * If set to `true`, the week days will be hidden. Defaults to `false`. - */ - hide_days?: boolean - /** - * Use `true` to only show the defined month. Disables the month navigation possibility. Defaults to `false`. - */ - only_month?: boolean - /** - * Use `true` to only show the last week in the current month if it needs to be shown. The result is that there will mainly be shows five (5) weeks (rows) instead of six (6). Defaults to `false`. - */ - hide_last_week?: boolean - /** - * Once the date picker gets opened, there is a focus handling to ensure good accessibility. can be disabled with property. Defaults to `false`. - */ - disable_autofocus?: boolean - enable_keyboard_nav?: boolean - /** - * If the input fields with the mask should be visible. Defaults to `false`. - */ - show_input?: boolean - /** - * If set to `true`, a submit button will be shown. You can change the default text by using `submit_button_text="Ok"`. Defaults to `false`. If the `range` prop is `true`, then the submit button is shown. - */ - show_submit_button?: boolean - /** - * If set to `true`, a cancel button will be shown. You can change the default text by using `cancel_button_text="Avbryt"` Defaults to `false`. If the `range` prop is `true`, then the cancel button is shown. - */ - show_cancel_button?: boolean - /** - * If set to `true`, a reset button will be shown. You can change the default text by using `reset_button_text="Tilbakestill"` Defaults to `false`. - */ - show_reset_button?: boolean - submit_button_text?: string - cancel_button_text?: string - reset_button_text?: string - reset_date?: boolean - /** - * To define the first day of the week. Defaults to `monday`. - */ - first_day?: string - /** - * @deprecated set locale with `Provider` instead. - */ - locale?: Locale - /** - * If the date picker should support a range of two dates (starting and ending date). Defaults to `false`. - */ - range?: boolean - /** - * Link both calendars, once to the user is navigating between months. Only meant to use if the range is set to `true`. Defaults to `false`. - */ - link?: boolean - /** - * Sync input values with the calendars views. Once the input values get changed, the calendar changes its views in sync. Defaults to `true`. - */ - sync?: boolean - /** - * A prepending label in sync with the date input field. - */ - label?: React.ReactNode - /** - * Use `label_direction="vertical"` to change the label layout direction. Defaults to `horizontal`. - */ - label_direction?: 'vertical' | 'horizontal' - /** - * Use `true` to make the label only readable by screen readers. - */ - label_sr_only?: boolean - /** - * Gives you the possibility to use a plain/vanilla `` HTML element by defining it as a string `input_element="input"`, a React element, or a render function `input_element={(internalProps) => ()}`. Can also be used in circumstances where the `react-text-mask` not should be used, e.g. in testing environments. Defaults to custom masked input. - */ - input_element?: InputInputElement - /** - * Gives you the possibility to inject a React element showing up over the footer. Use it to customize `shortcuts`. - */ - addon_element?: React.ReactNode - /** - * Gives you the possibility to set predefined dates and date ranges so the user can select these by one click. Define either a JSON or an object with the defined shortcuts. More info is below. - */ - shortcuts?: DatePickerAddonProps['shortcuts'] - disabled?: boolean - /** - * If set to `true`, then the date-picker input field will be 100% in `width`. - */ - stretch?: boolean - /** - * If set to `true`, an overlaying skeleton with animation will be shown. - */ - skeleton?: SkeletonShow - /** - * The sizes you can choose is `small` (1.5rem), `default` (2rem), `medium` (2.5rem) and `large` (3rem) are supported component sizes. Defaults to `default` / `null`. - */ - size?: InputSize - /** - * Text with a status message. The style defaults to an error message. You can use `true` to only get the status color, without a message. - */ - status?: FormStatusText - /** - * Defines the state of the status. Currently, there are two statuses `[error, info]`. Defaults to `error`. - */ - status_state?: FormStatusState - /** - * Use an object to define additional FormStatus properties. - */ - status_props?: FormStatusProps - status_no_animation?: boolean - /** - * The configuration used for the target GlobalStatus. - */ - globalStatus?: GlobalStatusConfigObject - /** - * Text describing the content of the DatePicker more than the label. You can also send in a React component, so it gets wrapped inside the DatePicker component. - */ - suffix?: React.ReactNode - /** - * To open the date-picker by default. Defaults to `false`. - */ - opened?: boolean - /** - * Provide a short Tooltip content that shows up on the picker button. - */ - tooltip?: React.ReactNode - tabIndex?: number - prevent_close?: boolean - no_animation?: boolean - direction?: 'auto' | 'top' | 'bottom' - /** - * Use `right` to change the calendar alignment direction. Defaults to `left`. - */ - align_picker?: 'auto' | 'left' | 'right' - className?: string - /** - * Will be called right before every new calendar view gets rendered. See the example above. - */ - on_days_render?: ( - days: Array, - nr?: DatePickerCalendarProps['nr'] - ) => void - /** - * Will be called on a date change event. Returns an `object`. See Returned Object below. - */ - on_change?: ( - event: DatePickerEvent> - ) => void - /** - * Will be called on every input and date picker interaction. Returns an `object`. See Returned Object below. - */ - on_type?: ( - event: DatePickerEvent> - ) => void - /** - * Will be called once date-picker is visible. - */ - on_show?: (event: DatePickerEvent) => void - /** - * Will be called once date-picker is hidden. - */ - on_hide?: (event: DatePickerEvent) => void - /** - * Will be called once a user presses the submit button. - */ - on_submit?: ( - event: DatePickerEvent> - ) => void - /** - * Will be called once a user presses the cancel button. - */ - on_cancel?: ( - event: DatePickerEvent> - ) => void - /** - * Will be called once a user presses the reset button. - */ - on_reset?: ( - event: DatePickerEvent> - ) => void - /** - * Will be called once the input gets focus. - */ - onFocus?: ( - event: DatePickerEvent> - ) => void - /** - * Will be called once the input lose focus. - */ - onBlur?: ( - event: DatePickerEvent> - ) => void - } +export type DatePickerProps = { + /** + * Defines the pre-filled date by either a JavaScript DateInstance or (ISO 8601) like `date="2019-05-05"`. + */ + date?: DateType + /** + * To set the pre-filled starting date. Is used if `range={true}` is set to `true`. Defaults to `null`, showing the `maskPlaceholder`. + */ + startDate?: DateType + /** + * To set the pre-filled ending date. Is used if `range={true}` is set to `true`. Defaults to `null`, showing the `maskPlaceholder`. + */ + endDate?: DateType + /** + * To display what month should be shown in the first calendar by default. Defaults to the `date` respective `startDate`. + */ + month?: DateType + /** + * To display what month should be shown in the first calendar by default. Defaults to the `date` respective `startDate`. + */ + startMonth?: DateType + /** + * To display what month should be shown in the second calendar by default. Defaults to the `date` respective `startDate`. + */ + endMonth?: DateType + /** + * To limit a date range to a minimum `startDate`. Defaults to `null`. + */ + minDate?: DateType + /** + * To limit a date range to a maximum `endDate`. Defaults to `null`. + */ + maxDate?: DateType + /** + * Corrects the input date value to be the same as either `minDate` or `maxDate`, when the user types in a date that is either before or after one of these. Defaults to `false`. + */ + correctInvalidDate?: boolean + /** + * To define the order of the masked placeholder input fields. Defaults to `dd/mm/yyyy` + */ + maskOrder?: string + /** + * To display the placeholder on input. Defaults to `dd/mm/åååå`. + */ + maskPlaceholder?: string + /** + * Defines how the prop dates (`date`, `startDate` and `endDate`) should be parsed, e.g. `yyyy/MM/dd`. Defaults to `yyyy-MM-dd`. + */ + dateFormat?: string + /** + * Defines how the returned date, as a string, should be formatted as. Defaults to `yyyy-MM-dd`. + */ + returnFormat?: string + /** + * If set to `true`, the navigation will be hidden. Defaults to `false`. + */ + hideNavigation?: boolean + hideNavigationButtons?: boolean + /** + * If set to `true`, the week days will be hidden. Defaults to `false`. + */ + hideDays?: boolean + /** + * Use `true` to only show the defined month. Disables the month navigation possibility. Defaults to `false`. + */ + onlyMonth?: boolean + /** + * Use `true` to only show the last week in the current month if it needs to be shown. The result is that there will mainly be shows five (5) weeks (rows) instead of six (6). Defaults to `false`. + */ + hideLastWeek?: boolean + /** + * Once the date picker gets opened, there is a focus handling to ensure good accessibility. can be disabled with property. Defaults to `false`. + */ + disableAutofocus?: boolean + enableKeyboardNav?: boolean + /** + * If the input fields with the mask should be visible. Defaults to `false`. + */ + showInput?: boolean + /** + * If set to `true`, a submit button will be shown. You can change the default text by using `submitButtonText="Ok"`. Defaults to `false`. If the `range` prop is `true`, then the submit button is shown. + */ + showSubmitButton?: boolean + /** + * If set to `true`, a cancel button will be shown. You can change the default text by using `cancelButtonText="Avbryt"` Defaults to `false`. If the `range` prop is `true`, then the cancel button is shown. + */ + showCancelButton?: boolean + /** + * If set to `true`, a reset button will be shown. You can change the default text by using `resetButtonText="Tilbakestill"` Defaults to `false`. + */ + showResetButton?: boolean + submitButtonText?: string + cancelButtonText?: string + resetButtonText?: string + resetDate?: boolean + /** + * To define the first day of the week. Defaults to `monday`. + */ + firstDay?: + | 'monday' + | 'tuesday' + | 'wednesday' + | 'thursday' + | 'friday' + | 'saturday' + | 'sunday' + /** + * If the date picker should support a range of two dates (starting and ending date). Defaults to `false`. + */ + range?: boolean + /** + * Link both calendars, once to the user is navigating between months. Only meant to use if the range is set to `true`. Defaults to `false`. + */ + link?: boolean + /** + * Sync input values with the calendars views. Once the input values get changed, the calendar changes its views in sync. Defaults to `true`. + */ + sync?: boolean + /** + * A prepending label in sync with the date input field. + */ + label?: React.ReactNode + /** + * Use `labelDirection="vertical"` to change the label layout direction. Defaults to `horizontal`. + */ + labelDirection?: 'vertical' | 'horizontal' + /** + * Use `true` to make the label only readable by screen readers. + */ + labelSrOnly?: boolean + /** + * Gives you the possibility to use a plain/vanilla `` HTML element by defining it as a string `inputElement="input"`, a React element, or a render function `inputElement={(internalProps) => ()}`. Can also be used in circumstances where the `react-text-mask` not should be used, e.g. in testing environments. Defaults to custom masked input. + */ + inputElement?: InputInputElement + /** + * Gives you the possibility to inject a React element showing up over the footer. Use it to customize `shortcuts`. + */ + addonElement?: React.ReactNode + /** + * Gives you the possibility to set predefined dates and date ranges so the user can select these by one click. Define either a JSON or an object with the defined shortcuts. More info is below. + */ + shortcuts?: DatePickerAddonProps['shortcuts'] + disabled?: boolean + /** + * If set to `true`, then the date-picker input field will be 100% in `width`. + */ + stretch?: boolean + /** + * If set to `true`, an overlaying skeleton with animation will be shown. + */ + skeleton?: SkeletonShow + /** + * The sizes you can choose is `small` (1.5rem), `default` (2rem), `medium` (2.5rem) and `large` (3rem) are supported component sizes. Defaults to `default` / `null`. + */ + size?: InputSize + /** + * Text with a status message. The style defaults to an error message. You can use `true` to only get the status color, without a message. + */ + status?: FormStatusText + /** + * Defines the state of the status. Currently, there are two statuses `[error, info]`. Defaults to `error`. + */ + statusState?: FormStatusState + /** + * Use an object to define additional FormStatus properties. + */ + statusProps?: FormStatusProps + statusNoAnimation?: boolean + /** + * The configuration used for the target GlobalStatus. + */ + globalStatus?: GlobalStatusConfigObject + /** + * Text describing the content of the DatePicker more than the label. You can also send in a React component, so it gets wrapped inside the DatePicker component. + */ + suffix?: React.ReactNode + /** + * To open the date-picker by default. Defaults to `false`. + */ + opened?: boolean + /** + * Provide a short Tooltip content that shows up on the picker button. + */ + tooltip?: React.ReactNode + tabIndex?: number + preventClose?: boolean + noAnimation?: boolean + direction?: 'auto' | 'top' | 'bottom' + /** + * Use `right` to change the calendar alignment direction. Defaults to `left`. + */ + alignPicker?: 'auto' | 'left' | 'right' + className?: string + /** + * Will be called right before every new calendar view gets rendered. See the example above. + */ + onDaysRender?: ( + days: Array, + nr?: DatePickerCalendarProps['nr'] + ) => void + /** + * Will be called on a date change event. Returns an `object`. See Returned Object below. + */ + onChange?: ( + event: DatePickerEvent> + ) => void + /** + * Will be called on every input and date picker interaction. Returns an `object`. See Returned Object below. + */ + onType?: ( + event: DatePickerEvent> + ) => void + /** + * Will be called once date-picker is visible. + */ + onShow?: (event: DatePickerEvent) => void + /** + * Will be called once date-picker is hidden. + */ + onHide?: (event: DatePickerEvent) => void + /** + * Will be called once a user presses the submit button. + */ + onSubmit?: ( + event: DatePickerEvent> + ) => void + /** + * Will be called once a user presses the cancel button. + */ + onCancel?: ( + event: DatePickerEvent> + ) => void + /** + * Will be called once a user presses the reset button. + */ + onReset?: ( + event: DatePickerEvent> + ) => void + /** + * Will be called once the input gets focus. + */ + onFocus?: (event: DatePickerEvent>) => void + /** + * Will be called once the input lose focus. + */ + onBlur?: (event: DatePickerEvent>) => void +} +// Can be removed in v11 +type DatePickerDeprecatedProps = { + /** + * @deprecated use `startDate` instead. + */ + start_date?: DateType + /** + * @deprecated use `endDate` instead. + */ + end_date?: DateType + /** + * @deprecated use `startMonth` instead. + */ + start_month?: DateType + /** + * @deprecated use `endMonth` instead. + */ + end_month?: DateType + /** + * @deprecated use `minDate` instead. + */ + min_date?: DateType + /** + * @deprecated use `maxDate` instead. + */ + max_date?: DateType + /** + * @deprecated use `correctInvalidDate` instead. + */ + correct_invalid_date?: boolean + /** + * @deprecated use `maskOrder` instead. + */ + mask_order?: string + /** + * @deprecated use `maskPlaceholder` instead. + */ + mask_placeholder?: string + /** + * @deprecated use `dateFormat` instead. + */ + date_format?: string + /** + * @deprecated use `returnFormat` instead. + */ + return_format?: string + /** + * @deprecated use `hideNavigation` instead. + */ + hide_navigation?: boolean + /** + * @deprecated use `hideNavigationButtons` instead. + */ + hide_navigation_buttons?: boolean + /** + * @deprecated use `hideDays` instead. + */ + hide_days?: boolean + /** + * @deprecated use `onlyMonth` instead. + */ + only_month?: boolean + /** + * @deprecated use `hideLastWeek` instead. + */ + hide_last_week?: boolean + /** + * @deprecated use `disableAutofocus` instead. + */ + disable_autofocus?: boolean + /** + * @deprecated use `enableKeyboardNav` instead. + */ + enable_keyboard_nav?: boolean + /** + * @deprecated use `showInput` instead. + */ + show_input?: boolean + /** + * @deprecated use `showSubmitButton` instead. + */ + show_submit_button?: boolean + /** + * @deprecated use `showCancelButton` instead. + */ + show_cancel_button?: boolean + /** + * @deprecated + */ + show_reset_button?: boolean + /** + * @deprecated use `submitButtonText` instead. + */ + submit_button_text?: string + /** + * @deprecated use `cancelButtonText` instead. + */ + cancel_button_text?: string + /** + * @deprecated use `resetButtonText` instead. + */ + reset_button_text?: string + /** + * @deprecated use `resetDate` instead. + */ + reset_date?: boolean + /** + * @deprecated use `firstDay` instead. + */ + first_day?: string + /** + * @deprecated set locale with `Provider` instead. + */ + locale?: Locale + /** + * @deprecated use `labelDirection` instead. + */ + label_direction?: 'vertical' | 'horizontal' + /** + * @deprecated use `labelSrOnly` instead. + */ + label_sr_only?: boolean + /** + * @deprecated use `inputElement` instead. + */ + input_element?: InputInputElement + /** + * @deprecated use `addonElement` instead. + */ + addon_element?: React.ReactNode + /** + * @deprecated use `statusState` instead. + */ + status_state?: FormStatusState + /** + * @deprecated use `statusProps` instead. + */ + status_props?: FormStatusProps + /** + * @deprecated use `statusNoAnimation` instead. + */ + status_no_animation?: boolean + /** + * @deprecated use `preventClose` instead. + */ + prevent_close?: boolean + /** + * @deprecated use `no_animation` instead. + */ + no_animation?: boolean + /** + * @deprecated use `alignPicker` instead. + */ + align_picker?: 'auto' | 'left' | 'right' + /** + * @deprecated use `onDaysRender` instead. + */ + on_days_render?: ( + days: Array, + nr?: DatePickerCalendarProps['nr'] + ) => void + /** + * @deprecated use `onChange` instead. + */ + on_change?: ( + event: DatePickerEvent> + ) => void + /** + * @deprecated use `onType` instead. + */ + on_type?: ( + event: DatePickerEvent> + ) => void + /** + * @deprecated use `onShow` instead. + */ + on_show?: (event: DatePickerEvent) => void + /** + * @deprecated use `onHide` instead. + */ + on_hide?: (event: DatePickerEvent) => void + /** + * @deprecated use `onSubmit` instead. + */ + on_submit?: ( + event: DatePickerEvent> + ) => void + /** + * @deprecated use `onCancel` instead. + */ + on_cancel?: ( + event: DatePickerEvent> + ) => void + /** + * @deprecated use `onReset` instead. + */ + on_reset?: ( + event: DatePickerEvent> + ) => void +} + +export type DatePickerAllProps = DatePickerProps & + DatePickerDeprecatedProps & + SpacingProps & + Omit< + React.HTMLProps, + | 'ref' + | 'children' + | 'label' + | 'size' + | 'onChange' + | 'onBlur' + | 'onFocus' + | 'onSubmit' + | 'start' + > + +// Added to prevent type errors when destructuring the translations from props +type DatePickerTranslations = Partial & + Partial & + Partial const defaultProps: DatePickerProps = { - mask_order: 'dd/mm/yyyy', - mask_placeholder: 'dd/mm/åååå', // have to be same setup as "mask" - but can be like - date_format: 'yyyy-MM-dd', // in v1 of date-fns we were more flexible in terms of the format - return_format: 'yyyy-MM-dd', // used in date-fns v1 - hide_navigation: false, - hide_navigation_buttons: false, - hide_days: false, - only_month: false, - hide_last_week: false, - disable_autofocus: false, - enable_keyboard_nav: false, - show_input: false, - submit_button_text: 'Ok', - cancel_button_text: 'Avbryt', - reset_button_text: 'Tilbakestill', - reset_date: true, - first_day: 'monday', + maskOrder: 'dd/mm/yyyy', + maskPlaceholder: 'dd/mm/åååå', // have to be same setup as "mask" - but can be like + dateFormat: 'yyyy-MM-dd', // in v1 of date-fns we were more flexible in terms of the format + returnFormat: 'yyyy-MM-dd', // used in date-fns v1 + hideNavigation: false, + hideNavigationButtons: false, + hideDays: false, + onlyMonth: false, + hideLastWeek: false, + disableAutofocus: false, + enableKeyboardNav: false, + showInput: false, + submitButtonText: 'Ok', + cancelButtonText: 'Avbryt', + resetButtonText: 'Tilbakestill', + resetDate: true, + firstDay: 'monday', range: false, link: false, sync: true, - status_state: 'error', + statusState: 'error', opened: false, - no_animation: false, + noAnimation: false, direction: 'auto', } -function DatePicker(externalProps: DatePickerProps) { +function DatePicker(externalProps: DatePickerAllProps) { const props = { ...defaultProps, ...externalProps } const { - prevent_close, - on_hide, - on_show, - on_submit, - on_cancel, - on_reset, - no_animation, - show_input, - align_picker, - show_submit_button, - show_cancel_button, + preventClose, + onHide, + onShow, + onSubmit, + onCancel, + onReset, + noAnimation, + showInput, + alignPicker, + showSubmitButton, + showCancelButton, range, - hide_days, - hide_navigation, - opened: propsOpened, - end_date, - } = props + hideDays, + hideNavigation, + opened: openedProp, + endDate: endDateProp, + } = convertSnakeCaseProps(props) // convertSnakeCaseProps - can be removed in v11 - const [opened, setOpened] = useState(propsOpened) + const [opened, setOpened] = useState(openedProp) const [hidden, setHidden] = useState(!opened) - const [startDate, setStartDate] = useState() - const [endDate, setEndDate] = useState() - - const showInput = props.show_input + const [dates, setDates] = useState< + Pick + >({}) const context = useContext(Context) const blurDelay = 201 // some ms more than "dropdownSlideDown 200ms" @@ -399,9 +620,9 @@ function DatePicker(externalProps: DatePickerProps) { const translation = useTranslation().DatePicker - if (end_date && !range) { + if (endDateProp && !range) { warn( - `The DatePicker got a "end_date". You have to set range={true} as well!.` + `The DatePicker got a "endDate". You have to set range={true} as well!.` ) } @@ -413,7 +634,7 @@ function DatePicker(externalProps: DatePickerProps) { const hidePicker = useCallback( (args?: DisplayPickerEvent) => { - if (prevent_close) { + if (preventClose) { return // stop here } @@ -424,7 +645,7 @@ function DatePicker(externalProps: DatePickerProps) { setOpened(false) // Double check and compare return - on_hide?.({ + onHide?.({ ...getReturnObject.current(args), }) @@ -441,12 +662,12 @@ function DatePicker(externalProps: DatePickerProps) { } } }, - no_animation ? 1 : blurDelay + noAnimation ? 1 : blurDelay ) // wait until animation is over removeOutsideClickHandler() }, - [no_animation, prevent_close, on_hide, removeOutsideClickHandler] + [noAnimation, preventClose, onHide, removeOutsideClickHandler] ) const setOutsideClickHandler = useCallback(() => { @@ -460,7 +681,7 @@ function DatePicker(externalProps: DatePickerProps) { const setTrianglePosition = useCallback(() => { const triangleWidth = 16 - if (show_input && triangleRef.current && innerRef.current) { + if (showInput && triangleRef.current && innerRef.current) { try { const shellWidth = innerRef.current .querySelector('.dnb-input__shell') @@ -470,7 +691,7 @@ function DatePicker(externalProps: DatePickerProps) { .querySelector('.dnb-input__submit-button__button') .getBoundingClientRect().width - if (align_picker === 'right') { + if (alignPicker === 'right') { const distance = buttonWidth / 2 - triangleWidth / 2 triangleRef.current.style.marginRight = `${distance / 16}rem` } else { @@ -481,7 +702,7 @@ function DatePicker(externalProps: DatePickerProps) { warn(e) } } - }, [show_input, align_picker]) + }, [showInput, alignPicker]) const showPicker = useCallback( (event?: DisplayPickerEvent) => { @@ -492,16 +713,17 @@ function DatePicker(externalProps: DatePickerProps) { setOpened(true) setHidden(false) - on_show?.({ ...getReturnObject.current(event) }) + onShow?.({ ...getReturnObject.current(event) }) setTrianglePosition() setOutsideClickHandler() }, - [setTrianglePosition, setOutsideClickHandler, on_show] + [setTrianglePosition, setOutsideClickHandler, onShow] ) + // React to opened prop changes useEffect(() => { - if (propsOpened) { + if (openedProp) { showPicker() } @@ -509,7 +731,7 @@ function DatePicker(externalProps: DatePickerProps) { clearTimeout(hideTimeout.current) removeOutsideClickHandler() } - }, [propsOpened, removeOutsideClickHandler, showPicker]) + }, [openedProp, removeOutsideClickHandler, showPicker]) const onPickerChange = useCallback( ({ @@ -519,24 +741,23 @@ function DatePicker(externalProps: DatePickerProps) { | React.MouseEvent | React.KeyboardEvent >) => { - if (shouldHidePicker && !show_submit_button && !show_cancel_button) { + if (shouldHidePicker && !showSubmitButton && !showCancelButton) { hidePicker() } - setStartDate(args.startDate) - setEndDate(args.endDate) + setDates({ startDate: args.startDate, endDate: args.endDate }) }, - [hidePicker, show_submit_button, show_cancel_button] + [hidePicker, showSubmitButton, showCancelButton] ) const onSubmitHandler = useCallback( (event: React.MouseEvent) => { hidePicker(event) - on_submit?.({ + onSubmit?.({ ...getReturnObject.current({ event }), }) }, - [hidePicker, on_submit] + [hidePicker, onSubmit] ) const onCancelHandler = useCallback( @@ -544,9 +765,9 @@ function DatePicker(externalProps: DatePickerProps) { event: DatePickerChangeEvent> ) => { hidePicker() - on_cancel?.({ ...getReturnObject.current(event) }) + onCancel?.({ ...getReturnObject.current(event) }) }, - [hidePicker, on_cancel] + [hidePicker, onCancel] ) const onResetHandler = useCallback( @@ -554,9 +775,9 @@ function DatePicker(externalProps: DatePickerProps) { event: DatePickerChangeEvent> ) => { hidePicker() - on_reset?.({ ...getReturnObject.current(event) }) + onReset?.({ ...getReturnObject.current(event) }) }, - [hidePicker, on_reset] + [hidePicker, onReset] ) const togglePicker = useCallback( @@ -567,7 +788,8 @@ function DatePicker(externalProps: DatePickerProps) { ) const formatSelectedDateTitle = useCallback(() => { - const { selected_date, start, end } = translation + const { selectedDate, start, end } = translation + const { startDate, endDate } = dates let currentDate = startDate ? format(startDate, 'PPPP') : null @@ -578,52 +800,52 @@ function DatePicker(externalProps: DatePickerProps) { )}` } - return currentDate ? selected_date.replace(/%s/, currentDate) : '' - }, [range, translation, startDate, endDate]) + return currentDate ? selectedDate.replace(/%s/, currentDate) : '' + }, [range, translation, dates]) // use only the props from context, who are available here anyway const extendedProps = extendPropsWithContext( props, defaultProps, { skeleton: context?.skeleton }, - context.getTranslation(props).DatePicker, + convertSnakeCaseProps(context.getTranslation(props).DatePicker), // convertSnakeCaseProps - can be removed in v11 pickFormElementProps(context?.FormRow), // Deprecated – can be removed in v11 - pickFormElementProps(context?.formElement), + convertSnakeCaseProps(pickFormElementProps(context?.formElement)), // Deprecated – can be removed in v11 context.DatePicker ) const { label, title, - label_direction, - label_sr_only, - only_month, - hide_last_week, - disable_autofocus, - hide_navigation_buttons, - first_day, - reset_date, + labelDirection, + labelSrOnly, + onlyMonth, + hideLastWeek, + disableAutofocus, + hideNavigationButtons, + firstDay, + resetDate, link, sync, - input_element, - addon_element, + inputElement, + addonElement, shortcuts, disabled, stretch, skeleton, size, status, - status_state, - status_props, - status_no_animation, + statusState, + statusProps, + statusNoAnimation, globalStatus, suffix, - mask_order, - mask_placeholder, - submit_button_text, - cancel_button_text, - reset_button_text, - show_reset_button, + maskOrder, + maskPlaceholder, + submitButtonText, + cancelButtonText, + resetButtonText, + showResetButton, className, tooltip, ...restProps @@ -637,30 +859,52 @@ function DatePicker(externalProps: DatePickerProps) { id, month, date, - start_date, - end_date, - min_date, - max_date, - enable_keyboard_nav, - hide_navigation, - return_format, - date_format, - hide_days, - correct_invalid_date, + startDate, + endDate, + minDate, + maxDate, + enableKeyboardNav, + hideNavigation, + returnFormat, + dateFormat, + hideDays, + correctInvalidDate, opened, direction, range, + showInput, + noAnimation, + onDaysRender, + onShow, + onType, + onHide, + showSubmitButton, + showCancelButton, + // These translations needs to be filtered out here, since the validateDOMAttributes + // is unable to filter out the translation props after they have been camelCased + selectedDate, + selectedMonth, + selectedYear, + nextMonth, + nextYear, + openPickerText, + placeholderCharacters, + prevMonth, + prevYear, + endMonth, + startMonth, + ...rest - } = restProps + } = restProps as DatePickerAllProps & DatePickerTranslations attributes = rest } - const shouldHideDays = only_month ? true : hide_days - const shouldHideNavigation = only_month - ? hide_navigation_buttons + const shouldHideDays = onlyMonth ? true : hideDays + const shouldHideNavigation = onlyMonth + ? hideNavigationButtons ? false : true - : hide_navigation + : hideNavigation const showStatus = getStatusState(status) @@ -686,17 +930,14 @@ function DatePicker(externalProps: DatePickerProps) { const mainParams = { className: classnames( 'dnb-date-picker', - status && `dnb-date-picker__status--${status_state}`, - label_direction && `dnb-date-picker--${label_direction}`, + status && `dnb-date-picker__status--${statusState}`, + labelDirection && `dnb-date-picker--${labelDirection}`, opened && 'dnb-date-picker--opened', hidden && 'dnb-date-picker--hidden', showInput && 'dnb-date-picker--show-input', - (range || - show_submit_button || - show_cancel_button || - show_reset_button) && + (range || showSubmitButton || showCancelButton || showResetButton) && 'dnb-date-picker--show-footer', - align_picker && `dnb-date-picker--${align_picker}`, + alignPicker && `dnb-date-picker--${alignPicker}`, stretch && `dnb-date-picker--stretch`, 'dnb-form-component', size && `dnb-date-picker--${size}`, @@ -726,8 +967,8 @@ function DatePicker(externalProps: DatePickerProps) { id={id + '-label'} forId={id} text={label} - label_direction={label_direction} - srOnly={label_sr_only} + labelDirection={labelDirection} + srOnly={labelSrOnly} disabled={disabled} skeleton={skeleton} /> @@ -748,10 +989,10 @@ function DatePicker(externalProps: DatePickerProps) { text_id={id + '-status'} // used for "aria-describedby" width_selector={id + '-shell'} text={status} - state={status_state} - no_animation={status_no_animation} + state={statusState} + no_animation={statusNoAnimation} skeleton={skeleton} - {...status_props} + {...statusProps} /> @@ -762,22 +1003,22 @@ function DatePicker(externalProps: DatePickerProps) { disabled={disabled} stretch={stretch} skeleton={skeleton} - maskOrder={mask_order} - maskPlaceholder={mask_placeholder} + maskOrder={maskOrder} + maskPlaceholder={maskPlaceholder} isRange={range} showInput={showInput} selectedDateTitle={selectedDateTitle} - input_element={input_element} + inputElement={inputElement} opened={opened} hidden={hidden} size={size} status={status ? 'error' : null} - status_state={status_state} + statusState={statusState} lang={context.locale} {...attributes} submitAttributes={remainingSubmitProps} onSubmit={togglePicker} - {...status_props} + {...statusProps} /> - {(addon_element || shortcuts) && ( + {(addonElement || shortcuts) && ( )} @@ -817,9 +1058,9 @@ function DatePicker(externalProps: DatePickerProps) { onSubmit={onSubmitHandler} onCancel={onCancelHandler} onReset={onResetHandler} - submitButtonText={submit_button_text} - cancelButtonText={cancel_button_text} - resetButtonText={reset_button_text} + submitButtonText={submitButtonText} + cancelButtonText={cancelButtonText} + resetButtonText={resetButtonText} /> )} diff --git a/packages/dnb-eufemia/src/components/date-picker/DatePickerAddon.tsx b/packages/dnb-eufemia/src/components/date-picker/DatePickerAddon.tsx index 3d6b7c79dbb..96b2e158dac 100644 --- a/packages/dnb-eufemia/src/components/date-picker/DatePickerAddon.tsx +++ b/packages/dnb-eufemia/src/components/date-picker/DatePickerAddon.tsx @@ -8,6 +8,7 @@ import { convertStringToDate } from './DatePickerCalc' import Button from '../button/Button' import DatePickerContext from './DatePickerContext' +// TODO: convert shortcut properties to camelCase, constitutes a breaking change - could be included in v11 export type DatePickerShortcut = { title?: string date?: string | Date | ((...args: unknown[]) => Date) diff --git a/packages/dnb-eufemia/src/components/date-picker/DatePickerCalc.ts b/packages/dnb-eufemia/src/components/date-picker/DatePickerCalc.ts index 0b8481e831a..a9ad4a1426b 100644 --- a/packages/dnb-eufemia/src/components/date-picker/DatePickerCalc.ts +++ b/packages/dnb-eufemia/src/components/date-picker/DatePickerCalc.ts @@ -239,7 +239,7 @@ export function correctV1Format(date: string) { // TODO: Remove this in next major version if (/YYYY/.test(date) && /DD/.test(date)) { warn( - 'You are using "YYYY-MM-DD" as the date_format or return_format? Please use "yyyy-MM-dd" instead!' + 'You are using "YYYY-MM-DD" as the dateFormat or returnFormat? Please use "yyyy-MM-dd" instead!' ) date = date.replace(/DD/, 'dd').replace(/YYYY/, 'yyyy') } @@ -249,7 +249,7 @@ export function correctV1Format(date: string) { export function convertStringToDate( date: string | Date, - { date_format = null }: { date_format?: string | null } = {} + { dateFormat = null }: { dateFormat?: string | null } = {} ): Date { if (!date) { return null @@ -259,9 +259,9 @@ export function convertStringToDate( dateObject = typeof date === 'string' ? parseISO(date) : toDate(date) // check one more time if we can generate a valid date - if (typeof date === 'string' && date_format && !isValid(dateObject)) { - date_format = correctV1Format(date_format) - dateObject = parse(date, date_format, new Date()) + if (typeof date === 'string' && dateFormat && !isValid(dateObject)) { + dateFormat = correctV1Format(dateFormat) + dateObject = parse(date, dateFormat, new Date()) } // rather return null than an invalid date diff --git a/packages/dnb-eufemia/src/components/date-picker/DatePickerCalendar.tsx b/packages/dnb-eufemia/src/components/date-picker/DatePickerCalendar.tsx index f6227d483f5..2d262c77c00 100644 --- a/packages/dnb-eufemia/src/components/date-picker/DatePickerCalendar.tsx +++ b/packages/dnb-eufemia/src/components/date-picker/DatePickerCalendar.tsx @@ -86,7 +86,7 @@ export type DatePickerCalendarProps = Omit< id?: string nr?: number /** - * To display what month should be shown in the first calendar by default. Defaults to the `date` respective `start_date`. + * To display what month should be shown in the first calendar by default. Defaults to the `date` respective `startDate`. */ month?: Date prevBtn?: boolean @@ -169,9 +169,9 @@ function DatePickerCalendar(restOfProps: DatePickerCalendarProps) { startMonth, endMonth, translation: { - DatePicker: { selected_month }, + DatePicker: { selectedMonth }, }, - props: { on_days_render }, + props: { onDaysRender }, } = useContext(DatePickerContext) const { @@ -251,8 +251,8 @@ function DatePickerCalendar(restOfProps: DatePickerCalendarProps) { }) ) - if (on_days_render) { - const changedDays = on_days_render(daysFromCalendar, nr) + if (onDaysRender) { + const changedDays = onDaysRender(daysFromCalendar, nr) if (Array.isArray(changedDays)) { daysFromCalendar = changedDays } @@ -271,7 +271,7 @@ function DatePickerCalendar(restOfProps: DatePickerCalendarProps) { maxDate, minDate, nr, - on_days_render, + onDaysRender, onlyMonth, startDate, ] @@ -293,7 +293,7 @@ function DatePickerCalendar(restOfProps: DatePickerCalendarProps) { const findValid = useCallback( (date: Date, keyCode: string) => { - if (!on_days_render) { + if (!onDaysRender) { return date } @@ -330,7 +330,7 @@ function DatePickerCalendar(restOfProps: DatePickerCalendarProps) { return date }, - [on_days_render, getDays, keyNavCalc] + [onDaysRender, getDays, keyNavCalc] ) const hasReachedEnd = useCallback( @@ -539,7 +539,7 @@ function DatePickerCalendar(restOfProps: DatePickerCalendarProps) {