all files / built/src/components/ Select.js

53.19% Statements 50/94
35.71% Branches 25/70
30.77% Functions 8/26
56.63% Lines 47/83
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                                                                                                                                                 
"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
        s = arguments[i];
        for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
            t[p] = s[p];
    }
    return t;
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    Iif (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) Eif (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var mobx_1 = require("mobx");
var mobx_react_1 = require("mobx-react");
var FormElementProps_1 = require("./FormElementProps");
var TypedSelect = TypedSelect_1 = (function (_super) {
    __extends(TypedSelect, _super);
    function TypedSelect() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    TypedSelect.defaultLabels = function (value) {
        return value + "";
    };
    TypedSelect.defaultKeys = function (value) {
        if (value === undefined) {
            return "undefined";
        }
        value = value.valueOf();
        return JSON.stringify(value);
    };
    TypedSelect.prototype.updateValue = function (ev) {
        var keys = this.props.keys || TypedSelect_1.defaultKeys;
        // Find a value in the list that coerces to the new value
        for (var _i = 0, _a = this.props.options; _i < _a.length; _i++) {
            var option = _a[_i];
            if (keys(option) === ev.currentTarget.value) {
                this.props.value.set(option);
                return;
            }
        }
    };
    TypedSelect.prototype.render = function () {
        var _a = this.props, _b = _a.selectComponent, SelectComponent = _b === void 0 ? "select" : _b, _c = _a.optionComponent, OptionComponent = _c === void 0 ? "option" : _c;
        var labels = this.props.labels || TypedSelect_1.defaultLabels;
        var keys = this.props.keys || TypedSelect_1.defaultKeys;
        var selectedValue = this.props.value.get();
        var selectedKey = keys(selectedValue);
        var options = this.props.options.map(function (option) { return ({
            key: keys(option),
            label: labels(option)
        }); });
        if (!options.some(function (option) { return option.key === selectedKey; })) {
            options = options.concat({
                key: selectedKey,
                label: labels(selectedValue)
            });
        }
        return (React.createElement(SelectComponent, __assign({}, FormElementProps_1.removeProps(this.props, "value", "options", "labels", "keys", "size"), { value: selectedKey, onChange: this.updateValue }), options.map(function (option) { return React.createElement(OptionComponent, { key: option.key, value: option.key }, option.label); })));
    };
    return TypedSelect;
}(React.Component));
__decorate([
    mobx_1.action.bound
], TypedSelect.prototype, "updateValue", null);
TypedSelect = TypedSelect_1 = __decorate([
    mobx_react_1.observer
], TypedSelect);
exports.TypedSelect = TypedSelect;
var Select = (function (_super) {
    __extends(Select, _super);
    function Select() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return Select;
}(TypedSelect));
exports.Select = Select;
var SelectString = (function (_super) {
    __extends(SelectString, _super);
    function SelectString() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return SelectString;
}(TypedSelect));
exports.SelectString = SelectString;
var SelectNumber = (function (_super) {
    __extends(SelectNumber, _super);
    function SelectNumber() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return SelectNumber;
}(TypedSelect));
exports.SelectNumber = SelectNumber;
function SelectUsing(selectComponent, optionComponent) {
    var S = (function (_super) {
        __extends(class_1, _super);
        function class_1() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        return class_1;
    }(TypedSelect));
    return function (props) { return (React.createElement(S, __assign({ selectComponent: selectComponent, optionComponent: optionComponent }, props))); };
}
exports.SelectUsing = SelectUsing;
var TypedSelect_1;
//# sourceMappingURL=Select.js.map