Skip to content

Commit

Permalink
Merge 88d8096 into 150a09b
Browse files Browse the repository at this point in the history
  • Loading branch information
eternalsky committed Aug 31, 2020
2 parents 150a09b + 88d8096 commit e37c373
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 55 deletions.
15 changes: 11 additions & 4 deletions demo/MessageDemo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Message.config({
document.body.appendChild(div);
return div;
},
// multipleInstance: false,
multipleInstance: false,
});

class Demo extends React.Component {
Expand All @@ -26,11 +26,18 @@ class Demo extends React.Component {
this.state = {};
}

componentWillMount() {
const hide = Message.mask_loading('loading', 10);
setTimeout(() => {
hide();
}, 3000);
}

handleClick(type) {
Message[type](type, 3);
setTimeout(() => {
Message.clear();
}, 1500);
// setTimeout(() => {
// Message.clear();
// }, 1500);
}

render() {
Expand Down
119 changes: 73 additions & 46 deletions src/Message.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,27 @@ function createMessageInstance(options, type) {
if (messageInstance && messageInstance.destroy) {
messageInstance.destroy();
}
let notification = null;
Notification.newInstance(
{
prefixCls,
className,
transitionName: type === 'mask_loading' ? '' : transitionName,
getContainer,
style: {
left: '50%',
}, // 覆盖原来的样式
},
(n) => {
notification = n;
messageInstance = n;
},
);

return notification;


return new Promise((resolve) => {
let notification = null;
Notification.newInstance(
{
prefixCls,
className,
transitionName: type === 'mask_loading' ? '' : transitionName,
getContainer,
style: {
left: '50%',
}, // 覆盖原来的样式
},
(n) => {
notification = n;
messageInstance = n;
resolve(notification);
},
);
});
}

function incrementCounter() {
Expand Down Expand Up @@ -99,38 +102,62 @@ function notice(content, duration = defaultDuration, type, onClose) {
activeContentStyle = { padding: '23px 38px', backgroundColor: '#fff' };
}

instance.notice({
key,
duration: options ? options.duration : duration,
className: options ? options.className : null,
style: activeWrapStyle,
content: (
<div
className={classnames({
[`${prefixCls}-container ${prefixCls}-container-${type}`]: true,
'fn-clear': true,
})}
style={activeContentStyle}
>
<i className={iconClass} />
<div className={`${prefixCls}-content`}>{options ? options.content : content}</div>
</div>
),
onClose(...params) {
// see https://github.com/uxcore/uxcore-message/issues/17
decrementCounter();
tryRemoveMessageInstance();

const callback = (options && options.onClose) || onClose || function noop() {};

callback(...params);
},
});
const getNoticeFunc = () => {
const target = key;
return (inst) => {
inst.notice({
key: target,
duration: options ? options.duration : duration,
className: options ? options.className : null,
style: activeWrapStyle,
content: (
<div
className={classnames({
[`${prefixCls}-container ${prefixCls}-container-${type}`]: true,
'fn-clear': true,
})}
style={activeContentStyle}
>
<i className={iconClass} />
<div className={`${prefixCls}-content`}>{options ? options.content : content}</div>
</div>
),
onClose(...params) {
// see https://github.com/uxcore/uxcore-message/issues/17
decrementCounter();
tryRemoveMessageInstance();

const callback = (options && options.onClose) || onClose || function noop() {};

callback(...params);
},
});
};
};

const noticeFunc = getNoticeFunc();

if (instance.then) {
instance.then((inst) => {
noticeFunc(inst);
});
} else {
noticeFunc(instance);
}


return (function removeNotice() {
const target = key;
key += 1;
return () => instance.removeNotice(target);
return () => {
if (instance.then) {
instance.then((inst) => {
inst.removeNotice(target);
});
} else {
instance.removeNotice(target);
}
};
}());
}

Expand Down
25 changes: 20 additions & 5 deletions tests/Message.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,25 @@ describe('Message', () => {
content: 'this is second msg',
className: 'multi',
});
expect($$('.multi').length).to.be(2);
done();
setTimeout(() => {
expect($$('.multi').length).to.be(2);
done();
});
});

it('should be able to close by returned handler', (done) => {
const hide = Message.mask_loading({
content: 'loading',
duration: 10,
className: 'handler-test',
});
setTimeout(() => {
hide();
}, 1000);
setTimeout(() => {
expect($$('.handler-test').length).to.be(0);
done();
}, 1500);
});

it('should call the close callback', (done) => {
Expand Down Expand Up @@ -69,9 +86,7 @@ describe('Message', () => {
});

const instances = Array.prototype.slice.call($$('.uxcore-icon'));
const matched = instances.filter(item =>
iconArr.indexOf(item.getAttribute('class')) !== -1,
);
const matched = instances.filter(item => iconArr.indexOf(item.getAttribute('class')) !== -1);
expect(matched.length).to.be(instances.length);
done();
});
Expand Down

0 comments on commit e37c373

Please sign in to comment.