UNPKG

wix-style-react

Version:
140 lines (120 loc) • 5.67 kB
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; }; }(); function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } 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; } import React from 'react'; import EditableSelector from '../../src/EditableSelector'; import { Container, Row, Col } from '../../src/Grid'; import Card from '../../src/Card'; var CardWithEditableSelector = function (_React$Component) { _inherits(CardWithEditableSelector, _React$Component); function CardWithEditableSelector(props) { _classCallCheck(this, CardWithEditableSelector); var _this = _possibleConstructorReturn(this, (CardWithEditableSelector.__proto__ || Object.getPrototypeOf(CardWithEditableSelector)).call(this, props)); _this.onOptionAdded = function (_ref) { var newTitle = _ref.newTitle; _this.setState({ options: [].concat(_toConsumableArray(_this.state.options), [{ title: newTitle }]) }); }; _this.onOptionEdit = function (_ref2) { var newTitle = _ref2.newTitle, index = _ref2.index; _this.setState({ options: _this.state.options.map(function (option, i) { return index === i ? { title: newTitle } : option; }) }); }; _this.onOptionToggle = function (index) { var selectedIndex = _this.state.selectedIndex; if (index === selectedIndex) { return; } var newOptions = _this.state.options.map(function (opt) { return Object.assign({}, opt); }); if (selectedIndex !== null) { newOptions[selectedIndex].isSelected = false; } newOptions[index].isSelected = true; _this.setState({ options: newOptions, selectedIndex: index }); }; _this.onOptionDelete = function (_ref3) { var index = _ref3.index; var newSelectedIndex = _this.state.selectedIndex; if (index === _this.state.selectedIndex) { newSelectedIndex = null; } else if (index < _this.state.selectedIndex) { newSelectedIndex = 1; } _this.setState({ options: _this.state.options.filter(function (option, i) { return index !== i; }), selectedIndex: newSelectedIndex }); }; _this.state = { options: [{ title: 'Pumpkin Seeds' }, { title: 'Sunflower Seeds' }], selectedIndex: null }; return _this; } _createClass(CardWithEditableSelector, [{ key: 'render', value: function render() { var _this2 = this; return React.createElement( 'div', { style: { backgroundColor: '#f0f4f7', padding: '20px' } }, React.createElement( Container, null, React.createElement( Row, null, React.createElement( Col, { span: 4 }, React.createElement( Card, null, React.createElement(Card.Header, { title: 'Editable Selector Inside Card' }), React.createElement( Card.Content, null, React.createElement(EditableSelector, { dataHook: 'story-editable-selector', onOptionAdded: function onOptionAdded(params) { return _this2.onOptionAdded(params); }, onOptionEdit: function onOptionEdit(params) { return _this2.onOptionEdit(params); }, onOptionDelete: function onOptionDelete(params) { return _this2.onOptionDelete(params); }, onOptionToggle: function onOptionToggle(params) { return _this2.onOptionToggle(params); }, toggleType: 'radio', title: 'Type of Seeds', options: this.state.options }), '\xA0' ) ) ) ) ) ); } }]); return CardWithEditableSelector; }(React.Component); export default CardWithEditableSelector;