grommet
Version:
focus on the essential experience
394 lines (334 loc) • 10.7 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React, { Component } from 'react';
import { storiesOf } from '@storybook/react';
import { css } from 'styled-components';
import { Box, Button, Grommet, CheckBox, Text } from 'grommet';
import { grommet } from 'grommet/themes';
import { normalizeColor, deepMerge } from 'grommet/utils';
import { FormCheckmark } from "grommet-icons/es6/icons/FormCheckmark";
var SimpleCheckBox =
/*#__PURE__*/
function (_Component) {
_inheritsLoose(SimpleCheckBox, _Component);
function SimpleCheckBox(props) {
var _this;
_this = _Component.call(this, props) || this;
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onChange", function (event) {
return _this.setState({
checked: event.target.checked
});
});
_this.state = {
checked: !!props.checked
};
return _this;
}
var _proto = SimpleCheckBox.prototype;
_proto.render = function render() {
var checked = this.state.checked;
return React.createElement(Grommet, {
theme: grommet
}, React.createElement(Box, {
align: "center",
pad: "large"
}, React.createElement(CheckBox, _extends({}, this.props, {
label: "Choice",
checked: checked,
onChange: this.onChange
}))));
};
return SimpleCheckBox;
}(Component);
var customCheckBoxTheme = {
checkBox: {
border: {
color: {
light: 'neutral-1'
},
radius: '2px'
},
color: {
light: 'neutral-1'
},
check: {
extend: function extend(_ref) {
var theme = _ref.theme,
checked = _ref.checked;
return "\n " + (checked && "background-color: " + normalizeColor('neutral-1', theme) + ";") + "\n ";
}
},
hover: {
border: {
color: undefined
}
},
icon: {
size: '18px',
extend: 'stroke: white;'
},
icons: {
checked: FormCheckmark
},
gap: 'xsmall',
size: '18px',
extend: "\n color: #9C9C9C;\n "
}
};
var ThemedCheckBox =
/*#__PURE__*/
function (_Component2) {
_inheritsLoose(ThemedCheckBox, _Component2);
function ThemedCheckBox() {
var _this2;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this2 = _Component2.call.apply(_Component2, [this].concat(args)) || this;
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "state", {
checked: false
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "onChange", function (event) {
return _this2.setState({
checked: event.target.checked
});
});
return _this2;
}
var _proto2 = ThemedCheckBox.prototype;
_proto2.render = function render() {
var checked = this.state.checked;
return React.createElement(Grommet, {
theme: deepMerge(grommet, customCheckBoxTheme)
}, React.createElement(Box, {
align: "center",
pad: "large"
}, React.createElement(CheckBox, _extends({}, this.props, {
label: "Choice",
checked: checked,
onChange: this.onChange
}))));
};
return ThemedCheckBox;
}(Component);
var checkboxCheckStyle = css(["background-color:#2196f3;border-color:#2196f3;"]);
var customToggleTheme = {
global: {
colors: {
'toggle-bg': '#757575',
'toggle-knob': 'white'
}
},
checkBox: {
border: {
color: {
light: 'toggle-bg'
}
},
color: {
light: 'toggle-knob'
},
check: {
radius: '2px'
},
hover: {
border: {
color: undefined
}
},
toggle: {
background: 'toggle-bg',
color: {
light: 'toggle-knob'
},
size: '36px',
knob: {
extend: "\n top: -4px;\n box-shadow: 0px 0px 2px 0px rgba(0,0,0,0.12), 0px 2px 2px 0px rgba(0,0,0,0.24);\n "
},
extend: function extend(_ref2) {
var checked = _ref2.checked;
return "\n height: 14px;\n " + (checked && checkboxCheckStyle) + "\n ";
}
},
gap: 'xsmall',
size: '18px'
}
};
var ThemedToggle =
/*#__PURE__*/
function (_Component3) {
_inheritsLoose(ThemedToggle, _Component3);
function ThemedToggle() {
var _this3;
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
_this3 = _Component3.call.apply(_Component3, [this].concat(args)) || this;
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this3)), "state", {
checked: false
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this3)), "onChange", function (event) {
return _this3.setState({
checked: event.target.checked
});
});
return _this3;
}
var _proto3 = ThemedToggle.prototype;
_proto3.render = function render() {
var checked = this.state.checked;
return React.createElement(Grommet, {
theme: deepMerge(grommet, customToggleTheme)
}, React.createElement(Box, {
align: "center",
pad: "large"
}, React.createElement(CheckBox, _extends({}, this.props, {
label: "Choice",
checked: checked,
onChange: this.onChange,
toggle: true
}))));
};
return ThemedToggle;
}(Component);
var CheckBoxInsideButton =
/*#__PURE__*/
function (_Component4) {
_inheritsLoose(CheckBoxInsideButton, _Component4);
function CheckBoxInsideButton() {
var _this4;
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
_this4 = _Component4.call.apply(_Component4, [this].concat(args)) || this;
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this4)), "state", {
checked: false
});
return _this4;
}
var _proto4 = CheckBoxInsideButton.prototype;
_proto4.render = function render() {
var _this5 = this;
var checked = this.state.checked;
return React.createElement(Grommet, {
theme: grommet
}, React.createElement(Box, {
align: "center",
pad: "large"
}, React.createElement(Button, {
hoverIndicator: "background",
onClick: function onClick() {
_this5.setState({
checked: !checked
});
}
}, React.createElement(CheckBox, {
tabIndex: "-1",
checked: checked,
label: React.createElement(Text, null, "Hi"),
onChange: function onChange() {}
}))));
};
return CheckBoxInsideButton;
}(Component);
var IndeterminateCheckBox =
/*#__PURE__*/
function (_Component5) {
_inheritsLoose(IndeterminateCheckBox, _Component5);
function IndeterminateCheckBox() {
var _this6;
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
args[_key4] = arguments[_key4];
}
_this6 = _Component5.call.apply(_Component5, [this].concat(args)) || this;
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this6)), "state", {
checked: [],
checkboxes: ['fruits', 'vegetables', 'olive oil']
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this6)), "onCheckAll", function (event) {
var checkboxes = _this6.state.checkboxes;
if (event.target.checked) {
_this6.setState({
checked: checkboxes
});
} else {
_this6.setState({
checked: []
});
}
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this6)), "onCheck", function (event, value) {
var checked = _this6.state.checked;
if (event.target.checked) {
checked.push(value);
_this6.setState({
checked: checked
});
} else {
_this6.setState({
checked: checked.filter(function (item) {
return item !== value;
})
});
}
});
return _this6;
}
var _proto5 = IndeterminateCheckBox.prototype;
_proto5.render = function render() {
var _this7 = this;
var _this$state = this.state,
checked = _this$state.checked,
checkboxes = _this$state.checkboxes;
return React.createElement(Grommet, {
theme: grommet
}, React.createElement(Box, {
align: "center",
pad: "large"
}, React.createElement(Box, {
direction: "row",
gap: "medium"
}, React.createElement(CheckBox, {
checked: checked.length === 3,
indeterminate: checked.length > 0 && checked.length < 3,
label: "All",
onChange: this.onCheckAll
}), checkboxes.map(function (item) {
return React.createElement(CheckBox, {
key: item,
checked: checked.indexOf(item) !== -1,
label: item,
onChange: function onChange(e) {
return _this7.onCheck(e, item);
}
});
}))));
};
return IndeterminateCheckBox;
}(Component);
storiesOf('CheckBox', module).add('Simple', function () {
return React.createElement(SimpleCheckBox, null);
}).add('Toggle', function () {
return React.createElement(SimpleCheckBox, {
toggle: true
});
}).add('Disabled', function () {
return React.createElement(SimpleCheckBox, {
checked: true,
disabled: true
});
}).add('Reverse', function () {
return React.createElement(SimpleCheckBox, {
reverse: true
});
}).add('Themed CheckBox', function () {
return React.createElement(ThemedCheckBox, null);
}).add('Themed Toggle', function () {
return React.createElement(ThemedToggle, null);
}).add('Inside a Button', function () {
return React.createElement(CheckBoxInsideButton, null);
}).add('Interminate CheckBox', function () {
return React.createElement(IndeterminateCheckBox, null);
});