import type { ForwardedRef, ReactElement } from 'react'; import React from 'react'; import { Label } from 'react-aria-components'; import classNames from 'classnames'; import { isNativeHtmlStructure } from '../../utils/isNativeHtmlStructure'; export interface FormRowLabelOwnProps { /** * Additional classes to apply to the label. * **⚠️ INTERNAL UI KIT USE ONLY** */ className?: string; children: string | ReactElement; /** * The ID of the form element that the label is associated with. * Applies only when `as` is "label" (default behavior). * @default undefined */ htmlFor?: string; /** * Controls the alignment of the label. */ justify?: boolean; /** * Displays an asterisk (*) to indicate that the field is required. */ required?: boolean; /** * Whether the label is visually hidden. */ visuallyHidden?: boolean | 'keepSpace'; /** * The ID of the label element. */ id?: string; } /** @inheritdoc */ export interface FormRowLabelProps extends Omit { /** * The element to render the form row as. * @default 'label' */ as?: 'label' | 'span'; } export function getFormFieldLabelProps({ className, children, htmlFor, justify, required, ...rest }: T) { if (process.env.NODE_ENV === 'development' && !isNativeHtmlStructure(children)) { throw new TypeError('Only native HTML elements as children are allowed'); } return { ...rest, children, htmlFor, className: classNames(className, 'v2FormField__label', { 'v2FormField__label--justify': justify, 'v2FormField__label--required': required, }), }; } /** @deprecated */ export const FormRowLabel = React.forwardRef(function FormRowLabel( { as = 'label', visuallyHidden = false, ...props }: FormRowLabelProps, forwardedRef: ForwardedRef ) { const Container = as === 'label' ? Label : 'span'; const containerProps = getFormFieldLabelProps(props); if (visuallyHidden === true) { return null; } return ( } {...containerProps}> {visuallyHidden === 'keepSpace' ? null : props.children} ); });