sweetalert2 11.7.19 → 11.7.21

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.
@@ -5,7 +5,7 @@ import { getDirectChildByClass } from './domUtils.js'
5
5
  import * as dom from './index.js'
6
6
 
7
7
  /**
8
- * @typedef { string | number | boolean } InputValue
8
+ * @typedef { string | number | boolean | undefined } InputValue
9
9
  */
10
10
 
11
11
  /**
@@ -16,7 +16,7 @@ export const handleInputOptionsAndValue = (instance, params) => {
16
16
  if (params.input === 'select' || params.input === 'radio') {
17
17
  handleInputOptions(instance, params)
18
18
  } else if (
19
- ['text', 'email', 'number', 'tel', 'textarea'].includes(params.input) &&
19
+ ['text', 'email', 'number', 'tel', 'textarea'].some((i) => i === params.input) &&
20
20
  (hasToPromiseFn(params.inputValue) || isPromise(params.inputValue))
21
21
  ) {
22
22
  showLoading(dom.getConfirmButton())
@@ -63,7 +63,7 @@ const getRadioValue = (input) => (input.checked ? input.value : null)
63
63
  * @returns {FileList | File | null}
64
64
  */
65
65
  const getFileValue = (input) =>
66
- input.files.length ? (input.getAttribute('multiple') !== null ? input.files : input.files[0]) : null
66
+ input.files && input.files.length ? (input.getAttribute('multiple') !== null ? input.files : input.files[0]) : null
67
67
 
68
68
  /**
69
69
  * @param {SweetAlert} instance
@@ -71,11 +71,18 @@ const getFileValue = (input) =>
71
71
  */
72
72
  const handleInputOptions = (instance, params) => {
73
73
  const popup = dom.getPopup()
74
+ if (!popup) {
75
+ return
76
+ }
74
77
  /**
75
78
  * @param {Record<string, any>} inputOptions
76
79
  */
77
80
  const processInputOptions = (inputOptions) => {
78
- populateInputOptions[params.input](popup, formatInputOptions(inputOptions), params)
81
+ if (params.input === 'select') {
82
+ populateSelectOptions(popup, formatInputOptions(inputOptions), params)
83
+ } else if (params.input === 'radio') {
84
+ populateRadioOptions(popup, formatInputOptions(inputOptions), params)
85
+ }
79
86
  }
80
87
  if (hasToPromiseFn(params.inputOptions) || isPromise(params.inputOptions)) {
81
88
  showLoading(dom.getConfirmButton())
@@ -96,6 +103,9 @@ const handleInputOptions = (instance, params) => {
96
103
  */
97
104
  const handleInputValue = (instance, params) => {
98
105
  const input = instance.getInput()
106
+ if (!input) {
107
+ return
108
+ }
99
109
  dom.hide(input)
100
110
  asPromise(params.inputValue)
101
111
  .then((inputValue) => {
@@ -113,89 +123,95 @@ const handleInputValue = (instance, params) => {
113
123
  })
114
124
  }
115
125
 
116
- const populateInputOptions = {
126
+ /**
127
+ * @param {HTMLElement} popup
128
+ * @param {InputOptionFlattened[]} inputOptions
129
+ * @param {SweetAlertOptions} params
130
+ */
131
+ function populateSelectOptions(popup, inputOptions, params) {
132
+ const select = getDirectChildByClass(popup, swalClasses.select)
133
+ if (!select) {
134
+ return
135
+ }
117
136
  /**
118
- * @param {HTMLElement} popup
119
- * @param {Record<string, any>} inputOptions
120
- * @param {SweetAlertOptions} params
137
+ * @param {HTMLElement} parent
138
+ * @param {string} optionLabel
139
+ * @param {string} optionValue
121
140
  */
122
- select: (popup, inputOptions, params) => {
123
- const select = getDirectChildByClass(popup, swalClasses.select)
124
- /**
125
- * @param {HTMLElement} parent
126
- * @param {string} optionLabel
127
- * @param {string} optionValue
128
- */
129
- const renderOption = (parent, optionLabel, optionValue) => {
130
- const option = document.createElement('option')
131
- option.value = optionValue
132
- dom.setInnerHtml(option, optionLabel)
133
- option.selected = isSelected(optionValue, params.inputValue)
134
- parent.appendChild(option)
141
+ const renderOption = (parent, optionLabel, optionValue) => {
142
+ const option = document.createElement('option')
143
+ option.value = optionValue
144
+ dom.setInnerHtml(option, optionLabel)
145
+ option.selected = isSelected(optionValue, params.inputValue)
146
+ parent.appendChild(option)
147
+ }
148
+ inputOptions.forEach((inputOption) => {
149
+ const optionValue = inputOption[0]
150
+ const optionLabel = inputOption[1]
151
+ // <optgroup> spec:
152
+ // https://www.w3.org/TR/html401/interact/forms.html#h-17.6
153
+ // "...all OPTGROUP elements must be specified directly within a SELECT element (i.e., groups may not be nested)..."
154
+ // check whether this is a <optgroup>
155
+ if (Array.isArray(optionLabel)) {
156
+ // if it is an array, then it is an <optgroup>
157
+ const optgroup = document.createElement('optgroup')
158
+ optgroup.label = optionValue
159
+ optgroup.disabled = false // not configurable for now
160
+ select.appendChild(optgroup)
161
+ optionLabel.forEach((o) => renderOption(optgroup, o[1], o[0]))
162
+ } else {
163
+ // case of <option>
164
+ renderOption(select, optionLabel, optionValue)
135
165
  }
136
- inputOptions.forEach((inputOption) => {
137
- const optionValue = inputOption[0]
138
- const optionLabel = inputOption[1]
139
- // <optgroup> spec:
140
- // https://www.w3.org/TR/html401/interact/forms.html#h-17.6
141
- // "...all OPTGROUP elements must be specified directly within a SELECT element (i.e., groups may not be nested)..."
142
- // check whether this is a <optgroup>
143
- if (Array.isArray(optionLabel)) {
144
- // if it is an array, then it is an <optgroup>
145
- const optgroup = document.createElement('optgroup')
146
- optgroup.label = optionValue
147
- optgroup.disabled = false // not configurable for now
148
- select.appendChild(optgroup)
149
- optionLabel.forEach((o) => renderOption(optgroup, o[1], o[0]))
150
- } else {
151
- // case of <option>
152
- renderOption(select, optionLabel, optionValue)
153
- }
154
- })
155
- select.focus()
156
- },
166
+ })
167
+ select.focus()
168
+ }
157
169
 
158
- /**
159
- * @param {HTMLElement} popup
160
- * @param {Record<string, any>} inputOptions
161
- * @param {SweetAlertOptions} params
162
- */
163
- radio: (popup, inputOptions, params) => {
164
- const radio = getDirectChildByClass(popup, swalClasses.radio)
165
- inputOptions.forEach((inputOption) => {
166
- const radioValue = inputOption[0]
167
- const radioLabel = inputOption[1]
168
- const radioInput = document.createElement('input')
169
- const radioLabelElement = document.createElement('label')
170
- radioInput.type = 'radio'
171
- radioInput.name = swalClasses.radio
172
- radioInput.value = radioValue
173
- if (isSelected(radioValue, params.inputValue)) {
174
- radioInput.checked = true
175
- }
176
- const label = document.createElement('span')
177
- dom.setInnerHtml(label, radioLabel)
178
- label.className = swalClasses.label
179
- radioLabelElement.appendChild(radioInput)
180
- radioLabelElement.appendChild(label)
181
- radio.appendChild(radioLabelElement)
182
- })
183
- const radios = radio.querySelectorAll('input')
184
- if (radios.length) {
185
- radios[0].focus()
170
+ /**
171
+ * @param {HTMLElement} popup
172
+ * @param {InputOptionFlattened[]} inputOptions
173
+ * @param {SweetAlertOptions} params
174
+ */
175
+ function populateRadioOptions(popup, inputOptions, params) {
176
+ const radio = getDirectChildByClass(popup, swalClasses.radio)
177
+ if (!radio) {
178
+ return
179
+ }
180
+ inputOptions.forEach((inputOption) => {
181
+ const radioValue = inputOption[0]
182
+ const radioLabel = inputOption[1]
183
+ const radioInput = document.createElement('input')
184
+ const radioLabelElement = document.createElement('label')
185
+ radioInput.type = 'radio'
186
+ radioInput.name = swalClasses.radio
187
+ radioInput.value = radioValue
188
+ if (isSelected(radioValue, params.inputValue)) {
189
+ radioInput.checked = true
186
190
  }
187
- },
191
+ const label = document.createElement('span')
192
+ dom.setInnerHtml(label, radioLabel)
193
+ label.className = swalClasses.label
194
+ radioLabelElement.appendChild(radioInput)
195
+ radioLabelElement.appendChild(label)
196
+ radio.appendChild(radioLabelElement)
197
+ })
198
+ const radios = radio.querySelectorAll('input')
199
+ if (radios.length) {
200
+ radios[0].focus()
201
+ }
188
202
  }
189
203
 
190
204
  /**
191
205
  * Converts `inputOptions` into an array of `[value, label]`s
192
206
  *
193
207
  * @param {Record<string, any>} inputOptions
194
- * @returns {Array<Array<string>>}
208
+ * @typedef {string[]} InputOptionFlattened
209
+ * @returns {InputOptionFlattened[]}
195
210
  */
196
211
  const formatInputOptions = (inputOptions) => {
212
+ /** @type {InputOptionFlattened[]} */
197
213
  const result = []
198
- if (typeof Map !== 'undefined' && inputOptions instanceof Map) {
214
+ if (inputOptions instanceof Map) {
199
215
  inputOptions.forEach((value, key) => {
200
216
  let valueFormatted = value
201
217
  if (typeof valueFormatted === 'object') {
@@ -223,5 +239,5 @@ const formatInputOptions = (inputOptions) => {
223
239
  * @returns {boolean}
224
240
  */
225
241
  const isSelected = (optionValue, inputValue) => {
226
- return inputValue && inputValue.toString() === optionValue.toString()
242
+ return !!inputValue && inputValue.toString() === optionValue.toString()
227
243
  }
@@ -7,12 +7,14 @@ import { warn } from './utils.js'
7
7
  */
8
8
  function setDefaultInputValidators(params) {
9
9
  // Use default `inputValidator` for supported input types if not provided
10
- if (!params.inputValidator) {
11
- Object.keys(defaultInputValidators).forEach((key) => {
12
- if (params.input === key) {
13
- params.inputValidator = defaultInputValidators[key]
14
- }
15
- })
10
+ if (params.inputValidator) {
11
+ return
12
+ }
13
+ if (params.input === 'email') {
14
+ params.inputValidator = defaultInputValidators['email']
15
+ }
16
+ if (params.input === 'url') {
17
+ params.inputValidator = defaultInputValidators['url']
16
18
  }
17
19
  }
18
20
 
package/sweetalert2.d.ts CHANGED
@@ -951,7 +951,7 @@ declare module 'sweetalert2' {
951
951
  *
952
952
  * @default undefined
953
953
  */
954
- preConfirm?(inputValue: PreConfirmCallbackValue): PreConfirmResult
954
+ preConfirm?(inputValue: PreConfirmCallbackValue): SyncOrAsync<PreConfirmResult>
955
955
 
956
956
  /**
957
957
  * Function to execute before denying, may be async (Promise-returning) or sync.
@@ -1069,7 +1069,7 @@ declare module 'sweetalert2' {
1069
1069
  *
1070
1070
  * @default undefined
1071
1071
  */
1072
- inputValidator?(inputValue: string): SyncOrAsync<string | null>
1072
+ inputValidator?(inputValue: string, validationMessage?: string): SyncOrAsync<string | null | void>
1073
1073
 
1074
1074
  /**
1075
1075
  * If you want to return the input value as `result.value` when denying the popup, set to `true`.