Skip to content
Snippets Groups Projects
Radio.js 3.55 KiB
Newer Older
glennvorhes's avatar
glennvorhes committed
"use strict";
Glenn Vorhes's avatar
Glenn Vorhes committed
/**
 * Created by glenn on 6/12/2017.
 */
Object.defineProperty(exports, "__esModule", { value: true });
Glenn Vorhes's avatar
Glenn Vorhes committed
const React = require("react");
Glenn Vorhes's avatar
Glenn Vorhes committed
require("jquery-ui");
Glenn Vorhes's avatar
Glenn Vorhes committed
const makeGuid_1 = require("../util/makeGuid");
class RadioItem extends React.Component {
    constructor(props, context) {
        super(props, context);
        this.guid = makeGuid_1.default();
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
Glenn Vorhes's avatar
Glenn Vorhes committed
    render() {
        let style = {};
Glenn Vorhes's avatar
Glenn Vorhes committed
        if (this.props.inline) {
            style = {
                display: 'inline-block',
                padding: '0 5px'
            };
        }
Glenn Vorhes's avatar
Glenn Vorhes committed
        let props = {
Glenn Vorhes's avatar
Glenn Vorhes committed
            id: this.guid,
            type: "radio",
            name: this.props.groupId,
            value: typeof this.props.index == 'undefined' ? this.props.text : this.props.index.toFixed(),
Glenn Vorhes's avatar
Glenn Vorhes committed
            onChange: (evt) => {
                this.props.change(evt.target.value);
Glenn Vorhes's avatar
Glenn Vorhes committed
                evt.target.checked = true;
            },
            checked: this.props.checked,
            defaultChecked: this.props.checked
        };
        if (this.props.connected) {
            delete props.defaultChecked;
        }
        else {
            delete props.checked;
        }
Glenn Vorhes's avatar
Glenn Vorhes committed
        return React.createElement("li", { style: style },
            React.createElement("input", Object.assign({}, props)),
            React.createElement("label", { htmlFor: this.guid }, this.props.text));
    }
}
class RadioBase extends React.Component {
    constructor(props, context) {
        super(props, context);
        this.inline = this.props.inline || false;
        this.groupId = this.props.title.toLowerCase().replace(/ /g, '');
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
Glenn Vorhes's avatar
Glenn Vorhes committed
    render() {
        let arr = [];
        for (let i = 0; i < this.props.items.length; i++) {
            let itemProps = {
Glenn Vorhes's avatar
Glenn Vorhes committed
                groupId: this.groupId,
                text: this.props.items[i],
                inline: this.props.inline,
Glenn Vorhes's avatar
Glenn Vorhes committed
                change: (s) => (this.props.callback(s)),
Glenn Vorhes's avatar
Glenn Vorhes committed
                key: this.props.items[i],
                connected: this.props.connected || false,
                checked: false,
                index: i
            };
            if (typeof this.props.selectedValueOrIndex == 'number') {
                itemProps.checked = i == this.props.selectedValueOrIndex;
            }
            else {
                itemProps.checked = this.props.items[i] == this.props.selectedValueOrIndex;
                delete itemProps.index;
            }
Glenn Vorhes's avatar
Glenn Vorhes committed
            arr.push(React.createElement(RadioItem, Object.assign({}, itemProps)));
Glenn Vorhes's avatar
Glenn Vorhes committed
        }
Glenn Vorhes's avatar
Glenn Vorhes committed
        let classes = ['radio-list'];
Glenn Vorhes's avatar
Glenn Vorhes committed
        if (this.props.classes) {
            classes = classes.concat(this.props.classes);
        }
Glenn Vorhes's avatar
Glenn Vorhes committed
        return React.createElement("div", { className: classes.join(' ') },
            React.createElement("h4", null, this.props.title),
            React.createElement("ul", { style: { listStyle: 'none' } }, arr));
    }
}
class Radio extends React.Component {
    render() {
        return React.createElement(RadioBase, { title: this.props.title, items: this.props.items, callback: this.props.callback, inline: this.props.inline, selectedValueOrIndex: this.props.defaultValue, connected: false, classes: this.props.classes });
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
Glenn Vorhes's avatar
Glenn Vorhes committed
exports.Radio = Radio;
Glenn Vorhes's avatar
Glenn Vorhes committed
class RadioConnected extends React.Component {
    render() {
        return React.createElement(RadioBase, { title: this.props.title, items: this.props.items, callback: this.props.callback, inline: this.props.inline, selectedValueOrIndex: this.props.selectedIndex, connected: true, classes: this.props.classes });
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
Glenn Vorhes's avatar
Glenn Vorhes committed
exports.RadioConnected = RadioConnected;
//# sourceMappingURL=Radio.js.map