Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/ui / dist / inputs / Incrementer / IncrementerState.js
Size: Mime:
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

const tslib_1 = require("tslib");

const mobx_1 = require("xmobx/mobx");

const exotic_1 = require("exotic");

class IncrementerState {
  constructor() {
    this.count = 1;
    this.shouldIncrement = true;
    this.shouldDecrement = false;
    this.step = 1;
    this.maxValue = 99;
    this.minValue = 1;
  } // === actions ===


  update(values) {
    Object.keys(values).forEach(key => {
      if (this[key] !== undefined) {
        this[key] = values[key]; // handles state on mounting phase

        if (key === 'count') {
          if (values[key] > this.minValue && values[key] < this.maxValue) {
            this.shouldDecrement = true;
          } else if (values[key] === this.maxValue) {
            this.shouldIncrement = false;
            this.shouldDecrement = true;
          }
        }
      }
    });
  }

  incrementCount(props) {
    const {
      event,
      onValueChange
    } = props;

    if (this.count < this.maxValue) {
      this.count += this.step;
      this.shouldDecrement = true;

      if (exotic_1.isFunction(onValueChange)) {
        onValueChange(this);
      }
    }

    if (this.count === this.maxValue) {
      this.shouldIncrement = false;
      event.preventDefault();
    }
  }

  decrementCount(props) {
    const {
      event,
      onValueChange
    } = props;

    if (this.count > this.minValue) {
      this.count -= this.step;
      this.shouldIncrement = true;

      if (exotic_1.isFunction(onValueChange)) {
        onValueChange(this);
      }
    }

    if (this.count === this.minValue) {
      this.shouldDecrement = false;
      event.preventDefault();
    }
  } // === handlers ===


  handleChange(props) {
    const {
      event,
      onValueChange
    } = props;
    event.preventDefault();
    const isValueEmpty = event.target.value === '';
    const value = exotic_1.toNumber(event.target.value);

    if (isValueEmpty || value >= this.minValue && value <= this.maxValue) {
      this.count = value;
    } else if (value === 0) {
      this.count = 1;
    }

    this.shouldDecrement = !(this.count <= this.minValue);
    this.shouldIncrement = !(this.count >= this.maxValue);

    if (exotic_1.isFunction(onValueChange)) {
      onValueChange(this);
    }
  }

  handleBlur(event) {
    event.preventDefault();
    const isValueEmpty = event.target.value === '';
    const value = exotic_1.toNumber(event.target.value);

    if (isValueEmpty || value <= this.minValue) {
      this.count = this.minValue;
    } else if (value >= this.maxValue) {
      this.count = this.maxValue;
    }
  }

}

tslib_1.__decorate([mobx_1.observable], IncrementerState.prototype, "count", void 0);

tslib_1.__decorate([mobx_1.observable], IncrementerState.prototype, "shouldIncrement", void 0);

tslib_1.__decorate([mobx_1.observable], IncrementerState.prototype, "shouldDecrement", void 0);

tslib_1.__decorate([mobx_1.observable], IncrementerState.prototype, "step", void 0);

tslib_1.__decorate([mobx_1.observable], IncrementerState.prototype, "maxValue", void 0);

tslib_1.__decorate([mobx_1.observable], IncrementerState.prototype, "minValue", void 0);

tslib_1.__decorate([mobx_1.action], IncrementerState.prototype, "update", null);

tslib_1.__decorate([mobx_1.action.bound], IncrementerState.prototype, "incrementCount", null);

tslib_1.__decorate([mobx_1.action.bound], IncrementerState.prototype, "decrementCount", null);

tslib_1.__decorate([mobx_1.action.bound], IncrementerState.prototype, "handleChange", null);

tslib_1.__decorate([mobx_1.action.bound], IncrementerState.prototype, "handleBlur", null);

exports.IncrementerState = IncrementerState; //# sourceMappingURL=IncrementerState.js.map