Skip to content

Commit

Permalink
chore: extract input logic to inputUtils.js (#1681)
Browse files Browse the repository at this point in the history
  • Loading branch information
limonte committed Jul 20, 2019
1 parent 10b6790 commit 976e42c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
11 changes: 3 additions & 8 deletions src/instanceMethods/_main.js
Expand Up @@ -2,13 +2,13 @@ import defaultParams, { showWarningsForParams } from '../utils/params.js'
import * as dom from '../utils/dom/index.js'
import { swalClasses } from '../utils/classes.js'
import Timer from '../utils/Timer.js'
import { callIfFunction, isPromise } from '../utils/utils.js'
import { callIfFunction } from '../utils/utils.js'
import setParameters from '../utils/setParameters.js'
import globalState from '../globalState.js'
import { openPopup } from '../utils/openPopup.js'
import privateProps from '../privateProps.js'
import privateMethods from '../privateMethods.js'
import { handleInputOptions, handleInputValue } from '../utils/dom/inputUtils.js'
import { handleInputOptionsAndValue } from '../utils/dom/inputUtils.js'
import { handleConfirmButtonClick, handleCancelButtonClick } from './buttons-handlers.js'
import { addKeydownHandler, setFocus } from './keydown-handler.js'
import { handlePopupClick } from './popup-click-handler.js'
Expand Down Expand Up @@ -93,12 +93,7 @@ const swalPromise = (instance, domCache, innerParams) => {
dom.removeClass(document.body, swalClasses['toast-column'])
}

// inputOptions, inputValue
if (innerParams.input === 'select' || innerParams.input === 'radio') {
handleInputOptions(instance, innerParams)
} else if (['text', 'email', 'number', 'tel', 'textarea'].includes(innerParams.input) && isPromise(innerParams.inputValue)) {
handleInputValue(instance, innerParams)
}
handleInputOptionsAndValue(instance, innerParams)

openPopup(innerParams)

Expand Down
12 changes: 10 additions & 2 deletions src/utils/dom/inputUtils.js
Expand Up @@ -4,7 +4,15 @@ import { getChildByClass } from './domUtils.js'
import { error, isPromise } from '../utils.js'
import { showLoading } from '../../staticMethods/showLoading.js'

export const handleInputOptions = (instance, params) => {
export const handleInputOptionsAndValue = (instance, params) => {
if (params.input === 'select' || params.input === 'radio') {
handleInputOptions(instance, params)
} else if (['text', 'email', 'number', 'tel', 'textarea'].includes(params.input) && isPromise(params.inputValue)) {
handleInputValue(instance, params)
}
}

const handleInputOptions = (instance, params) => {
const content = dom.getContent()
const processInputOptions = (inputOptions) => populateInputOptions[params.input](content, formatInputOptions(inputOptions), params)
if (isPromise(params.inputOptions)) {
Expand All @@ -20,7 +28,7 @@ export const handleInputOptions = (instance, params) => {
}
}

export const handleInputValue = (instance, params) => {
const handleInputValue = (instance, params) => {
const input = instance.getInput()
dom.hide(input)
params.inputValue.then((inputValue) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dom/renderers/renderInput.js
Expand Up @@ -41,7 +41,7 @@ const showInput = (params) => {
// input autofocus
setTimeout(() => {
dom.focusInput(input)
}, 0)
})
}

const removeAttributes = (input) => {
Expand Down

0 comments on commit 976e42c

Please sign in to comment.