@coligo/react-native-table
Version:
Lightweight package for creating flexible tables in React Native.
71 lines (70 loc) • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TableHeader = void 0;
var _reactNative = require("react-native");
var _jsxRuntime = require("react/jsx-runtime");
const TableHeader = ({
columns,
sortConfig,
cellPadding,
sortingIcons,
borderStyle,
headerBackground = 'white',
onSort
}) => {
const seenKeys = new Set();
columns.forEach(column => {
const identifier = column.id ?? String(column.key);
if (seenKeys.has(identifier)) {
throw new Error(`Duplicate column identifier detected for key "${String(column.key)}". Ensure each column has a unique id or key.`);
}
seenKeys.add(identifier);
});
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
style: [styles.row, {
backgroundColor: headerBackground
}],
children: columns.map((column, index) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
style: [{
...cellPadding
}, column.width ? {
width: column.width
} : {
flex: column.flex ?? 1
}, {
borderColor: borderStyle?.borderColor ?? 'black'
}, borderStyle?.showVertical && columns.length - 1 !== index ? {
borderRightWidth: borderStyle.borderWidth ?? 1
} : undefined, borderStyle?.showHorizontalHeader ? {
borderBottomWidth: borderStyle.borderWidth ?? 1
} : undefined],
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.TouchableOpacity, {
onPress: () => column.sortable && onSort(column.key),
style: [styles.row],
disabled: !column.sortable,
children: [column.header ? column.header(column.label) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
style: styles.text,
children: column.label
}), column.sortable && sortConfig.key === column.key ? sortingIcons ? sortConfig.direction === 'asc' ? sortingIcons.asc : sortingIcons.desc : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
style: [styles.text, styles.sorting],
children: sortConfig.direction === 'asc' ? '↑' : '↓'
}) : null]
})
}, column.id ?? String(column.key)))
});
};
exports.TableHeader = TableHeader;
const styles = _reactNative.StyleSheet.create({
row: {
flexDirection: 'row'
},
text: {
fontWeight: 'bold'
},
sorting: {
marginLeft: 3
}
});
//# sourceMappingURL=TableHeader.js.map