UNPKG

react-query-builder-semantic

Version:
263 lines (235 loc) 9.63 kB
'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 _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); 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; } /** * Default element to represent a rule for a RuleGroup in the QueryBuilder */ var Rule = function (_React$Component) { _inherits(Rule, _React$Component); function Rule(props) { _classCallCheck(this, Rule); var _this = _possibleConstructorReturn(this, (Rule.__proto__ || Object.getPrototypeOf(Rule)).call(this, props)); _this.onFieldChanged = function (value) { _this.onElementChanged('field', value); }; _this.onOperatorChanged = function (value) { _this.onElementChanged('operator', value); }; _this.onValueChanged = function (value) { _this.onElementChanged('value', value); }; _this.onElementChanged = function (property, value) { var _this$props = _this.props, id = _this$props.id, onPropChange = _this$props.onPropChange; onPropChange(property, value, id); }; _this.removeRule = function (event) { event.preventDefault(); event.stopPropagation(); _this.props.onRuleRemove(_this.props.id, _this.props.parentId); }; return _this; } _createClass(Rule, [{ key: 'render', value: function render() { var _props = this.props, field = _props.field, operator = _props.operator, value = _props.value, translations = _props.translations, getOperators = _props.getOperators, classNames = _props.classNames, _props$schema = _props.schema, fields = _props$schema.fields, controls = _props$schema.controls; return _react2.default.createElement( 'div', { className: '' + classNames.ruleContainer }, _react2.default.createElement( 'div', { className: '' + classNames.rule }, _react2.default.createElement(controls.fieldSelector, { options: fields, title: translations.fields.title, value: field, className: '' + classNames.fields, handleOnChange: this.onFieldChanged }), _react2.default.createElement(controls.operatorSelector, { field: field, title: translations.operators.title, options: getOperators(field), value: operator, className: '' + classNames.operators, handleOnChange: this.onOperatorChanged }), _react2.default.createElement(controls.valueEditor, { field: field, title: translations.value.title, operator: operator, value: value, className: '' + classNames.value, handleOnChange: this.onValueChanged }), _react2.default.createElement( 'div', { className: '' + classNames.ruleHeader }, _react2.default.createElement(controls.removeRuleAction, { label: translations.removeRule.label, title: translations.removeRule.title, className: '' + classNames.removeRule, handleOnClick: this.removeRule }) ) ) ); } }]); return Rule; }(_react2.default.Component); Rule.propTypes = { /** * This is a callback function invoked to get the list of allowed operators for the given field */ getOperators: _propTypes2.default.func, /** * This is a callback function invoked to remove this rule from the RuleGroup */ onRuleRemove: _propTypes2.default.func, /** * This is a callback function invoked notify this rule has made a property change for operator,value,field */ onPropChange: _propTypes2.default.func, /** * This is a callback function invoked to return the current level for this rule */ getLevel: _propTypes2.default.func, /** * Random generated id for rule */ id: _propTypes2.default.any, /** * Id for the RuleGroup this rule is nested in */ parentId: _propTypes2.default.any, /** * Selected field for rule */ field: _propTypes2.default.any, /** * Selected operator for rule */ operator: _propTypes2.default.any, /** * Selected value for rule */ value: _propTypes2.default.any, /** * Current schema from QueryBuilder */ schema: _propTypes2.default.object, /** * This can be used to assign specific CSS classes to various controls that are created by the QueryBuilderSemantic */ classNames: _propTypes2.default.shape({ /** *Root <div> element */ queryBuilder: _propTypes2.default.string, /** *<div> containing the RuleGroup */ ruleGroup: _propTypes2.default.string, /** *<Dropdown> control for combinators */ combinators: _propTypes2.default.string, /** *<Button> to add a Rule */ addRule: _propTypes2.default.string, /** *<Button> to add a RuleGroup */ addGroup: _propTypes2.default.string, /** *<Button> to remove a RuleGroup */ removeGroup: _propTypes2.default.string, /** *<div> containing the Rule */ rule: _propTypes2.default.string, /** *<Dropdown> control for fields */ fields: _propTypes2.default.string, /** *<Dropdown> control for operators */ operators: _propTypes2.default.string, /** *<Input> for the field value */ value: _propTypes2.default.string, /** *<Button> to remove a Rule */ removeRule: _propTypes2.default.string }), /** * This can be used to override translatable texts created by the <QueryBuilderSemantic /> * https://react.semantic-ui.com/elements/icon/ */ translations: _propTypes2.default.shape({ fields: _propTypes2.default.shape({ title: _propTypes2.default.string }), operators: _propTypes2.default.shape({ title: _propTypes2.default.string }), value: _propTypes2.default.shape({ title: _propTypes2.default.string }), removeRule: _propTypes2.default.shape({ icon: _propTypes2.default.string, title: _propTypes2.default.string }), removeGroup: _propTypes2.default.shape({ icon: _propTypes2.default.string, title: _propTypes2.default.string }), addRule: _propTypes2.default.shape({ icon: _propTypes2.default.string, title: _propTypes2.default.string }), addGroup: _propTypes2.default.shape({ icon: _propTypes2.default.string, title: _propTypes2.default.string }), combinators: _propTypes2.default.shape({ title: _propTypes2.default.string }) }) }; Rule.defaultProps = { id: null, parentId: null, field: null, operator: null, value: null, schema: null }; exports.default = Rule;