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 / src / forms / __unused / RadioGroup / state.js
Size: Mime:
"use strict";

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

const forms_1 = require("../..");

const deps_1 = require("./deps");
/**
 * @example @ganesh @gp @todo serialization
 */
// const isSelected = input => input.isSelected
// toSerialized = () => {
//   const selectedInput = this.inputsList.find(isSelected)
//   const name = this.name || selectedInput.name
//   const { value, label, identity } = selectedInput
//   return {
//     [name]: value || label || identity,
//   }
// }


const IS_BROWSER = typeof window === 'object';
/**
 * @alias formhandler
 * ^ for some reason
 *
 * @extend this the same as FormState
 */

class RadioGroupState extends forms_1.FormState {
  constructor() {
    super(...arguments);
    /**
     * @default shouldNotLeaveThisAsDefault
     */

    this.name = 'RadioGroup';
    this.lastUpdate = Date.now();
  }
  /**
   * @deprecated this is stupid just add onChange
   *
   * @description this is intended ONLY to be update custom click handling
   *              AFTER the component is used (ASYNC)
   *              and AFTER the click handling logic is changed
   */


  updateFrom(list = undefined) {
    if (list === undefined) {
      console.warn('@@fixme!!! something has a typo on the .updateFrom...');
      return this;
    }

    const inputsList = list.map(deps_1.toRadioInput);
    this.inputsList = inputsList.map(forms_1.FormState.toInputState); // this has to be called after mapping, for now,
    // because the abstract radiogroupstate adds the onClick logic

    this.init();
    return this;
  }
  /**
   * @override
   * @description intended to be extended for hooking in to the change
   *              but NOT overriding it
   *
   * @listens onChange
   * @see this.handleInputChange
   */


  onInputChange(input) {} // @NO_OPERATION (extension only)

  /**
   * @todo should debounce, often this may be triggered multiple times
   * @todo - onChange could happen before or after this handling of change...
   *
   * @listens isSelected (value change)
   */
  // @lint @todo figure out how to split
  // eslint-disable-next-line


  handleInputChange(input, observableChange) {
    // console.log('RadioGroupState_HANDLING_INPUT_CHANGE')
    // differenceInMilliseconds
    const ms = Date.now() - this.lastUpdate;

    if (ms < 300) {
      this.lastUpdate = Date.now();
      console.log({
        ms,
        lastUpdate: this.lastUpdate
      });
      console.warn('optimized_debounce');
      return;
    }

    deps_1.unselectInputsList(input, this.inputsList);
    /**
     * @description forgot this lil ole line that selected it :-D
     */

    input.isSelected = true; // @note === this was for  make default which had another issue
    // input.isSelected = !input.isSelected
    // /**
    //  * @description this way we can use the logic  for more than radiogroups
    //  * (though it should be split out)
    //  */
    // if (input.type === 'radio') {
    //   input.isSelected = true
    // }

    this.onInputChange(input);
  }
  /**
   * @api https://mobx.js.org/refguide/observe.html
   */


  init() {
    if (IS_BROWSER === false) {
      return;
    }

    const handle = this.handleInputChange.bind(this);
    deps_1.subscribeRadioGroupInputsList(this.inputsList, handle);
  }

}

exports.RadioGroupState = RadioGroupState;
exports.default = RadioGroupState; //# sourceMappingURL=state.js.map