-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
renderInput.js
305 lines (272 loc) · 8.26 KB
/
renderInput.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/// <reference path="../../../../sweetalert2.d.ts"/>
/**
* @typedef { HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement } Input
* @typedef { 'input' | 'file' | 'range' | 'select' | 'radio' | 'checkbox' | 'textarea' } InputClass
*/
import privateProps from '../../../privateProps.js'
import { swalClasses } from '../../classes.js'
import * as dom from '../../dom/index.js'
import { error, isPromise, warn } from '../../utils.js'
/** @type {InputClass[]} */
const inputClasses = ['input', 'file', 'range', 'select', 'radio', 'checkbox', 'textarea']
/**
* @param {SweetAlert} instance
* @param {SweetAlertOptions} params
*/
export const renderInput = (instance, params) => {
const popup = dom.getPopup()
if (!popup) {
return
}
const innerParams = privateProps.innerParams.get(instance)
const rerender = !innerParams || params.input !== innerParams.input
inputClasses.forEach((inputClass) => {
const inputContainer = dom.getDirectChildByClass(popup, swalClasses[inputClass])
if (!inputContainer) {
return
}
// set attributes
setAttributes(inputClass, params.inputAttributes)
// set class
inputContainer.className = swalClasses[inputClass]
if (rerender) {
dom.hide(inputContainer)
}
})
if (params.input) {
if (rerender) {
showInput(params)
}
// set custom class
setCustomClass(params)
}
}
/**
* @param {SweetAlertOptions} params
*/
const showInput = (params) => {
if (!params.input) {
return
}
if (!renderInputType[params.input]) {
error(`Unexpected type of input! Expected ${Object.keys(renderInputType).join(' | ')}, got "${params.input}"`)
return
}
const inputContainer = getInputContainer(params.input)
const input = renderInputType[params.input](inputContainer, params)
dom.show(inputContainer)
// input autofocus
if (params.inputAutoFocus) {
setTimeout(() => {
dom.focusInput(input)
})
}
}
/**
* @param {HTMLInputElement} input
*/
const removeAttributes = (input) => {
for (let i = 0; i < input.attributes.length; i++) {
const attrName = input.attributes[i].name
if (!['id', 'type', 'value', 'style'].includes(attrName)) {
input.removeAttribute(attrName)
}
}
}
/**
* @param {InputClass} inputClass
* @param {SweetAlertOptions['inputAttributes']} inputAttributes
*/
const setAttributes = (inputClass, inputAttributes) => {
const input = dom.getInput(dom.getPopup(), inputClass)
if (!input) {
return
}
removeAttributes(input)
for (const attr in inputAttributes) {
input.setAttribute(attr, inputAttributes[attr])
}
}
/**
* @param {SweetAlertOptions} params
*/
const setCustomClass = (params) => {
const inputContainer = getInputContainer(params.input)
if (typeof params.customClass === 'object') {
dom.addClass(inputContainer, params.customClass.input)
}
}
/**
* @param {HTMLInputElement | HTMLTextAreaElement} input
* @param {SweetAlertOptions} params
*/
const setInputPlaceholder = (input, params) => {
if (!input.placeholder || params.inputPlaceholder) {
input.placeholder = params.inputPlaceholder
}
}
/**
* @param {Input} input
* @param {Input} prependTo
* @param {SweetAlertOptions} params
*/
const setInputLabel = (input, prependTo, params) => {
if (params.inputLabel) {
const label = document.createElement('label')
const labelClass = swalClasses['input-label']
label.setAttribute('for', input.id)
label.className = labelClass
if (typeof params.customClass === 'object') {
dom.addClass(label, params.customClass.inputLabel)
}
label.innerText = params.inputLabel
prependTo.insertAdjacentElement('beforebegin', label)
}
}
/**
* @param {SweetAlertOptions['input']} inputType
* @returns {HTMLElement}
*/
const getInputContainer = (inputType) => {
return dom.getDirectChildByClass(dom.getPopup(), swalClasses[inputType] || swalClasses.input)
}
/**
* @param {HTMLInputElement | HTMLOutputElement | HTMLTextAreaElement} input
* @param {SweetAlertOptions['inputValue']} inputValue
*/
const checkAndSetInputValue = (input, inputValue) => {
if (['string', 'number'].includes(typeof inputValue)) {
input.value = `${inputValue}`
} else if (!isPromise(inputValue)) {
warn(`Unexpected type of inputValue! Expected "string", "number" or "Promise", got "${typeof inputValue}"`)
}
}
/** @type {Record<SweetAlertInput, (input: Input | HTMLElement, params: SweetAlertOptions) => Input>} */
const renderInputType = {}
/**
* @param {HTMLInputElement} input
* @param {SweetAlertOptions} params
* @returns {HTMLInputElement}
*/
renderInputType.text =
renderInputType.email =
renderInputType.password =
renderInputType.number =
renderInputType.tel =
renderInputType.url =
renderInputType.search =
renderInputType.date =
renderInputType['datetime-local'] =
renderInputType.time =
renderInputType.week =
renderInputType.month =
(input, params) => {
checkAndSetInputValue(input, params.inputValue)
setInputLabel(input, input, params)
setInputPlaceholder(input, params)
input.type = params.input
return input
}
/**
* @param {HTMLInputElement} input
* @param {SweetAlertOptions} params
* @returns {HTMLInputElement}
*/
renderInputType.file = (input, params) => {
setInputLabel(input, input, params)
setInputPlaceholder(input, params)
return input
}
/**
* @param {HTMLInputElement} range
* @param {SweetAlertOptions} params
* @returns {HTMLInputElement}
*/
renderInputType.range = (range, params) => {
const rangeInput = range.querySelector('input')
const rangeOutput = range.querySelector('output')
checkAndSetInputValue(rangeInput, params.inputValue)
rangeInput.type = params.input
checkAndSetInputValue(rangeOutput, params.inputValue)
setInputLabel(rangeInput, range, params)
return range
}
/**
* @param {HTMLSelectElement} select
* @param {SweetAlertOptions} params
* @returns {HTMLSelectElement}
*/
renderInputType.select = (select, params) => {
select.textContent = ''
if (params.inputPlaceholder) {
const placeholder = document.createElement('option')
dom.setInnerHtml(placeholder, params.inputPlaceholder)
placeholder.value = ''
placeholder.disabled = true
placeholder.selected = true
select.appendChild(placeholder)
}
setInputLabel(select, select, params)
return select
}
/**
* @param {HTMLInputElement} radio
* @returns {HTMLInputElement}
*/
renderInputType.radio = (radio) => {
radio.textContent = ''
return radio
}
/**
* @param {HTMLLabelElement} checkboxContainer
* @param {SweetAlertOptions} params
* @returns {HTMLInputElement}
*/
renderInputType.checkbox = (checkboxContainer, params) => {
const checkbox = dom.getInput(dom.getPopup(), 'checkbox')
checkbox.value = '1'
checkbox.checked = Boolean(params.inputValue)
const label = checkboxContainer.querySelector('span')
dom.setInnerHtml(label, params.inputPlaceholder)
return checkbox
}
/**
* @param {HTMLTextAreaElement} textarea
* @param {SweetAlertOptions} params
* @returns {HTMLTextAreaElement}
*/
renderInputType.textarea = (textarea, params) => {
checkAndSetInputValue(textarea, params.inputValue)
setInputPlaceholder(textarea, params)
setInputLabel(textarea, textarea, params)
/**
* @param {HTMLElement} el
* @returns {number}
*/
const getMargin = (el) =>
parseInt(window.getComputedStyle(el).marginLeft) + parseInt(window.getComputedStyle(el).marginRight)
// https://github.com/sweetalert2/sweetalert2/issues/2291
setTimeout(() => {
// https://github.com/sweetalert2/sweetalert2/issues/1699
if ('MutationObserver' in window) {
const initialPopupWidth = parseInt(window.getComputedStyle(dom.getPopup()).width)
const textareaResizeHandler = () => {
// check if texarea is still in document (i.e. popup wasn't closed in the meantime)
if (!document.body.contains(textarea)) {
return
}
const textareaWidth = textarea.offsetWidth + getMargin(textarea)
if (textareaWidth > initialPopupWidth) {
dom.getPopup().style.width = `${textareaWidth}px`
} else {
dom.applyNumericalStyle(dom.getPopup(), 'width', params.width)
}
}
new MutationObserver(textareaResizeHandler).observe(textarea, {
attributes: true,
attributeFilter: ['style'],
})
}
})
return textarea
}