Skip to content

Commit

Permalink
fix: separate event setting from button creation
Browse files Browse the repository at this point in the history
Fixes the bug of not setting events on a button because the button is
not yet created or appended to the DOM. Event setting relies on the
element's ID in the DOM. Separated event setting into another method to
address this issue. Additionally, return `this` in methods where
chaining is necessary.
  • Loading branch information
chessurisme committed Jun 5, 2024
1 parent 794bc87 commit b312723
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
31 changes: 5 additions & 26 deletions src/components/button/button-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,33 @@

import { isConfigVerified } from '@utilities/config/config-verifier'
import { setAttributes } from '@utilities/components/set-attributes'
import { setEvents } from '@utilities/components/set-events'

function createButton(config) {
if (!isConfigVerified('button', config)) return

const { type, id, class_name, icon, events } = config
const { type, id, class_name, icon } = config
let { text } = config

let button

switch (type) {
case 'rounded-square':
text = undefined
button = createContainer(
icon,
text,
id,
class_name,
events,
'rounded-square'
)
button = createContainer(icon, text, id, class_name, 'rounded-square')
break
case 'slab':
if (!text && !icon) return
button = createContainer(icon, text, id, class_name, events, 'slab')
button = createContainer(icon, text, id, class_name, 'slab')
break
default:
if (!text && !icon) return
button = createContainer(
icon,
text,
id,
class_name,
events,
'transparent'
)
button = createContainer(icon, text, id, class_name, 'transparent')
}

return button
}

function createContainer(icon, text, id, class_name, events, type) {
if (!events) {
console.error('Button is useless without events.')
return
}

function createContainer(icon, text, id, class_name, type) {
const BUTTON = document.createElement('div')
setAttributes(BUTTON, {
id: `button-${id}`,
Expand All @@ -72,7 +52,6 @@ function createContainer(icon, text, id, class_name, events, type) {
BUTTON.appendChild(TEXT)
}

setEvents(events)
return BUTTON
}

Expand Down
12 changes: 12 additions & 0 deletions src/components/button/button-set-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

import { isConfigVerified } from '@utilities/config/config-verifier'
import { setEvents } from '@utilities/components/set-events'

function setButtonEvents(config) {
if (!isConfigVerified('button', config)) return
const { id, events } = config
setEvents(id, events)
}

export { setButtonEvents }
5 changes: 5 additions & 0 deletions src/components/button/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { isConfigVerified } from '@utilities/config/config-verifier'
import { createButton } from './button-create'
import { setEvents } from './button-set-events'
import { removeButton } from './button-remove'

class Button {
Expand All @@ -14,6 +15,10 @@ class Button {
createButton(this.config)
}

setEvents() {
setEvents(this.config)
}

remove() {
removeButton(this.config)
}
Expand Down

0 comments on commit b312723

Please sign in to comment.