From 201bd69a6a3ae72f0820f71e493c40e4df3a22f0 Mon Sep 17 00:00:00 2001 From: Peter Holmberg Date: Thu, 29 Nov 2018 15:04:49 +0100 Subject: [PATCH] user added thresholds state --- .../plugins/panel/gauge/Threshold.test.tsx | 32 ++++++++----------- public/app/plugins/panel/gauge/Thresholds.tsx | 26 +++++++-------- 2 files changed, 24 insertions(+), 34 deletions(-) diff --git a/public/app/plugins/panel/gauge/Threshold.test.tsx b/public/app/plugins/panel/gauge/Threshold.test.tsx index e74ed9ffcc2..65691315b25 100644 --- a/public/app/plugins/panel/gauge/Threshold.test.tsx +++ b/public/app/plugins/panel/gauge/Threshold.test.tsx @@ -35,12 +35,9 @@ describe('Add threshold', () => { }); it('should add threshold between min and added threshold', () => { - const instance = setup(); - - instance.state = { - thresholds: thresholds, - userAddedThresholds: 1, - }; + const instance = setup({ + options: { thresholds: thresholds }, + }); instance.onAddThreshold(1); @@ -70,7 +67,6 @@ describe('Add at index', () => { { index: 1, label: '', value: 50, canRemove: true }, { index: 2, label: 'Max', value: 100, canRemove: false }, ], - userAddedThresholds: 1, }; const result = instance.insertAtIndex(1); @@ -79,16 +75,16 @@ describe('Add at index', () => { }); it('should return 2, two added thresholds', () => { - const instance = setup(); - instance.state = { - thresholds: [ - { index: 0, label: 'Min', value: 0, canRemove: false }, - { index: 1, label: '', value: 25, canRemove: true }, - { index: 2, label: '', value: 50, canRemove: true }, - { index: 3, label: 'Max', value: 100, canRemove: false }, - ], - userAddedThresholds: 2, - }; + const instance = setup({ + options: { + thresholds: [ + { index: 0, label: 'Min', value: 0, canRemove: false }, + { index: 1, label: '', value: 25, canRemove: true }, + { index: 2, label: '', value: 50, canRemove: true }, + { index: 3, label: 'Max', value: 100, canRemove: false }, + ], + }, + }); const result = instance.insertAtIndex(2); @@ -103,7 +99,6 @@ describe('Add at index', () => { { index: 1, label: '', value: 50, canRemove: true }, { index: 2, label: 'Max', value: 100, canRemove: false }, ], - userAddedThresholds: 1, }; const result = instance.insertAtIndex(2); @@ -124,7 +119,6 @@ describe('change threshold value', () => { instance.state = { thresholds: mockThresholds, - userAddedThresholds: 1, }; const mockEvent = { target: { value: 78 } }; diff --git a/public/app/plugins/panel/gauge/Thresholds.tsx b/public/app/plugins/panel/gauge/Thresholds.tsx index 86ce30c8d21..fe929e1cb5f 100644 --- a/public/app/plugins/panel/gauge/Thresholds.tsx +++ b/public/app/plugins/panel/gauge/Thresholds.tsx @@ -6,7 +6,6 @@ import { ColorPicker } from '../../../core/components/colorpicker/ColorPicker'; interface State { thresholds: Threshold[]; - userAddedThresholds: number; } export default class Thresholds extends PureComponent, State> { @@ -18,7 +17,6 @@ export default class Thresholds extends PureComponent ({ + { thresholds: this.sortThresholds([ ...newThresholds, { index: index, label: '', value: value, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' }, ]), - userAddedThresholds: prevState.userAddedThresholds + 1, - }), + }, () => this.updateGauge() ); }; @@ -51,7 +48,6 @@ export default class Thresholds extends PureComponent { this.setState(prevState => ({ thresholds: prevState.thresholds.filter(t => t !== threshold), - userAddedThresholds: prevState.userAddedThresholds - 1, })); }; @@ -208,9 +204,9 @@ export default class Thresholds extends PureComponent @@ -290,7 +286,7 @@ export default class Thresholds extends PureComponent
{this.renderIndicator()}
- {userAddedThresholds === 0 ? this.renderNoThresholds() : this.renderThresholds()} + {thresholds.length > 2 ? this.renderThresholds() : this.renderNoThresholds()}