Skip to content

Commit

Permalink
fixup! Refactor(web-react): Move away from the defaultProps
Browse files Browse the repository at this point in the history
  • Loading branch information
literat committed Apr 11, 2024
1 parent 56c3c4e commit 925bd36
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
22 changes: 10 additions & 12 deletions packages/web-react/src/components/Field/HelperText.tsx
Original file line number Diff line number Diff line change
@@ -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<HelperTextProps> = {
className: undefined,
elementType: 'div',
id: undefined,
registerAria: undefined,
};

const HelperText = (props: Props) => {
const HelperText = <T extends ElementType = 'div'>(props: HelperTextProps<T>) => {
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 });
Expand Down
18 changes: 8 additions & 10 deletions packages/web-react/src/components/Field/ValidationText.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 });
Expand Down
24 changes: 24 additions & 0 deletions packages/web-react/src/components/Field/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ElementType, JSXElementConstructor, ReactNode } from 'react';
import { ValidationTextProp } from '../../types';
import { RegisterType } from './useAriaIds';

export interface FieldElementTypeProps<T extends ElementType = 'div'> {
/**
* The HTML element or React element used to render the pill, e.g. 'div', 'span'.
*
* @default 'div'
*/
elementType?: T | JSXElementConstructor<unknown>;
}

export interface FieldProps<T extends ElementType = 'div'> extends FieldElementTypeProps<T> {
className?: string;
id?: string;
registerAria?: RegisterType;
}

export interface HelperTextProps<T extends ElementType = 'div'> extends FieldProps<T> {
helperText: ReactNode;
}

export interface ValidationTextProps<T extends ElementType = 'div'> extends FieldProps<T>, ValidationTextProp {}

0 comments on commit 925bd36

Please sign in to comment.