Skip to content
This repository has been archived by the owner on Dec 24, 2019. It is now read-only.

Commit

Permalink
Merge pull request #119 from BamaBoy/barFactoriesUpdate
Browse files Browse the repository at this point in the history
Bar styles factories enhancement
  • Loading branch information
Patrick Burtchaell authored May 10, 2017
2 parents c98663c + 2988b5f commit 7653d93
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
8 changes: 4 additions & 4 deletions docs/guides/styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ I would highly suggest using this method since the styles included in the compon
These two function have the following signature:

```js
(index: Number, style: Object|Void) => Object
(index: Number, style: Object|Void, notification: Object) => Object
```

Where `index` is the index of the notification in the notifications array and
`style` is the style property of the individual notification.
Where `index` is the index of the notification in the notifications array,
`style` is the style property of the individual notification and `notification` is the notification itself.

This function is used to dynamically set the style of each notification in the
stack. The default function adds the `bottom` style property to correctly
position of the notification in a stack.

```js
function defaultStyleFactory(index, style) {
function defaultStyleFactory(index, style, notification) {
return Object.assign(
{},
style,
Expand Down
8 changes: 6 additions & 2 deletions src/notificationStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ const NotificationStack = props => (
const dismissNow = isLast || !props.dismissInOrder;

// Handle styles
const barStyle = props.barStyleFactory(index, notification.barStyle);
const activeBarStyle = props.activeBarStyleFactory(index, notification.activeBarStyle);
const barStyle = props.barStyleFactory(index, notification.barStyle, notification);
const activeBarStyle = props.activeBarStyleFactory(
index,
notification.activeBarStyle,
notification
);

// Allow onClick from notification stack or individual notifications
const onClick = notification.onClick || props.onClick;
Expand Down
40 changes: 40 additions & 0 deletions test/notificationStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,26 @@ describe('<NotificationStack />', () => {
expect(notification.prop('barStyle').background).to.equal('rgb(2, 2, 2)');
});

it('batStyleFactory should have access to notification', () => {
const styleFactory = (index, style, notification) => Object.assign(
{},
style,
{ bottom: `${index}px`, color: notification.key === 1111111 ? 'green' : 'red' }
);

const stack = mount(
<NotificationStack
notifications={[mockNotification]}
barStyleFactory={styleFactory}
onDismiss={() => {}}
/>
);

const notification = stack.find(Notification);

expect(notification.prop('barStyle').color).to.equal('green');
});

it('activeBarStyleFactory should set correct style on notification', () => {
const styleFactory = (index, style) => Object.assign(
{},
Expand Down Expand Up @@ -219,6 +239,26 @@ describe('<NotificationStack />', () => {
expect(notification.prop('activeBarStyle').left).to.equal('4rem');
});

it('activeBarStyleFactory should have access to notification', () => {
const styleFactory = (index, style, notification) => Object.assign(
{},
style,
{ bottom: `${index}px`, color: notification.key === 1111111 ? 'green' : 'red' }
);

const stack = mount(
<NotificationStack
notifications={[mockNotification]}
activeBarStyleFactory={styleFactory}
onDismiss={() => {}}
/>
);

const notification = stack.find(Notification);

expect(notification.prop('activeBarStyle').color).to.equal('green');
});

/**
* Test: Global handling of onClick:
*
Expand Down

0 comments on commit 7653d93

Please sign in to comment.