UNPKG

baseui

Version:

A React Component library implementing the Base design language

98 lines (96 loc) 4.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var React = _interopRequireWildcard(require("react")); var _constants = require("./constants"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /* Copyright (c) Uber Technologies, Inc. This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree. */ // handles the case where selected = 0 // @ts-ignore function isSelectedDefined(selected) { return Array.isArray(selected) || typeof selected === 'number'; } function defaultStateReducer(type, nextState, // eslint-disable-next-line @typescript-eslint/no-unused-vars currentState) { return nextState; } class StatefulContainer extends React.Component { constructor(props) { super(props); _defineProperty(this, "changeState", nextState => { if (this.props.stateReducer) { this.setState(this.props.stateReducer(_constants.STATE_CHANGE_TYPE.change, nextState, this.state)); } else { this.setState(nextState); } }); _defineProperty(this, "onClick", (event, index) => { if (this.props.mode === _constants.MODE.radio) { if (this.state.selected.length === 0 || this.state.selected[0] !== index) { this.changeState({ selected: [index] }); } else { this.changeState({ selected: [] }); } } if (this.props.mode === _constants.MODE.checkbox) { if (!this.state.selected.includes(index)) { this.changeState({ selected: [...this.state.selected, index] }); } else { this.changeState({ selected: this.state.selected.filter(value => value !== index) }); } } if (this.props.onClick) { this.props.onClick(event, index); } }); const { initialState = {} } = props; const { selected = [] } = initialState; this.state = { // @ts-ignore selected: isSelectedDefined(selected) ? [].concat(selected) : [] }; } render() { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { initialState, stateReducer, ...props } = this.props; return this.props.children({ ...props, onClick: this.onClick, selected: this.state.selected }); } } exports.default = StatefulContainer; _defineProperty(StatefulContainer, "defaultProps", { // @ts-ignore initialState: { selected: [] }, stateReducer: defaultStateReducer });