Skip to content

Commit dfa50d2

Browse files
committed
fix: support adding classes via state attributes
1 parent 0e87c48 commit dfa50d2

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

src/index.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,20 @@ function _setAttributeValues(el, attrValues) {
8181
async function _setAttributeValue(element, attribute, value, isOverwrite) {
8282
// TODO: if (value !== undefined)???
8383
if (!element.getAttribute(attribute) || isOverwrite) {
84-
if (attribute == "value") {
85-
if (element.value == "" || (element.value && isOverwrite))
86-
element.value = value;
87-
else if (
88-
isOverwrite ||
89-
(element.hasAttribute("value") && !(await element.getValue()))
90-
)
91-
element.setValue(value);
92-
} else if (element.hasAttribute(attribute) && (value || value === "")) {
93-
element.setAttribute(attribute, value);
94-
} else if (attribute.startsWith("$")) {
84+
if (attribute == "value") {
85+
if (element.value == "" || (element.value && isOverwrite))
86+
element.value = value;
87+
else if (
88+
isOverwrite ||
89+
(element.hasAttribute("value") && !(await element.getValue()))
90+
)
91+
element.setValue(value);
92+
} else if (attribute === "class" && value) {
93+
const classes = String(value).split(/\s+/).filter(Boolean);
94+
if (classes.length) element.classList.add(...classes);
95+
} else if (element.hasAttribute(attribute) && (value || value === "")) {
96+
element.setAttribute(attribute, value);
97+
} else if (attribute.startsWith("$")) {
9598
// Handle using operators.js
9699
const prop = attribute.substring(1);
97100
if (element[prop]) {
@@ -272,4 +275,4 @@ Action.init({
272275

273276
init();
274277

275-
export default { initElements, initElement, stateAttributes };
278+
export default { initElements, initElement, stateAttributes };

0 commit comments

Comments
 (0)