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

Fixed #35 add ability to show latest added notification on top. #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ props details:
<td></td>
<td>function returning html node which will act as notification container</td>
</tr>
<tr>
<td>isLastTop</td>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a better name.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isReverse ?

Or use isAddOnTop, mean is add a new notification to top or bottom and it not a global change yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newestOnTop

<td>Boolean</td>
<td></td>
<td>whether to show recent notification on top, this is global change.</td>
</tr>
</tbody>
</table>

Expand Down
7 changes: 7 additions & 0 deletions examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ function manualClose() {
});
}

function toggleOrder() {
notification.toggleOrder();
}

ReactDOM.render(<div>
<div>
<label><input type="checkbox" onChange={toggleOrder} />Show last added on top</label>
<br/>
<br/>
<button onClick={simpleFn}>simple show</button>
<button onClick={durationFn}>duration=0</button>
<button onClick={closableFn}>closable</button>
Expand Down
11 changes: 7 additions & 4 deletions src/Notification.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Notice from './Notice';

let seed = 0;
const now = Date.now();
let _isLastTop = false;

function getUuid() {
return `rcNotification_${now}_${seed++}`;
Expand Down Expand Up @@ -48,9 +49,7 @@ class Notification extends Component {
this.setState(previousState => {
const notices = previousState.notices;
if (!notices.filter(v => v.key === key).length) {
return {
notices: notices.concat(notice),
};
return { notices: _isLastTop ? [notice, ...notices] : notices.concat(notice) };
}
});
}
Expand Down Expand Up @@ -88,7 +87,8 @@ class Notification extends Component {
}

Notification.newInstance = function newNotificationInstance(properties, callback) {
const { getContainer, ...props } = properties || {};
const { getContainer, isLastTop, ...props } = properties || {};
_isLastTop = !!isLastTop;
const div = document.createElement('div');
if (getContainer) {
const root = getContainer();
Expand All @@ -103,6 +103,9 @@ Notification.newInstance = function newNotificationInstance(properties, callback
}
called = true;
callback({
toggleOrder() {
_isLastTop = !_isLastTop;
},
notice(noticeProps) {
notification.add(noticeProps);
},
Expand Down
35 changes: 35 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,39 @@ describe('rc-notification', () => {
expect(() => ReactDOM.render(<Test />, container))
.to.not.throwException();
});

it('last notification on top works', (done) => {
Notification.newInstance({ isLastTop: true }, notification => {
notification.notice({
content: <p className="test">1</p>,
duration: 0,
});
notification.notice({
content: <p className="test">2</p>,
duration: 0,
});
setTimeout(() => {
expect(
TestUtils.scryRenderedDOMComponentsWithClass(notification.component, 'test')
.map(e => e.textContent)
).to.eql(
['2', '1']
);
notification.toggleOrder();
notification.notice({
content: <p className="test">3</p>,
duration: 0,
});
setTimeout(() => {
expect(
TestUtils.scryRenderedDOMComponentsWithClass(notification.component, 'test')
.map(e => e.textContent)
).to.eql(
['2', '1', '3']
);
done();
});
}, 10);
});
});
});