@talend/react-bootstrap
Version:
Bootstrap 3 components built with React
129 lines (128 loc) • 5.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _classnames = _interopRequireDefault(require("classnames"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _uncontrollable = require("uncontrollable");
var _bootstrapUtils = require("./utils/bootstrapUtils");
var _ValidComponentChildren = _interopRequireDefault(require("./utils/ValidComponentChildren"));
var _PropTypes = require("./utils/PropTypes");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : 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); }
const propTypes = {
accordion: _propTypes.default.bool,
/**
* When `accordion` is enabled, `activeKey` controls the which child `Panel` is expanded. `activeKey` should
* match a child Panel `eventKey` prop exactly.
*
* @controllable onSelect
*/
activeKey: _propTypes.default.any,
/**
* A callback fired when a child Panel collapse state changes. It's called with the next expanded `activeKey`
*
* @controllable activeKey
*/
onSelect: _propTypes.default.func,
/**
* An HTML role attribute
*/
role: _propTypes.default.string,
/**
* A function that takes an eventKey and type and returns a
* unique id for each Panel heading and Panel Collapse. The function _must_ be a pure function,
* meaning it should always return the _same_ id for the same set of inputs. The default
* value requires that an `id` to be set for the PanelGroup.
*
* The `type` argument will either be `"body"` or `"heading"`.
*
* @defaultValue (eventKey, type) => `${this.props.id}-${type}-${key}`
*/
generateChildId: _propTypes.default.func,
/**
* HTML id attribute, required if no `generateChildId` prop
* is specified.
*/
id: (0, _PropTypes.generatedId)('PanelGroup')
};
const defaultProps = {
accordion: false
};
const childContextTypes = {
$bs_panelGroup: _propTypes.default.shape({
getId: _propTypes.default.func,
headerRole: _propTypes.default.string,
panelRole: _propTypes.default.string,
activeKey: _propTypes.default.any,
onToggle: _propTypes.default.func
})
};
class PanelGroup extends _react.default.Component {
constructor(...args) {
super(...args);
_defineProperty(this, "handleSelect", (key, expanded, e) => {
if (expanded) {
this.props.onSelect(key, e);
} else if (this.props.activeKey === key) {
this.props.onSelect(null, e);
}
});
}
getChildContext() {
const {
activeKey,
accordion,
generateChildId,
id
} = this.props;
let getId = null;
if (accordion) {
getId = generateChildId || ((key, type) => id ? `${id}-${type}-${key}` : null);
}
return {
$bs_panelGroup: {
getId,
headerRole: 'tab',
panelRole: 'tabpanel',
...(accordion && {
activeKey,
onToggle: this.handleSelect
})
}
};
}
render() {
const {
accordion,
className,
children,
...props
} = this.props;
const [bsProps, elementProps] = (0, _bootstrapUtils.splitBsPropsAndOmit)(props, ['onSelect', 'activeKey']);
if (accordion) {
elementProps.role = elementProps.role || 'tablist';
}
const classes = (0, _bootstrapUtils.getClassSet)(bsProps);
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
...elementProps,
className: (0, _classnames.default)(className, classes),
children: _ValidComponentChildren.default.map(children, child => /*#__PURE__*/(0, _react.cloneElement)(child, {
bsStyle: child.props.bsStyle || bsProps.bsStyle
}))
});
}
}
PanelGroup.propTypes = propTypes;
PanelGroup.defaultProps = defaultProps;
PanelGroup.childContextTypes = childContextTypes;
var _default = exports.default = (0, _uncontrollable.uncontrollable)((0, _bootstrapUtils.bsClass)('panel-group', PanelGroup), {
activeKey: 'onSelect'
});
//# sourceMappingURL=PanelGroup.js.map