Skip to content

Commit

Permalink
feat: double binds upgraded props - setting a corresponding attribute…
Browse files Browse the repository at this point in the history
… will upgrade its upgraded prop
  • Loading branch information
geotrev committed Jan 31, 2022
1 parent 1ce1244 commit f415994
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const External = {
}

export const Internal = {
// Primitives
// Properties
rotomId: Symbol("#rotomId"),
vnode: Symbol("#vnode"),
isFirstRender: Symbol("#isFirstRender"),
Expand Down
5 changes: 4 additions & 1 deletion src/properties/upgrade-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ export const upgradeProperty = (
if (reflected) {
const attribute = camelToKebab(propName)
const attrValue = String(value)
RotomInstance.setAttribute(attribute, attrValue)

if (RotomInstance.getAttribute(attribute) !== attrValue) {
RotomInstance.setAttribute(attribute, attrValue)
}
}
} else {
delete RotomInstance[privateName]
Expand Down
7 changes: 7 additions & 0 deletions src/rotom-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
isFunction,
camelToKebab,
createUUID,
kebabToCamel,
isUndefined,
} from "./utilities"

const SHADOW_ROOT_MODE = "open"
Expand Down Expand Up @@ -60,6 +62,11 @@ export function rotomFactory(renderer) {
oldValue,
newValue
)

const propName = kebabToCamel(name)
if (!isUndefined(this[propName]) && this[propName] !== newValue) {
this[propName] = newValue
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utilities/transform-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const kebabToCamel = (value) =>
value
.split("-")
.map((word, i) =>
i
i > 0
? word[0].toUpperCase() + word.slice(1).toLowerCase()
: word.toLowerCase()
)
Expand Down
26 changes: 20 additions & 6 deletions test/jsx/fixtures/nested-element-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class NestedElementTest extends RotomElement {
default: "lightgray",
type: "string",
},
name: {
default: "Chaos",
type: "string",
},
}
}

Expand All @@ -30,16 +34,26 @@ class NestedElementTest extends RotomElement {
this.handleClick = this.handleClick.bind(this)
}

getRandom(current, items) {
const newValue = items[Math.floor(Math.random() * items.length)]
if (newValue === current) return this.getRandom(current, items)
return newValue
}

handleClick() {
this.borderColor = ["gray", "blue", "purple", "lime", "orange"][
Math.floor(Math.random() * 5)
]
this.shadowRoot.querySelector("kitchen-sink-test").firstName = [
this.borderColor = this.getRandom(this.borderColor, [
"gray",
"blue",
"purple",
"lime",
"orange",
])
this.name = this.getRandom(this.name, [
"Mario",
"Samus",
"Luigi",
"C Falcon",
][Math.floor(Math.random() * 4)]
])
}

render() {
Expand All @@ -55,7 +69,7 @@ class NestedElementTest extends RotomElement {
borderColor: this.borderColor,
}}
>
<kitchen-sink-test first-name="Chaos" description="I'm nested!">
<kitchen-sink-test first-name={this.name} description="I'm nested!">
<slot></slot>
</kitchen-sink-test>
</div>
Expand Down
15 changes: 8 additions & 7 deletions test/template/fixtures/nested-element-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class NestedElementTest extends RotomElement {
default: "lightgray",
type: "string",
},
name: {
default: "Chaos",
type: "string",
},
}
}

Expand Down Expand Up @@ -42,12 +46,9 @@ class NestedElementTest extends RotomElement {
this.borderColor = ["gray", "blue", "purple", "lime", "orange"][
Math.floor(Math.random() * 5)
]
this.shadowRoot.querySelector("kitchen-sink-test").firstName = [
"Mario",
"Samus",
"Luigi",
"C Falcon",
][Math.floor(Math.random() * 4)]
this.name = ["Mario", "Samus", "Luigi", "C Falcon"][
Math.floor(Math.random() * 4)
]
}

render() {
Expand All @@ -56,7 +57,7 @@ class NestedElementTest extends RotomElement {
<p data-key="lede">This one is nested with inline styles.</p>
<button data-key="updater" id="clicker">Click to update</button>
<div data-key="nested" class="border" style="border-color: ${this.borderColor}">
<kitchen-sink-test first-name="Chaos" description="I'm nested!">
<kitchen-sink-test first-name="${this.name}" description="I'm nested!">
<slot></slot>
</kitchen-sink-test>
</div>
Expand Down

0 comments on commit f415994

Please sign in to comment.