(setting: Setting, options: NumberSettingOptions)
| 266 | * Helper for configuring a number input setting (works with SettingGroup.addSetting) |
| 267 | */ |
| 268 | export function configureNumberSetting(setting: Setting, options: NumberSettingOptions): Setting { |
| 269 | const setValue = options.debounceMs |
| 270 | ? debounce((value: number) => { |
| 271 | runAsyncSettingCallback(() => options.setValue(value)); |
| 272 | }, options.debounceMs) |
| 273 | : (value: number) => { |
| 274 | runAsyncSettingCallback(() => options.setValue(value)); |
| 275 | }; |
| 276 | |
| 277 | return setting |
| 278 | .setName(options.name) |
| 279 | .setDesc(options.desc) |
| 280 | .addText((text) => { |
| 281 | text.setValue(options.getValue().toString()).onChange((value) => { |
| 282 | const num = parseInt(value); |
| 283 | if (!isNaN(num)) { |
| 284 | if (options.min !== undefined && num < options.min) return; |
| 285 | if (options.max !== undefined && num > options.max) return; |
| 286 | setValue(num); |
| 287 | } |
| 288 | }); |
| 289 | |
| 290 | text.inputEl.type = "number"; |
| 291 | |
| 292 | if (options.placeholder) { |
| 293 | text.setPlaceholder(options.placeholder); |
| 294 | } |
| 295 | |
| 296 | if (options.min !== undefined) { |
| 297 | text.inputEl.setAttribute("min", options.min.toString()); |
| 298 | } |
| 299 | |
| 300 | if (options.max !== undefined) { |
| 301 | text.inputEl.setAttribute("max", options.max.toString()); |
| 302 | } |
| 303 | |
| 304 | if (options.ariaLabel) { |
| 305 | text.inputEl.setAttribute("aria-label", options.ariaLabel); |
| 306 | } |
| 307 | |
| 308 | // Apply consistent styling |
| 309 | text.inputEl.addClass("settings-view__input"); |
| 310 | |
| 311 | return text; |
| 312 | }); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Helper for creating standard number input settings |
no test coverage detected