Skip to content

Commit d681464

Browse files
committed
support array of objects in style binding (close #1242)
1 parent cfd1221 commit d681464

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/directives/style.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ module.exports = {
1414
if (this.arg) {
1515
this.setProp(this.arg, value)
1616
} else {
17-
if (typeof value === 'object') {
18-
this.objectHandler(value)
19-
} else {
17+
if (typeof value === 'string') {
2018
this.el.style.cssText = value
19+
} else if (_.isArray(value)) {
20+
this.objectHandler(value.reduce(_.extend, {}))
21+
} else {
22+
this.objectHandler(value)
2123
}
2224
}
2325
},
@@ -59,7 +61,6 @@ module.exports = {
5961
this.el.style.removeProperty(prop)
6062
}
6163
}
62-
6364
}
6465

6566
/**

test/unit/specs/directives/style_spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ if (_.inBrowser) {
8383
expect(el.style.getPropertyValue('padding')).toBeFalsy()
8484
})
8585

86+
it('update with array of objects', function () {
87+
el.style.padding = '10px'
88+
dir.update([{color: 'red'}, {marginRight: '30px'}])
89+
expect(el.style.getPropertyValue('color')).toBe('red')
90+
expect(el.style.getPropertyValue('margin-right')).toBe('30px')
91+
expect(el.style.getPropertyValue('padding')).toBe('10px')
92+
dir.update([{color: 'blue'}, {padding: null}])
93+
expect(el.style.getPropertyValue('color')).toBe('blue')
94+
expect(el.style.getPropertyValue('margin-right')).toBeFalsy()
95+
expect(el.style.getPropertyValue('padding')).toBeFalsy()
96+
})
97+
8698
it('update with object and auto prefix', function () {
8799
var prop = checkPrefixedProp('transform')
88100
var val = 'scale(0.5)'

0 commit comments

Comments
 (0)