-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathbutton-group.ts
84 lines (81 loc) · 2.73 KB
/
button-group.ts
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
import { addClass, createElement as internalCreateElement, isNullOrUndefined } from '@syncfusion/ej2-base';
import { ButtonModel, Button } from '@syncfusion/ej2-buttons';
/**
* Initialize ButtonGroup CSS component with specified properties.
* ```html
* <div id='buttongroup'>
* <button></button>
* <button></button>
* <button></button>
* </div>
* ```
* ```typescript
* createButtonGroup('#buttongroup', {
* cssClass: 'e-outline',
* buttons: [
* { content: 'Day' },
* { content: 'Week' },
* { content: 'Work Week'}
* ]
* });
* ```
*
* @param {string} selector
* @param {CreateButtonGroupModel} options
* @returns HTMLElement
*/
/**
* Creates button group.
*
* @param {string} selector - Specifies the selector.
* @param {CreateButtonGroupModel} options - Specifies the button group model.
* @param {Function} createElement - Specifies the element.
* @returns {HTMLElement} - Button group element.
*/
export function createButtonGroup(selector: string, options: CreateButtonGroupModel = {}, createElement?: Function): HTMLElement {
let child: Element;
let btnElem: Element;
let nextChild: Element;
let btnModel: ButtonModel | null;
if (isNullOrUndefined(createElement)) {
createElement = internalCreateElement;
}
const wrapper: HTMLElement = document.querySelector(selector) as HTMLElement;
addClass([wrapper], ['e-btn-group', 'e-css']);
wrapper.setAttribute('role', 'group');
const childs: HTMLCollection = wrapper.children;
options.buttons = options.buttons || [] as ButtonModel[];
for (let i: number = 0, j: number = 0; j < childs.length; i++, j++) {
child = childs[j as number];
btnModel = options.buttons[i as number];
if (btnModel !== null) {
if (child.tagName === 'BUTTON') {
btnElem = child;
} else {
btnElem = createElement('label');
nextChild = childs[j + 1];
if (nextChild) {
wrapper.insertBefore(btnElem, nextChild);
} else {
wrapper.appendChild(btnElem);
}
if (child.id) {
btnElem.setAttribute('for', child.id);
}
if (btnModel && btnModel.disabled) {
(child as HTMLInputElement).disabled = true;
}
j++;
}
if (options.cssClass && btnModel && !btnModel.cssClass) {
btnModel.cssClass = options.cssClass;
}
new Button(btnModel || {}, btnElem as HTMLButtonElement);
}
}
return wrapper;
}
export interface CreateButtonGroupModel {
cssClass?: string;
buttons?: (ButtonModel | null)[];
}