mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Merge new state on assignment for legacy widgets (#1476)
This commit is contained in:
parent
a68e6fde4a
commit
cb1622c418
@ -57,6 +57,31 @@ module.exports = function defineWidget(def, renderer) {
|
||||
|
||||
proto.constructor = def.constructor = Component;
|
||||
|
||||
Object.defineProperty(proto, "state", {
|
||||
get: function() {
|
||||
return this.___state;
|
||||
},
|
||||
set: function(newState) {
|
||||
newState = newState || {};
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
if ("MARKO_DEBUG") {
|
||||
if (
|
||||
Object.keys(newState)
|
||||
.sort()
|
||||
.join("") !==
|
||||
Object.keys((this.___state && this.___state.___raw) || {})
|
||||
.sort()
|
||||
.join("")
|
||||
)
|
||||
complain(
|
||||
"'widget.state = newState' has changed from merging the newState to replacing the old state."
|
||||
);
|
||||
}
|
||||
|
||||
this.setState(newState);
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperty(proto, "__document", {
|
||||
get: function() {
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
module.exports = require("marko-widgets").defineComponent({
|
||||
template: require("./template.marko"),
|
||||
getInitialState: function() {
|
||||
return {
|
||||
size: "normal",
|
||||
variant: "primary"
|
||||
};
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,8 @@
|
||||
<var classes = [
|
||||
data.size !== 'normal' && 'app-button-'+data.size,
|
||||
data.variant !== 'primary' && 'app-button-'+data.variant,
|
||||
]/>
|
||||
|
||||
<button class=classes w-bind>
|
||||
Hello
|
||||
</button>
|
||||
@ -0,0 +1,20 @@
|
||||
var expect = require("chai").expect;
|
||||
|
||||
module.exports = function(helpers) {
|
||||
var widget = helpers.mount(require.resolve("./"));
|
||||
expect(Object.keys(widget.state.toJSON())).has.length(2);
|
||||
expect(widget.state).has.property("size", "normal");
|
||||
expect(widget.state).has.property("variant", "primary");
|
||||
|
||||
widget.state = {};
|
||||
expect(Object.keys(widget.state.toJSON())).has.length(2);
|
||||
expect(widget.state).has.property("size", "normal");
|
||||
expect(widget.state).has.property("variant", "primary");
|
||||
|
||||
widget.state = { newProp: 1 };
|
||||
|
||||
expect(Object.keys(widget.state.toJSON())).has.length(3);
|
||||
expect(widget.state).has.property("size", "normal");
|
||||
expect(widget.state).has.property("variant", "primary");
|
||||
expect(widget.state).has.property("newProp", 1);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user