backpack-ui
Version:
Lonely Planet's Components
262 lines (211 loc) • 7.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
var _radium = require("radium");
var _radium2 = _interopRequireDefault(_radium);
var _kebabCase = require("lodash/kebabCase");
var _kebabCase2 = _interopRequireDefault(_kebabCase);
var _settings = require("../../../settings.json");
var _color = require("../../utils/color");
var _icon = require("../icon");
var _icon2 = _interopRequireDefault(_icon);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _ = { kebabCase: _kebabCase2.default };
var baseFontSize = 13;
var styles = {
container: {
base: {
display: "inline-block",
fontSize: baseFontSize + "px"
},
size: {
full: {
width: "100%"
},
half: {
width: "50%"
},
third: {
width: "33%"
}
}
},
button: {
base: {
backgroundColor: _settings.color.white,
border: 0,
color: _settings.color.darkGray,
cursor: "pointer",
display: "inline-block",
fontSize: "1em",
lineHeight: 16 / baseFontSize + "em",
paddingLeft: 28 / baseFontSize + "em",
paddingRight: 12 / baseFontSize + "em",
paddingTop: 3 / baseFontSize + "em",
position: "relative",
textAlign: "left",
verticalAlign: "text-top"
}
},
span: {
base: {
borderColor: _settings.color.gray,
borderStyle: "solid",
borderWidth: "1px",
color: _settings.color.white,
display: "block",
fontSize: "8px",
height: 16 / 8 + "em",
left: 0,
padding: 3 / 8 + "em " + 2 / 8 + "em " + 1 / 8 + "em",
position: "absolute",
textAlign: "center",
top: 0,
transition: "background-color " + _settings.timing.fast + ",\n border-color " + _settings.timing.fast,
userSelect: "none",
width: 16 / 8 + "em",
zIndex: _settings.zIndex.default
},
checked: {
backgroundColor: _settings.color.blue,
borderColor: _settings.color.blue
}
},
checkbox: {
hide: {
backgroundColor: _settings.color.white,
border: 0,
height: 16 / baseFontSize + "em",
left: 0,
outlineColor: (0, _color.darken)(_settings.color.gray, 7),
position: "absolute",
top: 0,
WebkitAppearance: "none",
width: 16 / baseFontSize + "em"
}
}
};
var Checkbox = function (_React$Component) {
_inherits(Checkbox, _React$Component);
function Checkbox(props) {
_classCallCheck(this, Checkbox);
var _this = _possibleConstructorReturn(this, (Checkbox.__proto__ || Object.getPrototypeOf(Checkbox)).call(this, props));
_this.state = {
checked: props.checked
};
_this.onClick = _this.onClick.bind(_this);
return _this;
}
_createClass(Checkbox, [{
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(_ref) {
var checked = _ref.checked;
this.setState({
checked: checked
});
}
}, {
key: "onClick",
value: function onClick(event, value, name) {
this.setState({
checked: !this.state.checked
});
if (this.props.onClick) {
this.props.onClick({ value: value, name: name, checked: !this.state.checked });
}
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _props = this.props;
var id = _props.id;
var value = _props.value;
var size = _props.size;
var name = _props.name;
var style = _props.style;
/* eslint-disable */
return _react2.default.createElement(
"span",
{
className: "Checkbox",
id: _.kebabCase(id),
ref: _.kebabCase(id),
style: [styles.container.base, styles.container.size[size], style]
},
_react2.default.createElement(
"label",
{ style: styles.button.base },
_react2.default.createElement(
"span",
{
style: [styles.span.base, this.state.checked && styles.span.checked]
},
_react2.default.createElement(_icon2.default.Checkmark, {
style: this.state.checked ? { opacity: 1 } : { opacity: 0 }
})
),
name,
_react2.default.createElement("input", {
type: "checkbox",
style: styles.checkbox.hide,
value: value,
name: name,
onClick: function onClick(e) {
return _this2.onClick(e, value, name);
}
})
)
);
/* eslint-enable */
}
}]);
return Checkbox;
}(_react2.default.Component);
Checkbox.propTypes = {
/**
* ID for the checkbox
*/
id: _react2.default.PropTypes.string.isRequired,
/**
* Value of the checkbox
*/
value: _react2.default.PropTypes.string,
/**
* Name of the checkbox
*/
name: _react2.default.PropTypes.string,
/**
* Whether or not the checkbox is checked
*/
checked: _react2.default.PropTypes.bool,
/**
* Click handler for checkbox
*/
onClick: _react2.default.PropTypes.func,
/**
* Set the checkbox size
*/
size: _react2.default.PropTypes.oneOf(["", "full", "half", "third"]),
/**
* CSS styles to append to component's styles
*/
style: _react2.default.PropTypes.objectOf(_react2.default.PropTypes.string, _react2.default.PropTypes.number)
};
Checkbox.defaultProps = {
id: "",
value: "",
checked: false,
onClick: null,
size: "",
style: {}
};
Checkbox.styles = styles;
exports.default = (0, _radium2.default)(Checkbox);