react-usemap-render-component
Version:
An npm package for rendering ReactJS components in a cleaner and more beautiful way, allowing elements to be passed to the components from the hook’s props.
81 lines (73 loc) • 1.97 kB
JavaScript
;
var React = require('react');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var React__default = /*#__PURE__*/_interopDefault(React);
// src/index.tsx
// src/utils/parseTypeItem.ts
var parseTypeItem = ({ item, key }) => {
return {
object: {
newKey: key,
newItem: item
},
noObject: {
newKey: key,
newItem: item
}
};
};
var parseTypeItem_default = parseTypeItem;
// src/utils/handleKeyObject.ts
var handleKeyObject = ({ item, key, index }) => {
if (!item) return index;
if (typeof item !== "object") return index;
if (key === "index") {
return index;
}
if (key !== "default" && key in item) {
return `${item[key]}`;
}
if (key === "default" && "id" in item) {
const newItem = item;
return `${newItem.id}`;
}
return index;
};
var handleKeyObject_default = handleKeyObject;
// src/index.tsx
var useMap = ({ data, Component, config, ...props }) => {
const { key = "default" } = { ...config };
return data.map((item, index) => {
let relativeKey = index;
if (typeof item === "object") {
const { newKey: newKey2, newItem: newItem2 } = parseTypeItem_default({ item, key }).object;
relativeKey = handleKeyObject_default({ item: newItem2, key: newKey2, index });
return /* @__PURE__ */ React__default.default.createElement(
Component,
{
key: relativeKey,
index,
...item,
...props
}
);
}
const { newKey, newItem } = parseTypeItem_default({ item, key }).noObject;
if (newKey === "item") {
relativeKey = newItem;
}
return /* @__PURE__ */ React__default.default.createElement(
Component,
{
key: relativeKey,
index,
...props
},
item
);
});
};
var index_default = useMap;
module.exports = index_default;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map