sweetalert2 11.26.12 → 11.26.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -185,9 +185,9 @@ const checkAndSetInputValue = (input, inputValue) => {
185
185
  const renderInputType = {}
186
186
 
187
187
  /**
188
- * @param {HTMLInputElement} input
188
+ * @param {Input | HTMLElement} input
189
189
  * @param {SweetAlertOptions} params
190
- * @returns {HTMLInputElement}
190
+ * @returns {Input}
191
191
  */
192
192
  renderInputType.text =
193
193
  renderInputType.email =
@@ -203,90 +203,113 @@ renderInputType.text =
203
203
  renderInputType.month =
204
204
  /** @type {(input: Input | HTMLElement, params: SweetAlertOptions) => Input} */
205
205
  (input, params) => {
206
- checkAndSetInputValue(input, params.inputValue)
207
- setInputLabel(input, input, params)
208
- setInputPlaceholder(input, params)
209
- input.type = params.input
210
- return input
206
+ const inputElement = /** @type {HTMLInputElement} */ (input)
207
+ checkAndSetInputValue(inputElement, params.inputValue)
208
+ setInputLabel(inputElement, inputElement, params)
209
+ setInputPlaceholder(inputElement, params)
210
+ inputElement.type = /** @type {string} */ (params.input)
211
+ return inputElement
211
212
  }
212
213
 
213
214
  /**
214
- * @param {HTMLInputElement} input
215
+ * @param {Input | HTMLElement} input
215
216
  * @param {SweetAlertOptions} params
216
- * @returns {HTMLInputElement}
217
+ * @returns {Input}
217
218
  */
218
219
  renderInputType.file = (input, params) => {
219
- setInputLabel(input, input, params)
220
- setInputPlaceholder(input, params)
221
- return input
220
+ const inputElement = /** @type {HTMLInputElement} */ (input)
221
+ setInputLabel(inputElement, inputElement, params)
222
+ setInputPlaceholder(inputElement, params)
223
+ return inputElement
222
224
  }
223
225
 
224
226
  /**
225
- * @param {HTMLInputElement} range
227
+ * @param {Input | HTMLElement} range
226
228
  * @param {SweetAlertOptions} params
227
- * @returns {HTMLInputElement}
229
+ * @returns {Input}
228
230
  */
229
231
  renderInputType.range = (range, params) => {
230
- const rangeInput = range.querySelector('input')
231
- const rangeOutput = range.querySelector('output')
232
- checkAndSetInputValue(rangeInput, params.inputValue)
233
- rangeInput.type = params.input
234
- checkAndSetInputValue(rangeOutput, params.inputValue)
235
- setInputLabel(rangeInput, range, params)
236
- return range
232
+ const rangeContainer = /** @type {HTMLElement} */ (range)
233
+ const rangeInput = rangeContainer.querySelector('input')
234
+ const rangeOutput = rangeContainer.querySelector('output')
235
+ if (rangeInput) {
236
+ checkAndSetInputValue(rangeInput, params.inputValue)
237
+ rangeInput.type = /** @type {string} */ (params.input)
238
+ setInputLabel(rangeInput, /** @type {Input} */ (range), params)
239
+ }
240
+ if (rangeOutput) {
241
+ checkAndSetInputValue(rangeOutput, params.inputValue)
242
+ }
243
+ return /** @type {Input} */ (range)
237
244
  }
238
245
 
239
246
  /**
240
- * @param {HTMLSelectElement} select
247
+ * @param {Input | HTMLElement} select
241
248
  * @param {SweetAlertOptions} params
242
- * @returns {HTMLSelectElement}
249
+ * @returns {Input}
243
250
  */
244
251
  renderInputType.select = (select, params) => {
245
- select.textContent = ''
252
+ const selectElement = /** @type {HTMLSelectElement} */ (select)
253
+ selectElement.textContent = ''
246
254
  if (params.inputPlaceholder) {
247
255
  const placeholder = document.createElement('option')
248
256
  dom.setInnerHtml(placeholder, params.inputPlaceholder)
249
257
  placeholder.value = ''
250
258
  placeholder.disabled = true
251
259
  placeholder.selected = true
252
- select.appendChild(placeholder)
260
+ selectElement.appendChild(placeholder)
253
261
  }
254
- setInputLabel(select, select, params)
255
- return select
262
+ setInputLabel(selectElement, selectElement, params)
263
+ return selectElement
256
264
  }
257
265
 
258
266
  /**
259
- * @param {HTMLInputElement} radio
260
- * @returns {HTMLInputElement}
267
+ * @param {Input | HTMLElement} radio
268
+ * @returns {Input}
261
269
  */
262
270
  renderInputType.radio = (radio) => {
263
- radio.textContent = ''
264
- return radio
271
+ const radioElement = /** @type {HTMLElement} */ (radio)
272
+ radioElement.textContent = ''
273
+ return /** @type {Input} */ (radio)
265
274
  }
266
275
 
267
276
  /**
268
- * @param {HTMLLabelElement} checkboxContainer
277
+ * @param {Input | HTMLElement} checkboxContainer
269
278
  * @param {SweetAlertOptions} params
270
- * @returns {HTMLInputElement}
279
+ * @returns {Input}
271
280
  */
272
281
  renderInputType.checkbox = (checkboxContainer, params) => {
273
- const checkbox = dom.getInput(dom.getPopup(), 'checkbox')
282
+ const popup = dom.getPopup()
283
+ if (!popup) {
284
+ throw new Error('Popup not found')
285
+ }
286
+ const checkbox = dom.getInput(popup, 'checkbox')
287
+ if (!checkbox) {
288
+ throw new Error('Checkbox input not found')
289
+ }
274
290
  checkbox.value = '1'
275
291
  checkbox.checked = Boolean(params.inputValue)
276
- const label = checkboxContainer.querySelector('span')
277
- dom.setInnerHtml(label, params.inputPlaceholder || params.inputLabel)
292
+ const containerElement = /** @type {HTMLElement} */ (checkboxContainer)
293
+ const label = containerElement.querySelector('span')
294
+ if (label) {
295
+ const placeholderOrLabel = params.inputPlaceholder || params.inputLabel
296
+ if (placeholderOrLabel) {
297
+ dom.setInnerHtml(label, placeholderOrLabel)
298
+ }
299
+ }
278
300
  return checkbox
279
301
  }
280
302
 
281
303
  /**
282
- * @param {HTMLTextAreaElement} textarea
304
+ * @param {Input | HTMLElement} textarea
283
305
  * @param {SweetAlertOptions} params
284
- * @returns {HTMLTextAreaElement}
306
+ * @returns {Input}
285
307
  */
286
308
  renderInputType.textarea = (textarea, params) => {
287
- checkAndSetInputValue(textarea, params.inputValue)
288
- setInputPlaceholder(textarea, params)
289
- setInputLabel(textarea, textarea, params)
309
+ const textareaElement = /** @type {HTMLTextAreaElement} */ (textarea)
310
+ checkAndSetInputValue(textareaElement, params.inputValue)
311
+ setInputPlaceholder(textareaElement, params)
312
+ setInputLabel(textareaElement, textareaElement, params)
290
313
 
291
314
  /**
292
315
  * @param {HTMLElement} el
@@ -299,25 +322,32 @@ renderInputType.textarea = (textarea, params) => {
299
322
  setTimeout(() => {
300
323
  // https://github.com/sweetalert2/sweetalert2/issues/1699
301
324
  if ('MutationObserver' in window) {
302
- const initialPopupWidth = parseInt(window.getComputedStyle(dom.getPopup()).width)
325
+ const popup = dom.getPopup()
326
+ if (!popup) {
327
+ return
328
+ }
329
+ const initialPopupWidth = parseInt(window.getComputedStyle(popup).width)
303
330
  const textareaResizeHandler = () => {
304
331
  // check if texarea is still in document (i.e. popup wasn't closed in the meantime)
305
- if (!document.body.contains(textarea)) {
332
+ if (!document.body.contains(textareaElement)) {
306
333
  return
307
334
  }
308
- const textareaWidth = textarea.offsetWidth + getMargin(textarea)
309
- if (textareaWidth > initialPopupWidth) {
310
- dom.getPopup().style.width = `${textareaWidth}px`
311
- } else {
312
- dom.applyNumericalStyle(dom.getPopup(), 'width', params.width)
335
+ const textareaWidth = textareaElement.offsetWidth + getMargin(textareaElement)
336
+ const popupElement = dom.getPopup()
337
+ if (popupElement) {
338
+ if (textareaWidth > initialPopupWidth) {
339
+ popupElement.style.width = `${textareaWidth}px`
340
+ } else {
341
+ dom.applyNumericalStyle(popupElement, 'width', params.width)
342
+ }
313
343
  }
314
344
  }
315
- new MutationObserver(textareaResizeHandler).observe(textarea, {
345
+ new MutationObserver(textareaResizeHandler).observe(textareaElement, {
316
346
  attributes: true,
317
347
  attributeFilter: ['style'],
318
348
  })
319
349
  }
320
350
  })
321
351
 
322
- return textarea
352
+ return textareaElement
323
353
  }