Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove use of defaultProps (issue #1644) #1654

Merged
merged 10 commits into from
Feb 17, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,32 @@ const componentName = 'DISPLAY_NAME';

// NOTE: the component SCSS is not imported here: it is rolled up separately.

// Default values can be included here and then assigned to the prop params,
// e.g. prop = defaults.prop,
// This gathers default values together neatly and ensures non-primitive
// values are initialized early to avoid react making unnecessary re-renders.
// Note that default values are not required for props that are 'required',
// nor for props where the component can apply undefined values reasonably.
// Default values should be provided when the component needs to make a choice
// or assumption when a prop is not supplied.

// Default values for props
// const defaults = {
// /* TODO: add defaults for relevant props if needed */
// };

/**
* TODO: A description of the component.
*/
export let DISPLAY_NAME = React.forwardRef(
(
{
// The component props, in alphabetical order (for consistency).

children /* TODO: remove if not needed. */,
className,
/* TODO: add other props for DISPLAY_NAME */
/* TODO: add other props for DISPLAY_NAME, with default values if needed */

// Collect any other property values passed in.
...rest
},
Expand Down Expand Up @@ -87,11 +103,3 @@ DISPLAY_NAME.propTypes = {

/* TODO: add types and DocGen for all props. */
};

// Default values for component props. Default values are not required for
// props that are required, nor for props where the component can apply
// 'undefined' values reasonably. Default values should be provided when the
// component needs to make a choice or assumption when a prop is not supplied.
DISPLAY_NAME.defaultProps = {
/* TODO: add defaults for relevant props. */
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,26 @@ import uuidv4 from '../../global/js/utils/uuidv4';

const componentName = 'APIKeyModal';

// Default values for props
const defaults = {
dcwarwick marked this conversation as resolved.
Show resolved Hide resolved
apiKeyName: '',
customSteps: Object.freeze([]),
};

export let APIKeyModal = forwardRef(
(
{
// The component props, in alphabetical order (for consistency).
apiKey,
apiKeyLabel,
apiKeyName,
apiKeyName = defaults.apiKeyName,
body,
className,
closeButtonText,
copyButtonText,
copyErrorText,
copyIconDescription,
customSteps,
customSteps = defaults.customSteps,
downloadBodyText,
downloadFileName,
downloadFileType,
Expand Down Expand Up @@ -78,6 +85,8 @@ export let APIKeyModal = forwardRef(
open,
previousStepButtonText,
showAPIKeyLabel,

// Collect any other property values passed in.
...rest
},
ref
Expand Down Expand Up @@ -504,15 +513,4 @@ APIKeyModal.propTypes = {
showAPIKeyLabel: PropTypes.string,
};

APIKeyModal.defaultProps = {
apiKeyName: '',
customSteps: [],
error: false,
hasAPIKeyVisibilityToggle: false,
hasDownloadLink: false,
loading: false,
nameRequired: false,
open: false,
};

APIKeyModal.displayName = componentName;
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ const componentName = 'ActionBar';
export let ActionBar = React.forwardRef(
(
{
// The component props, in alphabetical order (for consistency).

actions,
className,
maxVisible,
menuOptionsClass,
onWidthChange,
overflowAriaLabel,
rightAlign,

// Collect any other property values passed in.
...rest
},
Expand Down Expand Up @@ -282,7 +285,3 @@ ActionBar.propTypes = {
*/
rightAlign: PropTypes.bool,
};

ActionBar.defaultProps = {
rightAlign: false,
};
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ const reservedProps = [
];
// Base props on Carbon Button
const propTypes = { ...Button.propTypes };
const defaultProps = { ...Button.defaultProps };

// Remove reserved props
reservedProps.forEach((prop) => {
delete propTypes[prop];
delete defaultProps[prop];
});

ActionBarItem.displayName = componentName;
Expand Down Expand Up @@ -104,5 +102,3 @@ ActionBarItem.propTypes = {
*/
renderIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
};

ActionBarItem.defaultProps = { ...defaultProps };
19 changes: 10 additions & 9 deletions packages/cloud-cognitive/src/components/ActionSet/ActionSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ const ActionSetButton = React.forwardRef(
kind,
label,
loading,
onClick,
size,
// Collect any other property values passed in.
...rest
},
Expand All @@ -52,7 +50,7 @@ const ActionSetButton = React.forwardRef(
},
])}
disabled={disabled || loading || false}
{...{ kind, onClick, ref, size }}
{...{ kind, ref }}
>
{label}
{loading && <InlineLoading />}
Expand Down Expand Up @@ -80,6 +78,11 @@ const defaultKind = 'primary';
const willStack = (size, numberOfActions) =>
size === 'xs' || size === 'sm' || (size === 'md' && numberOfActions > 2);

// Default values for props
const defaults = {
size: 'md',
};

/**
* An ActionSet presents a set of action buttons, constructed from bundles
* of prop values and applying some layout rules. When the size is 'xs' or 'sm'
Expand All @@ -96,10 +99,12 @@ export const ActionSet = React.forwardRef(
(
{
// The component props, in alphabetical order (for consistency).

actions,
buttonSize,
className,
size,
size = defaults.size,

// Collect any other property values passed in.
...rest
},
Expand Down Expand Up @@ -186,7 +191,7 @@ ActionSet.validateActions =
const problems = [];

if (actions > 0) {
const size = sizeFn ? sizeFn(props) : props.size;
const size = sizeFn ? sizeFn(props) : props.size || defaults.size;
const stacking = willStack(size, actions);

const countActions = (kind) =>
Expand Down Expand Up @@ -294,7 +299,3 @@ ActionSet.propTypes = {
*/
size: PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xlg', 'max']),
};

ActionSet.defaultProps = {
size: 'md',
};
15 changes: 10 additions & 5 deletions packages/cloud-cognitive/src/components/AddSelect/AddSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ import { AddSelectBreadcrumbs } from './AddSelectBreadcrumbs';
import { AddSelectList } from './AddSelectList';
const componentName = 'AddSelect';

// Default values for props
const defaults = {
items: Object.freeze([]),
};

export let AddSelect = forwardRef(
(
{
// The component props, in alphabetical order (for consistency).

className,
description,
influencerTitle,
inputPlaceholder,
items,
items = defaults.items,
itemsLabel,
multi,
noResultsDescription,
Expand All @@ -40,6 +47,8 @@ export let AddSelect = forwardRef(
removeIconDescription,
textInputLabel,
title,

// Collect any other property values passed in.
...rest
},
ref
Expand Down Expand Up @@ -221,8 +230,4 @@ AddSelect.propTypes = {
title: PropTypes.string,
};

AddSelect.defaultProps = {
items: [],
};

AddSelect.displayName = componentName;
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,4 @@ BreadcrumbWithOverflow.propTypes = {
),
};

BreadcrumbWithOverflow.defaultProps = {
noTrailingSlash: false,
};
BreadcrumbWithOverflow.displayName = componentName;
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const componentName = 'ButtonMenu';

// NOTE: the component SCSS is not imported here: it is rolled up separately.

// Default values for props
const defaults = {
size: 'default',
};

/**
* Combining a standard button with an overflow menu, this component appears
* as a button and has all the usual carbon Button props and rendering, but
Expand All @@ -33,13 +38,15 @@ export let ButtonMenu = React.forwardRef(
(
{
// The component props, in alphabetical order (for consistency).

children,
className,
iconDescription,
label,
menuOptionsClass,
renderIcon: Icon,
size,
size = defaults.size,

// Collect any other property values passed in.
...rest
},
Expand Down Expand Up @@ -131,7 +138,3 @@ ButtonMenu.propTypes = {
*/
size: Button.propTypes.size,
};

ButtonMenu.defaultProps = {
size: 'default',
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,3 @@ export let ButtonMenuItem = React.forwardRef((props, ref) => (
ButtonMenuItem = pkg.checkComponentEnabled(ButtonMenuItem, componentName);

ButtonMenuItem.propTypes = OverflowMenuItem.propTypes;

ButtonMenuItem.defaultProps = OverflowMenuItem.defaultProps;
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ const componentName = 'CancelableTextEdit';

// NOTE: the component SCSS is not imported here: it is rolled up separately.

// Default values for props
const defaults = {
size: 'md',
};

/**
* TODO: A description of the component.
*/
export let CancelableTextEdit = React.forwardRef(
(
{
// The component props, in alphabetical order (for consistency).

className,
hideLabel,
inline,
Expand All @@ -45,7 +52,7 @@ export let CancelableTextEdit = React.forwardRef(
revertDescription,
saveDescription,
saveDisabled,
size,
size = defaults.size,
value,
warn,
warnText,
Expand Down Expand Up @@ -258,12 +265,3 @@ CancelableTextEdit.propTypes = {
*/
warnText: PropTypes.string,
};

// Default values for component props. Default values are not required for
// props that are required, nor for props where the component can apply
// 'undefined' values reasonably. Default values should be provided when the
// component needs to make a choice or assumption when a prop is not supplied.
CancelableTextEdit.defaultProps = {
/* TODO: add defaults for relevant props. */
size: 'md',
};