diff --git a/src/app-components/Label/Label.tsx b/src/app-components/Label/Label.tsx index c80c75f8d..2480fb1d8 100644 --- a/src/app-components/Label/Label.tsx +++ b/src/app-components/Label/Label.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import type { PropsWithChildren, ReactElement } from 'react'; +import type { JSX, PropsWithChildren, ReactElement } from 'react'; import { Label as DesignsystemetLabel } from '@digdir/designsystemet-react'; import { Grid, type GridProps } from '@material-ui/core'; @@ -10,10 +10,6 @@ import classes from 'src/app-components/Label/Label.module.css'; type GridSize = Pick; -type RequiredIndicatorProps = - | { required: true; requiredIndicator: ReactElement } - | { required?: false; requiredIndicator?: ReactElement }; - type LabelProps = { label: string | undefined; optionalIndicator?: ReactElement; @@ -21,8 +17,9 @@ type LabelProps = { description?: ReactElement; className?: string; grid?: GridSize; -} & RequiredIndicatorProps & - Pick; + required?: boolean; + requiredIndicator?: JSX.Element; +} & Pick; export function Label({ label, diff --git a/src/components/form/OptionalIndicator.tsx b/src/components/form/OptionalIndicator.tsx index 026e3174f..a39612224 100644 --- a/src/components/form/OptionalIndicator.tsx +++ b/src/components/form/OptionalIndicator.tsx @@ -2,15 +2,11 @@ import React from 'react'; import { useLanguage } from 'src/features/language/useLanguage'; -type OptionalIndicatorProps = { +export type OptionalIndicatorProps = { readOnly?: boolean; -} & ( - | { - required: true; - showOptionalMarking?: boolean; - } - | { required?: false; showOptionalMarking: boolean } -); + required?: boolean; + showOptionalMarking?: boolean; +}; export const OptionalIndicator = ({ readOnly, required, showOptionalMarking }: OptionalIndicatorProps) => { const { langAsString } = useLanguage(); diff --git a/src/layout/Datepicker/DatepickerComponent.tsx b/src/layout/Datepicker/DatepickerComponent.tsx index d7c598939..92a74f331 100644 --- a/src/layout/Datepicker/DatepickerComponent.tsx +++ b/src/layout/Datepicker/DatepickerComponent.tsx @@ -1,17 +1,12 @@ import React, { useState } from 'react'; -import { HelpText } from '@digdir/designsystemet-react'; import { Grid } from '@material-ui/core'; import { CalendarIcon } from '@navikt/aksel-icons'; import { formatDate, isValid as isValidDate } from 'date-fns'; import { Button } from 'src/app-components/button/Button'; import { Label } from 'src/app-components/Label/Label'; -import { Description } from 'src/components/form/Description'; -import { OptionalIndicator } from 'src/components/form/OptionalIndicator'; -import { RequiredIndicator } from 'src/components/form/RequiredIndicator'; import { useDataModelBindings } from 'src/features/formData/useDataModelBindings'; -import { Lang } from 'src/features/language/Lang'; import { useCurrentLanguage } from 'src/features/language/LanguageProvider'; import { useLanguage } from 'src/features/language/useLanguage'; import { useIsMobile } from 'src/hooks/useDeviceWidths'; @@ -22,6 +17,7 @@ import { DatePickerDialog } from 'src/layout/Datepicker/DatepickerDialog'; import { DatePickerInput } from 'src/layout/Datepicker/DatePickerInput'; import { getDateConstraint, getDateFormat, getSaveFormattedDateString, strictParseISO } from 'src/utils/dateHelpers'; import { getDatepickerFormat } from 'src/utils/formatDateLocale'; +import { useLabel } from 'src/utils/layout/useLabel'; import { useNodeItem } from 'src/utils/layout/useNodeItem'; import type { PropsFromGenericComponent } from 'src/layout'; @@ -68,41 +64,26 @@ export function DatepickerComponent({ node, overrideDisplay }: IDatepickerProps) setValue('simpleBinding', isoDateString); }; - const label = - (overrideDisplay?.renderLabel ?? true) && overrideDisplay?.renderedInTable !== true - ? langAsString(textResourceBindings?.title) - : undefined; + const { labelText, getRequiredComponent, getOptionalComponent, getHelpTextComponent, getDescriptionComponent } = + useLabel({ + overrideDisplay, + textResourceBindings, + readOnly, + required, + showOptionalMarking: !!labelSettings?.optionalIndicator, + }); return ( <>