react-super-responsive-table
Version:
react-super-responsive-table converts your table data to a user-friendly list in mobile view.
64 lines (63 loc) • 2.57 kB
JavaScript
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 __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;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import React, { useContext, useEffect } from 'react';
import { HeaderContext } from '../utils/tableContext';
import allowed from '../utils/allowed';
function TrInner(_a) {
var children = _a.children, inHeader = _a.inHeader, props = __rest(_a, ["children", "inHeader"]);
var context = useContext(HeaderContext);
if (!context) {
throw new Error('TrInner must be used as a child of the Table component');
}
var setHeaders = context.setHeaders, headers = context.headers;
useEffect(function () {
if (inHeader) {
var updatedHeaders_1 = __spreadArray([], headers, true);
React.Children.forEach(children, function (child, i) {
if (React.isValidElement(child)) {
updatedHeaders_1[i] = child.props.children;
}
});
setHeaders(updatedHeaders_1);
}
}, [inHeader, children]);
return (React.createElement("tr", __assign({ "data-testid": "tr" }, allowed(props)), children &&
React.Children.map(children, function (child, i) {
return React.isValidElement(child)
? React.cloneElement(child, {
key: i,
columnKey: i,
})
: null;
})));
}
export default TrInner;