UNPKG

@talend/react-bootstrap

Version:

Bootstrap 3 components built with React

151 lines (148 loc) 4.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _classnames = _interopRequireDefault(require("classnames")); var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _bootstrapUtils = require("./utils/bootstrapUtils"); var _StyleConfig = require("./utils/StyleConfig"); var _ValidComponentChildren = _interopRequireDefault(require("./utils/ValidComponentChildren")); 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 }; } const ROUND_PRECISION = 1000; /** * Validate that children, if any, are instances of `<ProgressBar>`. */ function onlyProgressBar(props, propName, componentName) { const children = props[propName]; if (!children) { return null; } let error = null; _react.default.Children.forEach(children, child => { if (error) { return; } /** * Compare types in a way that works with libraries that patch and proxy * components like react-hot-loader. * * see https://github.com/gaearon/react-hot-loader#checking-element-types */ const element = /*#__PURE__*/(0, _jsxRuntime.jsx)(ProgressBar, {}); if (child.type === element.type) return; const childIdentifier = /*#__PURE__*/_react.default.isValidElement(child) ? child.type.displayName || child.type.name || child.type : child; error = new Error(`Children of ${componentName} can contain only ProgressBar ` + `components. Found ${childIdentifier}.`); }); return error; } const propTypes = { min: _propTypes.default.number, now: _propTypes.default.number, max: _propTypes.default.number, label: _propTypes.default.node, srOnly: _propTypes.default.bool, striped: _propTypes.default.bool, active: _propTypes.default.bool, children: onlyProgressBar, /** * @private */ isChild: _propTypes.default.bool }; const defaultProps = { min: 0, max: 100, active: false, isChild: false, srOnly: false, striped: false }; function getPercentage(now, min, max) { const percentage = (now - min) / (max - min) * 100; return Math.round(percentage * ROUND_PRECISION) / ROUND_PRECISION; } class ProgressBar extends _react.default.Component { renderProgressBar({ min, now, max, label, srOnly, striped, active, className, style, ...props }) { const [bsProps, elementProps] = (0, _bootstrapUtils.splitBsProps)(props); const classes = { ...(0, _bootstrapUtils.getClassSet)(bsProps), active, [(0, _bootstrapUtils.prefix)(bsProps, 'striped')]: active || striped }; return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { ...elementProps, role: "progressbar", className: (0, _classnames.default)(className, classes), style: { width: `${getPercentage(now, min, max)}%`, ...style }, "aria-valuenow": now, "aria-valuemin": min, "aria-valuemax": max, children: srOnly ? /*#__PURE__*/(0, _jsxRuntime.jsx)("span", { className: "sr-only", children: label }) : label }); } render() { const { isChild, ...props } = this.props; if (isChild) { return this.renderProgressBar(props); } const { min, now, max, label, srOnly, striped, active, bsClass, bsStyle, className, children, ...wrapperProps } = props; return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { ...wrapperProps, className: (0, _classnames.default)(className, 'progress'), children: children ? _ValidComponentChildren.default.map(children, child => /*#__PURE__*/(0, _react.cloneElement)(child, { isChild: true })) : this.renderProgressBar({ min, now, max, label, srOnly, striped, active, bsClass, bsStyle }) }); } } ProgressBar.propTypes = propTypes; ProgressBar.defaultProps = defaultProps; var _default = exports.default = (0, _bootstrapUtils.bsClass)('progress-bar', (0, _bootstrapUtils.bsStyles)(Object.values(_StyleConfig.State), ProgressBar)); //# sourceMappingURL=ProgressBar.js.map