diff --git a/packages/web-react/src/components/Field/HelperText.tsx b/packages/web-react/src/components/Field/HelperText.tsx index 85a8d9398a..d99b69e7e0 100644 --- a/packages/web-react/src/components/Field/HelperText.tsx +++ b/packages/web-react/src/components/Field/HelperText.tsx @@ -1,24 +1,22 @@ import React, { ElementType, useEffect } from 'react'; -import { RegisterType } from './useAriaIds'; +import { HelperTextProps } from './types'; -interface Props { - helperText: React.ReactNode; - className?: string; - elementType?: ElementType; - id?: string; - registerAria?: RegisterType; -} - -const defaultProps = { +const defaultProps: Partial = { className: undefined, elementType: 'div', id: undefined, registerAria: undefined, }; -const HelperText = (props: Props) => { +const HelperText = (props: HelperTextProps) => { const propsWithDefaults = { ...defaultProps, ...props }; - const { helperText, className, elementType: ElementTag = 'div', id, registerAria } = propsWithDefaults; + const { + helperText, + className, + elementType: ElementTag = defaultProps.elementType as ElementType, + id, + registerAria, + } = propsWithDefaults; useEffect(() => { registerAria?.({ add: id }); diff --git a/packages/web-react/src/components/Field/ValidationText.tsx b/packages/web-react/src/components/Field/ValidationText.tsx index e8ee18b329..4009885f9b 100644 --- a/packages/web-react/src/components/Field/ValidationText.tsx +++ b/packages/web-react/src/components/Field/ValidationText.tsx @@ -1,13 +1,5 @@ import React, { ElementType, useEffect } from 'react'; -import { ValidationTextProp } from '../../types'; -import { RegisterType } from './useAriaIds'; - -export interface ValidationTextProps extends ValidationTextProp { - className?: string; - elementType?: ElementType; - id?: string; - registerAria?: RegisterType; -} +import { ValidationTextProps } from './types'; const defaultProps = { className: undefined, @@ -18,7 +10,13 @@ const defaultProps = { export const ValidationText = (props: ValidationTextProps) => { const propsWithDefaults = { ...defaultProps, ...props }; - const { className, validationText, elementType: ElementTag = 'div', id, registerAria } = propsWithDefaults; + const { + className, + validationText, + elementType: ElementTag = defaultProps.elementType as ElementType, + id, + registerAria, + } = propsWithDefaults; useEffect(() => { registerAria?.({ add: id }); diff --git a/packages/web-react/src/components/Field/types.ts b/packages/web-react/src/components/Field/types.ts new file mode 100644 index 0000000000..45930f6548 --- /dev/null +++ b/packages/web-react/src/components/Field/types.ts @@ -0,0 +1,24 @@ +import { ElementType, JSXElementConstructor, ReactNode } from 'react'; +import { ValidationTextProp } from '../../types'; +import { RegisterType } from './useAriaIds'; + +export interface FieldElementTypeProps { + /** + * The HTML element or React element used to render the pill, e.g. 'div', 'span'. + * + * @default 'div' + */ + elementType?: T | JSXElementConstructor; +} + +export interface FieldProps extends FieldElementTypeProps { + className?: string; + id?: string; + registerAria?: RegisterType; +} + +export interface HelperTextProps extends FieldProps { + helperText: ReactNode; +} + +export interface ValidationTextProps extends FieldProps, ValidationTextProp {}