forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
91 lines (84 loc) · 1.79 KB
/
index.js
File metadata and controls
91 lines (84 loc) · 1.79 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import "./styles.scss";
import Ref from "html-tag-js/ref";
/**
* @typedef {Object} Checkbox
* @property {string} text
* @property {Ref} ref
* @property {boolean} checked
* @property {string} [name]
* @property {string} [id]
* @property {string} [size]
* @property {"checkbox"|"radio"} [type]
*/
/**
* Create a checkbox
* @param {string | Checkbox} text Checkbox label
* @param {Boolean} checked Whether checkbox is checked or not
* @param {string} [name] Name of checkbox
* @param {string} [id] Id of checkbox
* @param {"checkbox"|"radio"} [type] Type of checkbox
* @param {Ref} [ref] A reference to the input element
* @param {string} [size] Size of checkbox
* @returns {Checkbox}
*/
function Checkbox(text, checked, name, id, type, ref, size) {
if (typeof text === "object") {
({ text, checked, name, id, type, ref, size } = text);
}
size = size || "1rem";
const $input = ref || new Ref();
const $checkbox = (
<label className="input-checkbox">
<input
ref={$input}
checked={checked}
type={type || "checkbox"}
name={name}
id={id}
/>
<span style={{ height: size, width: size }} className="box" />
<span>{text}</span>
</label>
);
Object.defineProperties($checkbox, {
checked: {
get() {
return !!$input.el.checked;
},
set(value) {
$input.el.checked = value;
},
},
onclick: {
get() {
return $input.el.onclick;
},
set(onclick) {
$input.el.onclick = onclick;
},
},
onchange: {
get() {
return $input.el.onchange;
},
set(onchange) {
$input.el.onchange = onchange;
},
},
value: {
get() {
return this.checked;
},
set(value) {
this.checked = value;
},
},
toggle: {
value() {
this.checked = !this.checked;
},
},
});
return $checkbox;
}
export default Checkbox;