@teamsnap/teamsnap-ui
Version:
a CSS component library for TeamSnap
148 lines (147 loc) • 7.13 kB
JavaScript
;
/**
* @name PanelRowExpandable
*
* @description
* An expandable panel row is used to show/hide nested table data. See the teamsnap patterns
* library for more information https://teamsnap-ui-patterns.netlify.com/patterns/components/panel.html
*
* This component accepts an array of objects as `parentColumns` and `childColumns`. These columns will spread the
* object out on the <PanelCell /> by default, unless a `renderColumn` function is passed.
*
* @example
* <PanelRowExpandable
* parentColumns={[{ children: 'Homer Simpson', mods: 'u-size1of2' }, { children: 'Marge Simpson', mods: 'u-size1of2' }]}
* childColumns={[{ children: 'Bart Simpson', mods: u-size1of2' }, { children: 'Lisa Simpson', mods: 'u-size1of2' }]}
* renderColumn={ column => <div style={{ outline: '1px dashed red' }} { ...column }></div> } />
*
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var PropTypes = __importStar(require("prop-types"));
var PanelRow_1 = require("../PanelRow");
var PanelCell_1 = require("../PanelCell");
var Icon_1 = require("../Icon");
var helpers_1 = require("../../utils/helpers");
var PanelRowExpandable = /** @class */ (function (_super) {
__extends(PanelRowExpandable, _super);
function PanelRowExpandable() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
isExpanded: false
};
_this.handleRowClick = function (e) {
e.preventDefault();
_this.setState({
isExpanded: !_this.state.isExpanded
});
};
_this.renderChildLink = function (children) {
var isExpanded = _this.state.isExpanded;
var linkClasses = (0, helpers_1.getClassName)("Panel-expandableControl", isExpanded && "is-expanded");
return (React.createElement("a", { onClick: _this.handleRowClick, className: linkClasses },
React.createElement("span", { className: "Panel-expandableControlIcon" },
React.createElement(Icon_1.Icon, { name: "right" })),
children));
};
_this.renderColumns = function (columns, includeLink) {
if (includeLink === void 0) { includeLink = false; }
var renderColumn = _this.props.renderColumn;
return columns.map(function (column, index) {
var columnChildren = column.children, props = __rest(column, ["children"]);
var children = includeLink && index === 0
? _this.renderChildLink(columnChildren)
: columnChildren;
return renderColumn ? (renderColumn(__assign({ key: index, children: children }, props))) : (React.createElement(PanelCell_1.PanelCell, __assign({ key: index }, props), children));
});
};
_this.renderChildColumns = function (childColumns) {
var isExpanded = _this.state.isExpanded;
return (React.createElement("div", { className: (0, helpers_1.getClassName)("Panel-childRows", isExpanded && "is-expanded") },
React.createElement(PanelRow_1.PanelRow, { isWithCells: true }, _this.renderColumns(childColumns))));
};
return _this;
}
PanelRowExpandable.prototype.render = function () {
var _a = this.props, parentColumns = _a.parentColumns, childColumns = _a.childColumns, className = _a.className, mods = _a.mods, style = _a.style, otherProps = _a.otherProps;
var hasChildren = childColumns && childColumns.length > 0;
return (React.createElement("div", __assign({ className: (0, helpers_1.getClassName)(className, mods), style: style }, otherProps),
React.createElement(PanelRow_1.PanelRow, { isWithCells: true, isParent: true }, this.renderColumns(parentColumns, hasChildren)),
hasChildren && this.renderChildColumns(childColumns)));
};
PanelRowExpandable.propTypes = {
parentColumns: PropTypes.arrayOf(PropTypes.object).isRequired,
childColumns: PropTypes.arrayOf(PropTypes.object),
renderColumn: PropTypes.func,
className: PropTypes.string,
mods: PropTypes.string,
style: PropTypes.object,
otherProps: PropTypes.object
};
PanelRowExpandable.defaultProps = {
childColumns: null,
renderColumn: null,
className: "Panel-expandableRow",
mods: null,
style: {},
otherProps: {}
};
return PanelRowExpandable;
}(React.PureComponent));
exports.default = PanelRowExpandable;