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

refactor: Destructure default props #525

Merged
merged 51 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
ed8c79f
chore: Destructure default props
mannycarrera4 Mar 18, 2020
1d00da0
chore: Destructure props for modal
mannycarrera4 Mar 18, 2020
51cf87a
fix: Make props optional in modal content
mannycarrera4 Mar 18, 2020
dd1d73c
fix: Update optional prop for combobox
mannycarrera4 Mar 19, 2020
a760d03
fix: Update default props for drawer and combo
mannycarrera4 Mar 19, 2020
a59071f
fix: Update aria label prop name
mannycarrera4 Mar 19, 2020
0359591
fix: Destructure props for drawer header
mannycarrera4 Mar 19, 2020
ad52580
fix: Destructure search box default props
mannycarrera4 Mar 19, 2020
57782f1
fix: Destructure props for header
mannycarrera4 Mar 19, 2020
ae1c3e4
fix: Destructure default props for menu item
mannycarrera4 Mar 19, 2020
7000d21
fix: Fix merge conflict with menu item
mannycarrera4 Mar 19, 2020
faf7616
fix: Destructure default props for menu item and pagination
mannycarrera4 Mar 19, 2020
7e036cd
fix: Destructure default props for menu avatar banner
mannycarrera4 Mar 19, 2020
214f13a
fix: Destructure props for avatar button card checkbox colorinput
mannycarrera4 Mar 19, 2020
4c4da79
fix: Destructure textbutton popper cookie banner fomfield hint label
mannycarrera4 Mar 19, 2020
430dd5b
fix: Destructure props for radio radigroup select
mannycarrera4 Mar 19, 2020
1015195
fix: Destructure for page header layout
mannycarrera4 Mar 20, 2020
3f610c6
fix: Destructure selec option sidepanel skeleton status
mannycarrera4 Mar 20, 2020
6e723d8
fix: Destructure button switch text area text input toast tooltip
mannycarrera4 Mar 20, 2020
879fdea
fix: Destructure props for different buttons and popper
mannycarrera4 Mar 20, 2020
3b6dbfd
fix: Update readme and component props
mannycarrera4 Mar 20, 2020
ce12722
fix: Update menu test and default prop for card
mannycarrera4 Mar 23, 2020
ac52c87
fix: Update icons props
mannycarrera4 Mar 23, 2020
7f5ae2d
test: Update classname prop for svg
mannycarrera4 Mar 23, 2020
69da57a
test: Revert files with failing tests
mannycarrera4 Mar 23, 2020
1fbe477
fix: Add padding as optional for card
mannycarrera4 Mar 25, 2020
5150eed
fix: Revert tooltip changes
mannycarrera4 Mar 25, 2020
11931c8
fix: Fix merge conflicts
mannycarrera4 Mar 25, 2020
2a81713
fix: Update default props for pagination
mannycarrera4 Mar 26, 2020
d9a9546
fix: Fix default props for system icons
mannycarrera4 Mar 26, 2020
3c29a85
fix: Destrcutrue props for menuitem
mannycarrera4 Mar 26, 2020
1b726d3
fix: Add todo for menuitem
mannycarrera4 Mar 26, 2020
47fc1f3
fix: Update props to bring to top and use new type
mannycarrera4 Mar 27, 2020
b180faf
fix: Reformat props
mannycarrera4 Mar 27, 2020
2be75fa
fix: Move defaults to the top
mannycarrera4 Mar 30, 2020
c5c875b
fix: Update default props to the top
mannycarrera4 Mar 30, 2020
55ee59c
fix: Update toast default props
mannycarrera4 Mar 30, 2020
b255f20
fix: Resolve popup conflicts
mannycarrera4 Mar 30, 2020
b6d65a2
fix: Fix storybook errors
mannycarrera4 Mar 30, 2020
5147ae6
fix: Update capwidth default prop
mannycarrera4 Mar 31, 2020
6e185e2
fix: Update comment
mannycarrera4 Mar 31, 2020
f84b2de
fix: Update popup type
mannycarrera4 Mar 31, 2020
953687b
fix: Pass prop down to label for proper styling
mannycarrera4 Mar 31, 2020
736ce06
fix: Address comments, fix test
mannycarrera4 Apr 1, 2020
d7d41b9
fix: Clean up props for modal and toast and header
mannycarrera4 Apr 2, 2020
d014e45
Merge branch 'prerelease/v4' into mc-destructure-props
mannycarrera4 Apr 2, 2020
1877712
fix: Update after merge conflicts
mannycarrera4 Apr 2, 2020
255f688
fix: Remove defaults for popup
mannycarrera4 Apr 6, 2020
06a6440
fix: Update modal props
mannycarrera4 Apr 6, 2020
700662c
Merge branch 'prerelease/v4' into mc-destructure-props
anicholls Apr 6, 2020
39d9408
Merge branch 'prerelease/v4' into mc-destructure-props
mannycarrera4 Apr 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions API_PATTERN_GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,35 @@ foo();

#### Default Props

- Use `defaultProps` whenever you find yourself checking for the existence of something before
executing branching logic. It significantly reduces conditionals, facilitating easier testing and
less bugs.
- Any prop included in `defaultProps` should be typed as required in the component interface.
However, it can still be documented as optional in the README. You can find more details
[here](https://stackoverflow.com/questions/37282159/default-property-value-in-react-component-using-typescript)
- We prefer to colocate our default props. If you're consuming our components, `defaultProps` does
not work if you're renaming our component on import.
- Note: If you assign a default value to a prop, make sure to make the prop as optional in the
interface.

```jsx
const someInterface {
/**
* If true, sets the Checkbox checked to true
* @default false
*/
checked?: boolean;
/**
* If true, set the Checkbox to the disabled state.
* @default false
*/
disabled?: boolean;
/**
* The value of the Checkbox.
*/
value?: string;
}
.
.
.
.

const {checked = false, disabled = false, value} = this.props;
```

#### Class Function Binding

Expand Down Expand Up @@ -325,7 +348,9 @@ The base pattern for prop descriptions is: `The <property> of the <component>.`
value?: string;
```

Be as specific as possible. For example, suppose there is a `label` prop for `Checkbox` which specifies the text of the label. Rather than describe `label` as `The label of the Checkbox`, the following is preferable:
Be as specific as possible. For example, suppose there is a `label` prop for `Checkbox` which
specifies the text of the label. Rather than describe `label` as `The label of the Checkbox`, the
following is preferable:

```
/**
Expand All @@ -343,7 +368,8 @@ Feel free to provide additional detail in the description:
value: number;
```

Be sure to specify a proper `@default` for enum props. Listing the named values which are accepted by the enum prop is encouraged:
Be sure to specify a proper `@default` for enum props. Listing the named values which are accepted
by the enum prop is encouraged:

```
/**
Expand All @@ -353,7 +379,8 @@ Be sure to specify a proper `@default` for enum props. Listing the named values
openDirection?: SidePanelOpenDirection;
```

Use a modified pattern for function props: `The function called when <something happens>.` For example:
Use a modified pattern for function props: `The function called when <something happens>.` For
example:

```
/**
Expand All @@ -362,7 +389,8 @@ Use a modified pattern for function props: `The function called when <something
onChange?: (e: React.SyntheticEvent) => void;
```

The pattern for booleans is also different: `If true, <do something>.` For standard 2-state booleans, set `@default false` in the description. For example:
The pattern for booleans is also different: `If true, <do something>.` For standard 2-state
booleans, set `@default false` in the description. For example:

```
/**
Expand All @@ -382,7 +410,8 @@ Provide additional detail for 2-state booleans where the `false` outcome cannot
centeredNav?: boolean;
```

For 3-state booleans, you will need to describe all 3 cases: `If true <do something>. If false <do something else>. If undefined <do yet another thing>.`
For 3-state booleans, you will need to describe all 3 cases:
`If true <do something>. If false <do something else>. If undefined <do yet another thing>.`

We also recommend the following pattern for errors:

Expand All @@ -393,7 +422,10 @@ We also recommend the following pattern for errors:
error?: ErrorType;
```

Occasionally, you may encounter props which don't play nicely with the suggested guidelines. Rather than following the patterns to the letter, adjust them to provide a better description if necessary. For example, rather than ambiguously describing `id` as `The id of the Checkbox`, provide a more explicit description:
Occasionally, you may encounter props which don't play nicely with the suggested guidelines. Rather
than following the patterns to the letter, adjust them to provide a better description if necessary.
For example, rather than ambiguously describing `id` as `The id of the Checkbox`, provide a more
explicit description:

```
/**
Expand Down
14 changes: 4 additions & 10 deletions modules/_labs/drawer/react/lib/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,15 @@ const ChildrenContainer = styled('div')<Pick<DrawerProps, 'padding'>>(

export default class Drawer extends React.Component<DrawerProps, {}> {
static OpenDirection = DrawerDirection;
static defaultProps = {
openDirection: DrawerDirection.Right,
padding: spacing.s,
width: 360,
showDropShadow: false,
};

public render() {
const {
children,
padding,
width,
openDirection,
padding = spacing.s,
width = 360,
openDirection = DrawerDirection.Right,
header,
showDropShadow,
showDropShadow = false,
role,
...elemProps
} = this.props;
Expand Down
20 changes: 6 additions & 14 deletions modules/_labs/drawer/react/lib/DrawerHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export interface DrawerHeaderProps extends React.HTMLAttributes<HTMLDivElement>
/**
* The background color of the DrawerHeader.
*/
headerColor: CanvasColor | string;
headerColor?: CanvasColor | string;
/**
* The border color of the DrawerHeader. This should match something close to `headerColor`.
*/
borderColor: CanvasColor | string;
borderColor?: CanvasColor | string;
/**
* If true, render the icon and header in white. Useful for preserving contrast with a dark `headerColor`.
* @default false
Expand Down Expand Up @@ -72,22 +72,14 @@ const CloseButton = styled(IconButton)({
});

export default class DrawerHeader extends React.Component<DrawerHeaderProps, {}> {
static defaultProps = {
closeIconLabel: 'Close',
headerColor: colors.soap100,
borderColor: colors.soap500,
showInverseButton: false,
inverse: false,
};

public render() {
const {
onClose,
title,
closeIconLabel,
headerColor,
borderColor,
inverse,
closeIconLabel = 'Close',
headerColor = colors.soap100,
borderColor = colors.soap500,
inverse = false,
id,
...elemProps
} = this.props;
Expand Down
7 changes: 2 additions & 5 deletions modules/_labs/header/react/lib/GlobalHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface GlobalHeaderProps {
* The custom brand node of the GlobalHeader. This React node replaces the dub logo and title.
* @default DubLogoTitle
*/
brand: React.ReactNode;
brand?: React.ReactNode;
/**
* The custom menu toggle node of the GlobalHeader. This React node replaces the default menu toggle.
*/
Expand All @@ -29,12 +29,9 @@ export interface GlobalHeaderProps {
}

export default class GlobalHeader extends React.Component<GlobalHeaderProps> {
static defaultProps = {
brand: <DubLogoTitle />,
};
public render() {
const {
brand,
brand = <DubLogoTitle />,
menuToggle,
onMenuClick,
isCollapsed,
Expand Down
20 changes: 8 additions & 12 deletions modules/_labs/header/react/lib/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export interface HeaderProps extends React.HTMLAttributes<HTMLDivElement> {
* The theme of the Header. Accepts `White`, `Blue`, or `Transparent`.
* @default HeaderTheme.White
*/
themeColor: HeaderTheme;
themeColor?: HeaderTheme;
/**
* The variant of the Header. Accepts `Dub` (small) or `Full` (large).
* @default HeaderVariant.Dub
*/
variant: HeaderVariant;
variant?: HeaderVariant;
/**
* The text of the Header title. Not used if `brand` is provided.
*/
Expand Down Expand Up @@ -68,7 +68,7 @@ const HeaderShell = styled('div')<Pick<HeaderProps, 'variant' | 'themeColor'>>(
MozOsxFontSmoothing: 'grayscale',
position: 'relative',
},
({variant, themeColor}) => ({
({variant, themeColor = HeaderTheme.White}) => ({
// Only the variant Full has a large header, all the other one (Dub, Global) have a small header height
height: variant === HeaderVariant.Full ? HeaderHeight.Large : HeaderHeight.Small,
background: themes[themeColor].background,
Expand Down Expand Up @@ -98,7 +98,7 @@ const BrandLink = styled('a')({
},
});

const navStyle = ({themeColor}: Pick<HeaderProps, 'themeColor'>) => {
const navStyle = ({themeColor = HeaderTheme.White}: Pick<HeaderProps, 'themeColor'>) => {
const theme = themes[themeColor];

return css({
Expand Down Expand Up @@ -210,7 +210,7 @@ class Brand extends React.Component<
Pick<HeaderProps, 'variant' | 'brand' | 'title' | 'themeColor'>
> {
render() {
const {variant, brand, themeColor, title} = this.props;
const {variant, brand, themeColor = HeaderTheme.White, title} = this.props;

switch (variant) {
case HeaderVariant.Global: {
Expand Down Expand Up @@ -239,7 +239,7 @@ class MenuIconButton extends React.Component<
Pick<HeaderProps, 'themeColor' | 'menuToggle' | 'onMenuClick'>
> {
render() {
const {themeColor, menuToggle, onMenuClick} = this.props;
const {themeColor = HeaderTheme.White, menuToggle, onMenuClick} = this.props;
if (menuToggle) {
const menuToggleElement = menuToggle as React.ReactElement<any>;
const onClick = menuToggleElement.props.onClick
Expand Down Expand Up @@ -269,10 +269,6 @@ class MenuIconButton extends React.Component<
export default class Header extends React.Component<HeaderProps, {}> {
static Theme = HeaderTheme;
static Variant = HeaderVariant;
static defaultProps = {
themeColor: HeaderTheme.White,
variant: HeaderVariant.Dub,
};

/**
* Helper that recursively maps ReactNodes to their theme-based equivalent.
Expand Down Expand Up @@ -350,8 +346,8 @@ export default class Header extends React.Component<HeaderProps, {}> {
render() {
const {
menuToggle,
themeColor,
variant,
themeColor = HeaderTheme.White,
variant = HeaderVariant.Dub,
centeredNav,
title,
brand,
Expand Down
11 changes: 4 additions & 7 deletions modules/_labs/header/react/lib/parts/DubLogoTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type DubTitleProps = {
* The theme of the DubLogoTitle. Accepts `White`, `Blue`, or `Transparent`.
* @default HeaderTheme.White
*/
themeColor: HeaderTheme;
themeColor?: HeaderTheme;
/**
* The text of the DubLogoTitle. Not used if `brand` is provided.
*/
Expand Down Expand Up @@ -63,21 +63,18 @@ const DubLogo = styled('div')<DubTitleProps>({
});

export class DubLogoTitle extends React.Component<DubTitleProps> {
static defaultProps = {
themeColor: HeaderTheme.White,
};

render() {
const {themeColor = HeaderTheme.White, title} = this.props;
return (
<LockupContainer>
<Lockup {...this.props}>
<DubLogo
{...this.props}
dangerouslySetInnerHTML={{
__html: this.props.themeColor === HeaderTheme.White ? dubLogoBlue : dubLogoWhite,
__html: themeColor === HeaderTheme.White ? dubLogoBlue : dubLogoWhite,
}}
/>
{this.props.title && <Title {...this.props}>{this.props.title}</Title>}
{title && <Title {...this.props}>{title}</Title>}
</Lockup>
</LockupContainer>
);
Expand Down
20 changes: 10 additions & 10 deletions modules/_labs/header/react/lib/parts/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ export interface SearchBarProps extends GrowthBehavior, React.FormHTMLAttributes
* The screenreader label text for the button to open the collapsed SearchBar.
* @default Open Search
*/
openButtonLabel: string;
openButtonLabel?: string;
/**
* The screenreader label text for the button to close the open SearchBar.
* @default Cancel
*/
closeButtonLabel: string;
closeButtonLabel?: string;
/**
* If true, render the SearchBar with a button to clear the text input.
* @default true
Expand Down Expand Up @@ -385,14 +385,14 @@ export class SearchBar extends React.Component<SearchBarProps, SearchBarState> {
autocompleteItems,
initialValue,
searchTheme,
placeholder,
rightAlign,
inputLabel,
submitLabel,
showClearButton,
clearButtonAriaLabel,
closeButtonLabel,
openButtonLabel,
clearButtonAriaLabel = 'Reset Search Form',
placeholder = 'Search',
inputLabel = 'Search',
submitLabel = 'Search',
openButtonLabel = 'Open Search',
closeButtonLabel = 'Cancel',
showClearButton = true,
...elemProps
} = this.props;

Expand Down Expand Up @@ -445,7 +445,7 @@ export class SearchBar extends React.Component<SearchBarProps, SearchBarState> {
onFocus={this.handleFocus}
onBlur={this.handleBlur}
showClearButton={!isCollapsed && showClearButton}
clearButtonAriaLabel={clearButtonAriaLabel || 'Reset Search Form'}
clearButtonAriaLabel={clearButtonAriaLabel}
labelId={this.labelId}
>
<SearchInput
Expand Down
9 changes: 2 additions & 7 deletions modules/_labs/header/react/lib/parts/WorkdayLogoTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type WorkdayLogoTitleProps = {
* The theme of the WorkdayLogoTitle. Accepts `White`, `Blue`, or `Transparent`.
* @default HeaderTheme.White
*/
themeColor: HeaderTheme;
themeColor?: HeaderTheme;
/**
* The text of the WorkdayLogoTitle. Not used if `brand` is provided.
* @default ''
Expand Down Expand Up @@ -66,13 +66,8 @@ const WorkdayLogo = styled('span')<WorkdayLogoTitleProps>({
});

export class WorkdayLogoTitle extends React.Component<WorkdayLogoTitleProps> {
static defaultProps = {
themeColor: HeaderTheme.White,
title: '',
};

public render() {
const {themeColor, title, variant, ...elemProps} = this.props;
const {themeColor = HeaderTheme.White, title = '', variant, ...elemProps} = this.props;

return (
<LockupContainer>
Expand Down
2 changes: 2 additions & 0 deletions modules/_labs/header/react/spec/GlobalHeader.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ describe('GlobalHeader', () => {
onMenuClick: jest.fn(),
leftSlot: <SearchBar onSubmit={jest.fn()} />,
isCollapsed: true,
themeColor: HeaderTheme.White,
};
const propsHeader2 = {
menuToggle: 'abcde',
isCollapsed: false,
themeColor: HeaderTheme.White,
};
const defaultProps = {
brand: <DubLogoTitle />,
Expand Down
Loading