Skip to content

Commit

Permalink
add ability to update notification props by key
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhen-hryhorevskyi committed Apr 4, 2018
1 parent 8af073b commit b288610
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
26 changes: 26 additions & 0 deletions examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,37 @@ function manualClose() {
});
}

let counter = 0;
let intervalKey;
function updatableFn() {
if (counter !== 0) {
return;
}

const key = 'updatable-notification';
const initialProps = {
content: `Timer: ${counter}s`,
key,
duration: null,
closable: true,
onClose() {
clearInterval(intervalKey);
counter = 0;
},
}

notification.notice(initialProps);
intervalKey = setInterval(() => {
notification.notice({ ...initialProps, content: `Timer: ${++counter}s` });
}, 1000);
}

ReactDOM.render(<div>
<div>
<button onClick={simpleFn}>simple show</button>
<button onClick={durationFn}>duration=0</button>
<button onClick={closableFn}>closable</button>
<button onClick={manualClose}>controlled close</button>
<button onClick={updatableFn}>updatable</button>
</div>
</div>, document.getElementById('__react-content'));
11 changes: 11 additions & 0 deletions src/Notice.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export default class Notice extends Component {
this.clearCloseTimer();
}

componentDidUpdate(prevProps) {
if (this.props.duration !== prevProps.duration) {
this.restartCloseTimer();
}
}

close = () => {
this.clearCloseTimer();
this.props.onClose();
Expand All @@ -48,6 +54,11 @@ export default class Notice extends Component {
}
}

restartCloseTimer() {
this.clearCloseTimer();
this.startCloseTimer();
}

render() {
const props = this.props;
const componentClass = `${props.prefixCls}-notice`;
Expand Down
14 changes: 10 additions & 4 deletions src/Notification.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ class Notification extends Component {
const key = notice.key = notice.key || getUuid();
this.setState(previousState => {
const notices = previousState.notices;
if (!notices.filter(v => v.key === key).length) {
return {
notices: notices.concat(notice),
};
const noticeIndex = notices.map(v => v.key).indexOf(notice.key);
let updatedNotices;
if (noticeIndex === -1) {
updatedNotices = notices.concat(notice);
} else {
updatedNotices = notices.concat();
updatedNotices.splice(noticeIndex, 1, notice);
}
return {
notices: updatedNotices,
};
});
}

Expand Down
38 changes: 38 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,44 @@ describe('rc-notification', () => {
});
});

it('update notification by key with multi instance', (done) => {
Notification.newInstance({}, notification => {
const key = 'updatable';
const value = 'value';
const notUpdatableValue = 'not-updatable-value';
notification.notice({
content: <p id="not-updatable" className="not-updatable">{notUpdatableValue}</p>,
duration: null,
});
notification.notice({
content: <p id="updatable" className="updatable">{`${value}-old`}</p>,
key,
duration: null,
});
notification.notice({
content: <p id="updatable" className="updatable">{value}</p>,
key,
duration: 0.1,
});

setTimeout(() => {
// Text updated successfully
expect(document.querySelectorAll('.updatable').length).to.be(1);
expect(document.querySelector('.updatable').innerText).to.be(value);

setTimeout(() => {
// Other notices are not affected
expect(document.querySelectorAll('.not-updatable').length).to.be(1);
expect(document.querySelector('.not-updatable').innerText).to.be(notUpdatableValue);
// Duration updated successfully
expect(document.querySelectorAll('.updatable').length).to.be(0);
notification.destroy();
done();
}, 500);
}, 10);
});
});

it('freeze notification layer when mouse over', (done) => {
Notification.newInstance({}, notification => {
notification.notice({
Expand Down

0 comments on commit b288610

Please sign in to comment.