From c0ba525674d4569213b54f95aace8ef2eb6c8a28 Mon Sep 17 00:00:00 2001 From: baizn <576375879@qq.com> Date: Thu, 11 Jun 2020 15:27:47 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E5=9C=A8setState=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=B8=AD=E9=99=90=E5=88=B6=E4=B8=8D=E5=AD=98=E5=9C=A8=E7=9A=84?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/graph/controller/item.ts | 3 +-- src/graph/graph.ts | 13 +++++++++++-- src/shape/shapeBase.ts | 14 +++++++++++++- tests/unit/combo-issue-spec.ts | 4 ++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/graph/controller/item.ts b/src/graph/controller/item.ts index 09b6272719e..fc8d2a33895 100644 --- a/src/graph/controller/item.ts +++ b/src/graph/controller/item.ts @@ -428,8 +428,7 @@ export default class ItemController { // 已经存在要设置的 state,或不存在 state 的样式为 undefined if (item.hasState(stateName) === value - || (isString(value) && item.hasState(stateName)) - || !item.getStateStyle(stateName)) { + || (isString(value) && item.hasState(stateName))) { return; } diff --git a/src/graph/graph.ts b/src/graph/graph.ts index cc536e28821..6e1223de228 100644 --- a/src/graph/graph.ts +++ b/src/graph/graph.ts @@ -1464,13 +1464,22 @@ export default class Graph extends EventEmitter implements IGraph { if (comboId === child.id && childItem && childItem.getType && childItem.getType() === 'combo') { // 更新具体的 Combo 之前先清除所有的已有状态,以免将 state 中的样式更新为 Combo 的样式 const states = [...childItem.getStates()] - each(states, state => this.setItemState(childItem, state, false)) + // || !item.getStateStyle(stateName) + each(states, state => { + if (childItem.getStateStyle(state)) { + this.setItemState(childItem, state, false) + } + }) // 更新具体的 Combo itemController.updateCombo(childItem, child.children); // 更新 Combo 后,还原已有的状态 - each(states, state => this.setItemState(childItem, state, true)); + each(states, state => { + if (childItem.getStateStyle(state)) { + this.setItemState(childItem, state, true) + } + }); if (comboId) comboId = child.parentId; } diff --git a/src/shape/shapeBase.ts b/src/shape/shapeBase.ts index b8d8d8fb275..20e43d3c8b1 100644 --- a/src/shape/shapeBase.ts +++ b/src/shape/shapeBase.ts @@ -287,10 +287,17 @@ export const shapeBase: ShapeOptions = { const stateName = isBoolean(value) ? name : `${name}:${value}`; const shapeStateStyle = this.getStateStyle(stateName, true, item); const itemStateStyle = item.getStateStyle(stateName); - + + // 不允许设置一个不存在的状态 + if (!itemStateStyle && !shapeStateStyle) { + return; + } + // 要设置或取消的状态的样式 // 当没有 state 状态时,默认使用 model.stateStyles 中的样式 const styles = mix({}, itemStateStyle || shapeStateStyle); + + const group = item.getContainer(); if (value) { @@ -410,11 +417,16 @@ export const shapeBase: ShapeOptions = { */ getStateStyle(name: string, value: string | boolean, item: Item): ShapeStyle { const model = item.getModel(); + const type = item.getType() if (value) { const modelStateStyle = model.stateStyles ? model.stateStyles[name] : this.options.stateStyles && this.options.stateStyles[name]; + + if (type === 'combo') { + return mix({}, modelStateStyle) + } return mix({}, model.style, modelStateStyle); } diff --git a/tests/unit/combo-issue-spec.ts b/tests/unit/combo-issue-spec.ts index 8399a5d59bb..69863dfb48b 100644 --- a/tests/unit/combo-issue-spec.ts +++ b/tests/unit/combo-issue-spec.ts @@ -48,6 +48,9 @@ describe('combo states', () => { comboStateStyles: { hover: { stroke: 'green' + }, + selected: { + stroke: 'red' } }, modes: { @@ -68,6 +71,7 @@ describe('combo states', () => { // combo 设置不存在的 state graph.on('combo:click', evt => { + debugger graph.setItemState(evt.item, 'notFound', true) })