Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
fix: sliders to allow zero values (#493)
Browse files Browse the repository at this point in the history
using ES2020's nullish coalescing operator `??`
it's just like `||`, but it allows `0` (and `NaN`)
  • Loading branch information
akloeckner authored Oct 21, 2020
1 parent 9f55a3d commit 232204e
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions scripts/controllers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,10 @@ App.controller('Main', function ($scope, $timeout, $location, Api) {
return page.header;
};

function toSafeNumber (value) {
return !isNaN(+value) ? +value : null;
}

function initSliderConf (item, entity, config, defaults) {
const key = '_c_slider_' + (config.field || defaults.field);

Expand All @@ -840,8 +844,8 @@ App.controller('Main', function ($scope, $timeout, $location, Api) {

const attrs = entity.attributes || {};
const sliderConf = {
min: config.min || attrs.min || defaults.min,
max: config.max || attrs.max || defaults.max,
min: config.min ?? attrs.min ?? defaults.min,
max: config.max ?? attrs.max ?? defaults.max,
step: config.step || attrs.step || defaults.step,
request: config.request || defaults.request,
curValue: undefined, // current value received from HA
Expand All @@ -855,9 +859,9 @@ App.controller('Main', function ($scope, $timeout, $location, Api) {
sliderConf.value = newValue;
} else {
const { attributes, state } = entity;
sliderConf.curValue = +attributes[config.field] || +attributes[defaults.field] || +state
|| config.value || defaults.value
|| config.min || defaults.min || attributes.min || 0;
sliderConf.curValue = toSafeNumber(+attributes[config.field]) ?? toSafeNumber(+attributes[defaults.field]) ?? toSafeNumber(+state)
?? config.value ?? defaults.value
?? config.min ?? attributes.min ?? defaults.min ?? 0;
if (sliderConf.oldValue !== sliderConf.curValue) {
sliderConf.oldValue = sliderConf.curValue;
sliderConf.value = sliderConf.curValue;
Expand Down

0 comments on commit 232204e

Please sign in to comment.