-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.js
38 lines (32 loc) · 1.01 KB
/
Button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { isCalledWithNew } from '../utils';
function Button({ $target, text, onBtnClick }) {
this.$target = null;
this.text = null;
this.onBtnClick = null;
isCalledWithNew(new.target);
this.$target = $target;
this.text = text;
this.onBtnClick = onBtnClick;
const $button = document.createElement('button');
$button.setAttribute('type', 'button');
$target.appendChild($button);
this.render = () => {
this._verifyParams();
$button.innerText = this.text;
};
this._verifyParams = () => {
if (typeof this.text !== 'string') {
return console.warn(`버튼안의 ${text}는 문자여야 합니다.`);
}
if (typeof this.onBtnClick !== 'function') {
return console.warn('onBtnClick은 함수여야 합니다.');
}
return true;
};
$button.addEventListener('click', () => {
const randomHexColor = () => `#${Math.round(Math.random()*0xFFFFFF).toString(16)}`
this.onBtnClick(randomHexColor(), randomHexColor())
});
this.render();
}
export default Button;