semantic-ui-calendar-react
Version:
date/time picker built from semantic-ui elements
139 lines (110 loc) • 4.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _semanticUiReact = require("semantic-ui-react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _lodash = _interopRequireDefault(require("lodash"));
var _Cell = _interopRequireDefault(require("./Cell"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var cellStyleWidth3 = {
width: '33.333333%',
minWidth: '7em'
};
var cellStyleWidth4 = {
width: '25%'
};
var cellStyleWidth7 = {
width: '14.285714%'
};
function buildRows(data
/*array*/
, width
/*number*/
) {
var height = data.length / width;
var rows = [];
for (var i = 0; i < height; i++) {
rows.push(data.slice(i * width, i * width + width));
}
return rows;
}
function isActive(rowIndex, rowWidth, colIndex, active) {
if (_lodash.default.isNil(active)) return false;
if (_lodash.default.isArray(active)) {
for (var i = 0; i < active.length; i++) {
if (rowIndex * rowWidth + colIndex === active[i]) return true;
}
}
return rowIndex * rowWidth + colIndex === active;
}
function isHovered(rowIndex, rowWidth, colIndex, hovered) {
if (_lodash.default.isNil(hovered)) return false;
return rowIndex * rowWidth + colIndex === hovered;
}
function isDisabled(rowIndex, rowWidth, colIndex, disabledIndexes) {
if (_lodash.default.isNil(disabledIndexes) || disabledIndexes.length === 0) return false;
for (var i = 0; i < disabledIndexes.length; i++) {
if (rowIndex * rowWidth + colIndex === disabledIndexes[i]) return true;
}
return false;
}
function getCellStyle(width) {
if (width === '3') {
return cellStyleWidth3;
}
if (width === '4') {
return cellStyleWidth4;
}
if (width === '7') {
return cellStyleWidth7;
}
return;
}
function Body(props) {
var data = props.data,
width = props.width,
onCellClick = props.onCellClick,
active = props.active,
disabled = props.disabled,
hovered = props.hovered,
onCellHover = props.onCellHover;
var content = buildRows(data, parseInt(width)).map(function (row, rowIndex) {
return _react.default.createElement(_semanticUiReact.Table.Row, {
key: "".concat(rowIndex).concat(row[0])
}, row.map(function (item, itemIndex) {
return _react.default.createElement(_Cell.default, {
style: getCellStyle(width),
active: isActive(rowIndex, parseInt(width), itemIndex, active),
hovered: isHovered(rowIndex, parseInt(width), itemIndex, hovered),
disabled: isDisabled(rowIndex, parseInt(width), itemIndex, disabled),
key: "".concat(rowIndex * width + itemIndex),
itemPosition: rowIndex * width + itemIndex,
content: item,
onHover: onCellHover,
onClick: onCellClick
});
}));
});
return _react.default.createElement(_semanticUiReact.Table.Body, null, content);
}
Body.handledProps = ["active", "data", "disabled", "hovered", "onCellClick", "onCellHover", "width"];
Body.propTypes = {
/** A number of columns in a row. */
width: _propTypes.default.oneOf(['3', '4', '7']).isRequired,
/** Data that is used to fill a calendar. */
data: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.string), _propTypes.default.arrayOf(_propTypes.default.number)]).isRequired,
/** Called after a click on calendar's cell. */
onCellClick: _propTypes.default.func,
onCellHover: _propTypes.default.func,
/** Index of an element in `data` array that should be displayed as hovered. */
hovered: _propTypes.default.number,
/** Index of an element (or array of indexes) in `data` array that should be displayed as active. */
active: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.arrayOf(_propTypes.default.number)]),
/** Array of element indexes in `data` array that should be displayed as disabled. */
disabled: _propTypes.default.arrayOf(_propTypes.default.number)
};
var _default = Body;
exports.default = _default;