-
Notifications
You must be signed in to change notification settings - Fork 152
/
float-label.ts
178 lines (171 loc) · 7.57 KB
/
float-label.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* FloatLable Moduel
* Specifies whether to display the floating label above the input element.
*/
import { removeClass, addClass, detach } from '@syncfusion/ej2-base';
import { attributes, isNullOrUndefined, createElement, getUniqueID } from '@syncfusion/ej2-base';
import { FloatLabelType } from '@syncfusion/ej2-inputs';
const FLOATLINE: string = 'e-float-line';
const FLOATTEXT: string = 'e-float-text';
const LABELTOP: string = 'e-label-top';
const LABELBOTTOM: string = 'e-label-bottom';
/* eslint-disable valid-jsdoc */
/**
* Function to create Float Label element.
*
* @param {HTMLDivElement} overAllWrapper - Overall wrapper of multiselect.
* @param {HTMLElement} searchWrapper - Search wrapper of multiselect.
* @param {HTMLElement} element - The given html element.
* @param {HTMLInputElement} inputElement - Specify the input wrapper.
* @param {number[] | string[] | boolean[]} value - Value of the MultiSelect.
* @param {FloatLabelType} floatLabelType - Specify the FloatLabel Type.
* @param {string} placeholder - Specify the PlaceHolder text.
*/
export function createFloatLabel(
overAllWrapper: HTMLDivElement,
searchWrapper: HTMLElement, element: HTMLElement,
inputElement: HTMLInputElement, value: number[] | string[] | boolean[] | object[], floatLabelType: FloatLabelType,
placeholder: string): void {
const floatLinelement: HTMLElement = createElement('span', { className: FLOATLINE });
const floatLabelElement: HTMLElement = createElement('label', { className: FLOATTEXT });
const id: string = element.getAttribute('id') ? element.getAttribute('id') : getUniqueID('ej2_multiselect');
element.id = id;
if (!isNullOrUndefined(element.id) && element.id !== '') {
floatLabelElement.id = 'label_' + element.id.replace(/ /g, '_');
floatLabelElement.setAttribute('for', element.id);
attributes(inputElement, { 'aria-labelledby': floatLabelElement.id });
}
if (!isNullOrUndefined(inputElement.placeholder) && inputElement.placeholder !== '') {
floatLabelElement.innerText = encodePlaceholder(inputElement.placeholder);
inputElement.removeAttribute('placeholder');
}
floatLabelElement.innerText = encodePlaceholder(placeholder);
searchWrapper.appendChild(floatLinelement);
searchWrapper.appendChild(floatLabelElement);
overAllWrapper.classList.add('e-float-input');
updateFloatLabelState(value, floatLabelElement);
if (floatLabelType === 'Always') {
if (floatLabelElement.classList.contains(LABELBOTTOM)) {
removeClass([floatLabelElement], LABELBOTTOM);
}
addClass([floatLabelElement], LABELTOP);
}
}
/**
* Function to update status of the Float Label element.
*
* @param {string[] | number[] | boolean[]} value - Value of the MultiSelect.
* @param {HTMLElement} label - Float label element.
*/
export function updateFloatLabelState(value: string[] | number[] | boolean[] | object[], label: HTMLElement): void {
if (value && value.length > 0) {
addClass([label], LABELTOP);
removeClass([label], LABELBOTTOM);
} else {
removeClass([label], LABELTOP);
addClass([label], LABELBOTTOM);
}
}
/**
* Function to remove Float Label element.
*
* @param {HTMLDivElement} overAllWrapper - Overall wrapper of multiselect.
* @param {HTMLDivElement} componentWrapper - Wrapper element of multiselect.
* @param {HTMLElement} searchWrapper - Search wrapper of multiselect.
* @param {HTMLInputElement} inputElement - Specify the input wrapper.
* @param {number[] | string[] | boolean[]} value - Value of the MultiSelect.
* @param {FloatLabelType} floatLabelType - Specify the FloatLabel Type.
* @param {string} placeholder - Specify the PlaceHolder text.
*/
export function removeFloating(
overAllWrapper: HTMLDivElement,
componentWrapper: HTMLDivElement,
searchWrapper: HTMLElement,
inputElement: HTMLInputElement,
value: number[] | string[] | boolean[] | object[],
floatLabelType: FloatLabelType,
placeholder: string): void {
const placeholderElement: HTMLElement = componentWrapper.querySelector('.' + FLOATTEXT) as HTMLElement;
const floatLine: HTMLElement = componentWrapper.querySelector('.' + FLOATLINE) as HTMLElement;
let placeholderText: string;
if (!isNullOrUndefined(placeholderElement)) {
placeholderText = placeholderElement.innerText;
detach(searchWrapper.querySelector('.' + FLOATTEXT));
setPlaceHolder(value, inputElement, placeholderText);
if (!isNullOrUndefined(floatLine)) {
detach(searchWrapper.querySelector('.' + FLOATLINE));
}
} else {
placeholderText = (placeholder !== null) ? placeholder : '';
setPlaceHolder(value, inputElement, placeholderText);
}
overAllWrapper.classList.remove('e-float-input');
}
/**
* Function to set the placeholder to the element.
*
* @param {number[] | string[] | boolean[]} value - Value of the MultiSelect.
* @param {HTMLInputElement} inputElement - Specify the input wrapper.
* @param {string} placeholder - Specify the PlaceHolder text.
*/
export function setPlaceHolder(value: number[] | string[] | boolean[] | object[],
inputElement: HTMLInputElement, placeholder: string): void {
if (value && value.length) {
inputElement.placeholder = '';
} else {
inputElement.placeholder = placeholder;
}
}
/**
* Function for focusing the Float Element.
*
* @param {HTMLDivElement} overAllWrapper - Overall wrapper of multiselect.
* @param {HTMLDivElement} componentWrapper - Wrapper element of multiselect.
*/
export function floatLabelFocus(overAllWrapper: HTMLDivElement, componentWrapper: HTMLDivElement): void {
overAllWrapper.classList.add('e-input-focus');
const label: Element = componentWrapper.querySelector('.' + FLOATTEXT);
if (!isNullOrUndefined(label)) {
addClass([label], LABELTOP);
if (label.classList.contains(LABELBOTTOM)) {
removeClass([label], LABELBOTTOM);
}
}
}
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Function to focus the Float Label element.
*
* @param {HTMLDivElement} overAllWrapper - Overall wrapper of multiselect.
* @param {HTMLDivElement} componentWrapper - Wrapper element of multiselect.
* @param {number[] | string[] | boolean[]} value - Value of the MultiSelect.
* @param {FloatLabelType} floatLabelType - Specify the FloatLabel Type.
* @param {string} placeholder - Specify the PlaceHolder text.
*/
export function floatLabelBlur(
overAllWrapper: HTMLDivElement,
componentWrapper: HTMLDivElement,
value: number[] | string[] | boolean[] |object[],
floatLabelType: FloatLabelType,
placeholder: string): void {
/* eslint-enable @typescript-eslint/no-unused-vars */
overAllWrapper.classList.remove('e-input-focus');
const label: Element = componentWrapper.querySelector('.' + FLOATTEXT);
if (value && value.length <= 0 && floatLabelType === 'Auto' && !isNullOrUndefined(label)) {
if (label.classList.contains(LABELTOP)) {
removeClass([label], LABELTOP);
}
addClass([label], LABELBOTTOM);
}
}
export function encodePlaceholder(placeholder: string): string {
let result: string = '';
if (!isNullOrUndefined(placeholder) && placeholder !== '') {
const spanElement: HTMLElement = document.createElement('span');
spanElement.innerHTML = '<input placeholder="' + placeholder + '"/>';
const hiddenInput: HTMLInputElement = (spanElement.children[0]) as HTMLInputElement;
result = hiddenInput.placeholder;
}
return result;
}
/* eslint-enable valid-jsdoc */