Skip to content

Commit

Permalink
Refactor(web-react): Move away from the defaultProps
Browse files Browse the repository at this point in the history
  * Support for defaultProps will be removed from function components in a future major release.
  * Use JavaScript default parameters instead.
  • Loading branch information
literat committed Feb 6, 2024
1 parent 65d6577 commit 35a0b1f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 22 deletions.
5 changes: 2 additions & 3 deletions packages/web-react/src/components/Field/HelperText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const defaultProps = {
};

const HelperText = (props: Props) => {
const { helperText, className, elementType: ElementTag = 'div', id, registerAria } = props;
const propsWithDefaults = { ...defaultProps, ...props };
const { helperText, className, elementType: ElementTag = 'div', id, registerAria } = propsWithDefaults;

useEffect(() => {
registerAria?.({ add: id });
Expand All @@ -38,6 +39,4 @@ const HelperText = (props: Props) => {
return null;
};

HelperText.defaultProps = defaultProps;

export default HelperText;
5 changes: 2 additions & 3 deletions packages/web-react/src/components/Heading/Heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const defaultProps = {
};

export const Heading = <T extends ElementType = 'div', S = void>(props: SpiritHeadingProps<T, S>): JSX.Element => {
const { elementType: ElementTag = 'div', children, ...restProps } = props;
const propsWithDefaults = { ...defaultProps, ...props };
const { elementType: ElementTag = 'div', children, ...restProps } = propsWithDefaults;
const { classProps, props: modifiedProps } = useHeadingStyleProps(restProps);
const { styleProps, props: otherProps } = useStyleProps(modifiedProps);

Expand All @@ -20,6 +21,4 @@ export const Heading = <T extends ElementType = 'div', S = void>(props: SpiritHe
);
};

Heading.defaultProps = defaultProps;

export default Heading;
7 changes: 3 additions & 4 deletions packages/web-react/src/components/Icon/Icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { htmlParser } from '../../utils/htmlParser';
import { NoSsr } from '../NoSsr';

const defaultProps = {
ariaHidden: true,
boxSize: 24,
ariaHidden: true,
};

export const Icon = (props: IconProps) => {
const { boxSize, name, title, ariaHidden, ...restProps } = props;
const propsWithDefaults = { ...defaultProps, ...props };
const { boxSize, name, title, ariaHidden, ...restProps } = propsWithDefaults;
let icon = useIcon(name);
const { styleProps, props: otherProps } = useStyleProps(restProps);
const isHtmlParserLoaded = typeof htmlParser === 'function';
Expand Down Expand Up @@ -63,6 +64,4 @@ export const Icon = (props: IconProps) => {
);
};

Icon.defaultProps = defaultProps;

export default Icon;
9 changes: 4 additions & 5 deletions packages/web-react/src/components/Stack/Stack.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { ElementType } from 'react';
import classNames from 'classnames';
import { useStackStyleProps } from './useStackStyleProps';
import React, { ElementType } from 'react';
import { useStyleProps } from '../../hooks';
import { SpiritStackProps } from '../../types';
import { useStackStyleProps } from './useStackStyleProps';

const defaultProps: SpiritStackProps = {
elementType: 'div',
Expand All @@ -13,7 +13,8 @@ const defaultProps: SpiritStackProps = {
};

export const Stack = <T extends ElementType = 'div'>(props: SpiritStackProps<T>): JSX.Element => {
const { elementType: ElementTag = 'div', children, ...restProps } = props;
const propsWithDefaults = { ...defaultProps, ...props };
const { elementType: ElementTag = 'div', children, ...restProps } = propsWithDefaults;
const { classProps, props: modifiedProps, styleProps: stackStyle } = useStackStyleProps(restProps);
const { styleProps, props: otherProps } = useStyleProps(modifiedProps);

Expand All @@ -31,6 +32,4 @@ export const Stack = <T extends ElementType = 'div'>(props: SpiritStackProps<T>)
);
};

Stack.defaultProps = defaultProps;

export default Stack;
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import React, { ElementType } from 'react';
import classNames from 'classnames';
import React, { ElementType } from 'react';
import { useStyleProps } from '../../hooks';
import { SpiritVisuallyHiddenProps } from '../../types/visuallyHidden';
import { useVisuallyHiddenProps } from './useVisuallyHiddenProps';

const defaultProps = {
elementType: 'span',
};

const VisuallyHidden = <T extends ElementType = 'span'>(props: SpiritVisuallyHiddenProps<T>): JSX.Element => {
const { children, elementType: ElementTag = 'span', ...rest } = props;
const { classProps, props: modifiedProps } = useVisuallyHiddenProps(rest);
Expand All @@ -20,6 +16,4 @@ const VisuallyHidden = <T extends ElementType = 'span'>(props: SpiritVisuallyHid
);
};

VisuallyHidden.defaultProps = defaultProps;

export default VisuallyHidden;

0 comments on commit 35a0b1f

Please sign in to comment.