Skip to content

Commit

Permalink
[PageActions] Add ReactNode as an accepted primaryAction prop value f…
Browse files Browse the repository at this point in the history
…or customizability (#5946)
  • Loading branch information
Rachel Leggett committed May 26, 2022
1 parent 9c1a868 commit 3a5be82
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-doors-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/polaris': minor
---

Added support for setting a `ReactNode` on the `PageActions` `primaryAction` prop
11 changes: 7 additions & 4 deletions polaris-react/src/components/PageActions/PageActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import styles from './PageActions.scss';

export interface PageActionsProps {
/** The primary action for the page */
primaryAction?: DisableableAction & LoadableAction;
primaryAction?: (DisableableAction & LoadableAction) | React.ReactNode;
/** The secondary actions for the page */
secondaryActions?: ComplexAction[] | React.ReactNode;
}
Expand All @@ -26,9 +26,12 @@ export function PageActions({
primaryAction,
secondaryActions,
}: PageActionsProps) {
const primaryActionMarkup = primaryAction
? buttonsFrom(primaryAction, {primary: true})
: null;
let primaryActionMarkup: MaybeJSX = null;
if (isReactElement(primaryAction)) {
primaryActionMarkup = <>{primaryAction}</>;
} else if (primaryAction) {
primaryActionMarkup = buttonsFrom(primaryAction, {primary: true});
}

let secondaryActionsMarkup: MaybeJSX = null;
if (isInterface(secondaryActions) && secondaryActions.length > 0) {
Expand Down
26 changes: 26 additions & 0 deletions polaris-react/src/components/PageActions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,32 @@ Not all page actions require a secondary action.
/>
```

### Page actions with custom primary action

Use to create a custom primary action.

```jsx
<PageActions
primaryAction={
<Button
primary
connectedDisclosure={{
accessibilityLabel: 'Other save actions',
actions: [{content: 'Save as draft'}],
}}
>
Save
</Button>
}
secondaryActions={[
{
content: 'Delete',
destructive: true,
},
]}
/>
```

### Page actions with custom secondary action

Use to create a custom secondary action.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ describe('<PageActions />', () => {
mountWithApp(<PageActions primaryAction={mockAction} />);
expect(buttonsFrom).toHaveBeenCalledWith(mockAction, {primary: true});
});

it('renders a `ReactNode`', () => {
const CustomPrimaryAction = () => null;
const pageActions = mountWithApp(
<PageActions primaryAction={<CustomPrimaryAction />} />,
);

expect(pageActions).toContainReactComponent(CustomPrimaryAction);
});
});

describe('secondaryActions', () => {
Expand Down

0 comments on commit 3a5be82

Please sign in to comment.