UNPKG

@commercelayer/react-components

Version:
2 lines 1.13 kB
"use client"; "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=GenericFieldComponent;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),Parent_1=tslib_1.__importDefault(require("./Parent")),react_1=require("react"),placeholderImages_1=require("../../utils/placeholderImages");function GenericFieldComponent(props){const{children,tagElement,attribute,context,resource,...p}=props,resourceContext=(0,react_1.useContext)(context);let attributeValue="";const keysContext=Object.keys(resourceContext).filter(key=>key===resource);if(keysContext.length===1){const[keyResource]=keysContext;keyResource&&attribute&&(attributeValue=resourceContext[keyResource][attribute])}const Tag=tagElement||"span";if(Tag==="img"&&!children){const src=attributeValue||placeholderImages_1.defaultImgUrl;return(0,jsx_runtime_1.jsx)("img",{alt:"",src,...p})}const parentProps={attributeValue,tagElement,...p};return children?(0,jsx_runtime_1.jsx)(Parent_1.default,{...parentProps,children}):(0,jsx_runtime_1.jsx)(Tag,{"data-testid":attributeValue,...p,children:attributeValue},attributeValue)}