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 authored and crishpeen committed Apr 9, 2024
1 parent 796b843 commit 7cf7f6d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 18 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;
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 7cf7f6d

Please sign in to comment.