Skip to content

Commit

Permalink
fix: create label inputs on page load
Browse files Browse the repository at this point in the history
Co-authored-by: Andrea Stagi <stagi.andrea@gmail.com>
  • Loading branch information
PiemP and astagi committed Dec 5, 2023
1 parent 90e6a10 commit 41b26d7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 54 deletions.
5 changes: 4 additions & 1 deletion src/js/plugins/input-label.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class InputLabel {
this._labelOut()
this._labelOver()
}
this._bindEvents()

if (label && label.getAttribute('it-bs-static') === null) {
this._bindEvents()
}
}

_bindEvents() {
Expand Down
19 changes: 7 additions & 12 deletions src/js/plugins/input-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const EVENT_CHANGE = `change${EVENT_KEY}`

//const EVENT_FOCUS_DATA_API = `focus${EVENT_KEY}${DATA_API_KEY}`
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
const EVENT_MOUSEDOWN_DATA_API = `mousedown${EVENT_KEY}${DATA_API_KEY}`
const EVENT_KEYUP_DATA_API = `keyup${EVENT_KEY}${DATA_API_KEY}`

const CLASS_NAME_ADAPTIVE = 'input-number-adaptive'
const CLASS_NAME_PERCENTAGE = 'input-number-percentage'
Expand Down Expand Up @@ -168,17 +166,14 @@ const createInput = (element) => {
return null
}

EventHandler.on(document, EVENT_MOUSEDOWN_DATA_API, SELECTOR_INPUT + ', label', function () {
const target = InputLabel.getInputFromLabel(this) || this
createInput(target)
})
EventHandler.on(document, EVENT_KEYUP_DATA_API, SELECTOR_INPUT + ', label', function () {
const target = InputLabel.getInputFromLabel(this) || this
const element = createInput(target)
if (element && element._label) {
element._label._labelOut()
}
document.addEventListener('DOMContentLoaded', function () {
var frmel = document.querySelectorAll(SELECTOR_INPUT + ', label')
frmel.forEach(function (item) {
const target = InputLabel.getInputFromLabel(item) || item
createInput(target)
})
})

EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_BTN, function () {
const wrapper = this.closest(SELECTOR_WRAPPER)
if (wrapper) {
Expand Down
19 changes: 6 additions & 13 deletions src/js/plugins/input-search-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ import InputLabel from './input-label'
const NAME = 'inputsearchautocomplete'
const DATA_KEY = 'bs.inputsearchautocomplete'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'

const Default = {
autocomplete: [],
}

const EVENT_KEYUP = `keyup${EVENT_KEY}`
const EVENT_KEYUP_DATA_API = `keyup${EVENT_KEY}${DATA_API_KEY}`
const EVENT_MOUSEDOWN_DATA_API = `mousedown${EVENT_KEY}${DATA_API_KEY}`

const CLASS_NAME_SHOW = 'autocomplete-list-show'
const CLASS_NAME_AUTOCOMPLETE = 'autocomplete'
Expand Down Expand Up @@ -137,16 +134,12 @@ const createInput = (element) => {
return null
}

EventHandler.on(document, EVENT_MOUSEDOWN_DATA_API, SELECTOR_SEARCH + ', label', function () {
const target = InputLabel.getInputFromLabel(this) || this
createInput(target)
})
EventHandler.on(document, EVENT_KEYUP_DATA_API, SELECTOR_SEARCH + ', label', function () {
const target = InputLabel.getInputFromLabel(this) || this
const element = createInput(target)
if (element && element._label) {
element._label._labelOut()
}
document.addEventListener('DOMContentLoaded', function () {
var frmel = document.querySelectorAll(SELECTOR_SEARCH + ', label')
frmel.forEach(function (item) {
const target = InputLabel.getInputFromLabel(item) || item
createInput(target)
})
})

export default InputSearch
36 changes: 8 additions & 28 deletions src/js/plugins/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import InputLabel from './input-label'
const NAME = 'input'
const DATA_KEY = 'bs.input'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'

const EVENT_MOUSEDOWN_DATA_API = `mousedown${EVENT_KEY}${DATA_API_KEY}`
const EVENT_KEYUP_DATA_API = `keyup${EVENT_KEY}${DATA_API_KEY}`

const EVENT_CHANGE = `change${EVENT_KEY}`

Expand Down Expand Up @@ -67,26 +63,14 @@ class Input extends BaseComponent {
*/
const excludes = [
'select',
//'input[data-bs-input][type="number"]',
'input[data-bs-input][type="password"]',
'input.input-password[data-bs-input]',
'input[data-bs-autocomplete][type="search"]',
'input[type="time"]',
'input[type="radio"]',
'input[type="checkbox"]',
]

/*const inputs = SelectorEngine.find('input, textarea').filter((input) => {
let result = true
excludes.forEach((selector) => {
if (input.matches(selector)) {
result = false
}
})
return result
})
inputs.forEach((input) => {
Input.getOrCreateInstance(input)
})*/

const createInput = (element) => {
const toExclude = !!excludes.find((selector) => element.matches(selector))
const isInputNumber = !!(element.getAttribute('type') === 'number' && element.parentNode.querySelector('button[class^="input-number-"]')) //check if it's a InputNumber component
Expand All @@ -96,16 +80,12 @@ const createInput = (element) => {
return null
}

EventHandler.on(document, EVENT_MOUSEDOWN_DATA_API, 'input, textarea, label', function () {
const target = InputLabel.getInputFromLabel(this) || this
createInput(target)
})
EventHandler.on(document, EVENT_KEYUP_DATA_API, 'input, textarea, label', function () {
const target = InputLabel.getInputFromLabel(this) || this
const element = createInput(target)
if (element && element._label) {
element._label._labelOut()
}
document.addEventListener('DOMContentLoaded', function () {
var frmel = document.querySelectorAll('input, textarea, label')
frmel.forEach(function (item) {
const target = InputLabel.getInputFromLabel(item) || item
createInput(target)
})
})

export default Input

0 comments on commit 41b26d7

Please sign in to comment.