Ver Fonte

user added thresholds state

Peter Holmberg há 7 anos atrás
pai
commit
201bd69a6a

+ 13 - 19
public/app/plugins/panel/gauge/Threshold.test.tsx

@@ -35,12 +35,9 @@ describe('Add threshold', () => {
   });
   });
 
 
   it('should add threshold between min and added 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);
     instance.onAddThreshold(1);
 
 
@@ -70,7 +67,6 @@ describe('Add at index', () => {
         { index: 1, label: '', value: 50, canRemove: true },
         { index: 1, label: '', value: 50, canRemove: true },
         { index: 2, label: 'Max', value: 100, canRemove: false },
         { index: 2, label: 'Max', value: 100, canRemove: false },
       ],
       ],
-      userAddedThresholds: 1,
     };
     };
 
 
     const result = instance.insertAtIndex(1);
     const result = instance.insertAtIndex(1);
@@ -79,16 +75,16 @@ describe('Add at index', () => {
   });
   });
 
 
   it('should return 2, two added thresholds', () => {
   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);
     const result = instance.insertAtIndex(2);
 
 
@@ -103,7 +99,6 @@ describe('Add at index', () => {
         { index: 1, label: '', value: 50, canRemove: true },
         { index: 1, label: '', value: 50, canRemove: true },
         { index: 2, label: 'Max', value: 100, canRemove: false },
         { index: 2, label: 'Max', value: 100, canRemove: false },
       ],
       ],
-      userAddedThresholds: 1,
     };
     };
 
 
     const result = instance.insertAtIndex(2);
     const result = instance.insertAtIndex(2);
@@ -124,7 +119,6 @@ describe('change threshold value', () => {
 
 
     instance.state = {
     instance.state = {
       thresholds: mockThresholds,
       thresholds: mockThresholds,
-      userAddedThresholds: 1,
     };
     };
 
 
     const mockEvent = { target: { value: 78 } };
     const mockEvent = { target: { value: 78 } };

+ 11 - 15
public/app/plugins/panel/gauge/Thresholds.tsx

@@ -6,7 +6,6 @@ import { ColorPicker } from '../../../core/components/colorpicker/ColorPicker';
 
 
 interface State {
 interface State {
   thresholds: Threshold[];
   thresholds: Threshold[];
-  userAddedThresholds: number;
 }
 }
 
 
 export default class Thresholds extends PureComponent<PanelOptionsProps<OptionsProps>, State> {
 export default class Thresholds extends PureComponent<PanelOptionsProps<OptionsProps>, State> {
@@ -18,7 +17,6 @@ export default class Thresholds extends PureComponent<PanelOptionsProps<OptionsP
         { index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
         { index: 0, label: 'Min', value: 0, canRemove: false, color: 'rgba(50, 172, 45, 0.97)' },
         { index: 1, label: 'Max', value: 100, canRemove: false },
         { index: 1, label: 'Max', value: 100, canRemove: false },
       ],
       ],
-      userAddedThresholds: 0,
     };
     };
   }
   }
 
 
@@ -37,13 +35,12 @@ export default class Thresholds extends PureComponent<PanelOptionsProps<OptionsP
     const value = newThresholds[index].value - (newThresholds[index].value - newThresholds[index - 1].value) / 2;
     const value = newThresholds[index].value - (newThresholds[index].value - newThresholds[index - 1].value) / 2;
 
 
     this.setState(
     this.setState(
-      prevState => ({
+      {
         thresholds: this.sortThresholds([
         thresholds: this.sortThresholds([
           ...newThresholds,
           ...newThresholds,
           { index: index, label: '', value: value, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
           { index: index, label: '', value: value, canRemove: true, color: 'rgba(237, 129, 40, 0.89)' },
         ]),
         ]),
-        userAddedThresholds: prevState.userAddedThresholds + 1,
-      }),
+      },
       () => this.updateGauge()
       () => this.updateGauge()
     );
     );
   };
   };
@@ -51,7 +48,6 @@ export default class Thresholds extends PureComponent<PanelOptionsProps<OptionsP
   onRemoveThreshold = threshold => {
   onRemoveThreshold = threshold => {
     this.setState(prevState => ({
     this.setState(prevState => ({
       thresholds: prevState.thresholds.filter(t => t !== threshold),
       thresholds: prevState.thresholds.filter(t => t !== threshold),
-      userAddedThresholds: prevState.userAddedThresholds - 1,
     }));
     }));
   };
   };
 
 
@@ -208,9 +204,9 @@ export default class Thresholds extends PureComponent<PanelOptionsProps<OptionsP
   }
   }
 
 
   insertAtIndex(index) {
   insertAtIndex(index) {
-    const { userAddedThresholds } = this.state;
+    const { thresholds } = this.state;
 
 
-    if (userAddedThresholds === 0 || index < 0) {
+    if (thresholds.length < 3 || index < 0) {
       return 1;
       return 1;
     }
     }
 
 
@@ -218,10 +214,10 @@ export default class Thresholds extends PureComponent<PanelOptionsProps<OptionsP
   }
   }
 
 
   renderIndicatorSection(index) {
   renderIndicatorSection(index) {
-    const { userAddedThresholds } = this.state;
-    const indicators = userAddedThresholds + 1;
+    const { thresholds } = this.state;
+    const indicators = thresholds.length - 1;
 
 
-    if (index === 0 || index === this.state.thresholds.length) {
+    if (index === 0 || index === thresholds.length) {
       return (
       return (
         <div
         <div
           key={index}
           key={index}
@@ -268,9 +264,9 @@ export default class Thresholds extends PureComponent<PanelOptionsProps<OptionsP
   }
   }
 
 
   renderIndicator() {
   renderIndicator() {
-    const { userAddedThresholds } = this.state;
+    const { thresholds } = this.state;
 
 
-    const indicators = userAddedThresholds + 1;
+    const indicators = thresholds.length - 1;
 
 
     const sections = [];
     const sections = [];
 
 
@@ -282,7 +278,7 @@ export default class Thresholds extends PureComponent<PanelOptionsProps<OptionsP
   }
   }
 
 
   render() {
   render() {
-    const { userAddedThresholds } = this.state;
+    const { thresholds } = this.state;
 
 
     return (
     return (
       <div className="section gf-form-group">
       <div className="section gf-form-group">
@@ -290,7 +286,7 @@ export default class Thresholds extends PureComponent<PanelOptionsProps<OptionsP
         <div className="thresholds">
         <div className="thresholds">
           <div className="color-indicators">{this.renderIndicator()}</div>
           <div className="color-indicators">{this.renderIndicator()}</div>
           <div className="threshold-rows">
           <div className="threshold-rows">
-            {userAddedThresholds === 0 ? this.renderNoThresholds() : this.renderThresholds()}
+            {thresholds.length > 2 ? this.renderThresholds() : this.renderNoThresholds()}
           </div>
           </div>
         </div>
         </div>
       </div>
       </div>