Skip to content

Commit

Permalink
[CP-3148] Proof of concept: Manipulating child components using ref a…
Browse files Browse the repository at this point in the history
…nd content.
  • Loading branch information
dkarski committed Oct 1, 2024
1 parent 0aacfe0 commit de5d30d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,8 @@ const view: View = {
"contactMiddleNameText",
"contactNameSuffixText",
"contactPhoneNumberText",
"contactPhoneTypeText",
"contactEmailAddressText",
"contactEmailTypeText",
"contactNickNameText",
"contactCompanyText",
"contactDepartmentText",
"contactWorkTitleText",
Expand Down Expand Up @@ -506,33 +505,23 @@ const view: View = {
},
},
},
contactPhoneTypeText: {
component: "text-plain",
dataProvider: {
source: "entities-field",
entitiesType: "contacts",
fields: {
"data.text": "phoneNumbers[0].phoneType",
},
},
},
contactEmailAddressText: {
component: "text-plain",
dataProvider: {
source: "entities-field",
entitiesType: "contacts",
fields: {
"data.text": "emailAddresses.emailAddress",
"data.text": "emailAddresses[0].emailAddress",
},
},
},
contactEmailTypeText: {
contactNickNameText: {
component: "text-plain",
dataProvider: {
source: "entities-field",
entitiesType: "contacts",
fields: {
"data.text": "emailAddresses.emailType",
"data.text": "nickName",
},
},
},
Expand Down Expand Up @@ -572,7 +561,7 @@ const view: View = {
source: "entities-field",
entitiesType: "contacts",
fields: {
"data.text": "address[0].streetAddress",
"data.text": "address.streetAddress",
},
},
},
Expand All @@ -582,7 +571,7 @@ const view: View = {
source: "entities-field",
entitiesType: "contacts",
fields: {
"data.text": "address[0].city",
"data.text": "address.city",
},
},
},
Expand All @@ -592,7 +581,7 @@ const view: View = {
source: "entities-field",
entitiesType: "contacts",
fields: {
"data.text": "address[0].country",
"data.text": "address.country",
},
},
},
Expand Down
26 changes: 18 additions & 8 deletions libs/generic-view/ui/src/lib/interactive/list-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,34 @@
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import React, { useEffect } from "react"
import React, { useLayoutEffect, useRef } from "react"
import { APIFC } from "generic-view/utils"
import { ListRendererConfig, ListRendererData } from "generic-view/models"

export const ListRenderer: APIFC<ListRendererData, ListRendererConfig> = ({
children,
}) => {
useEffect(() => {
console.log("Props children:", children)
const containerRef = useRef<HTMLDivElement>(null)

React.Children.forEach(children, (child) => {
if (child && typeof child === "object" && "props" in child) {
console.log("Element props:", child.props)
useLayoutEffect(() => {
if (!containerRef.current) {
return
}

const childNodes = Array.from(containerRef.current.children) as HTMLElement[]
let foundNonEmpty = false

childNodes.forEach((childNode) => {
const textContent = childNode.textContent ?? ""

if (textContent.trim().length > 0 && !foundNonEmpty) {
childNode.style.display = "block"
foundNonEmpty = true
} else {
console.log("Non-React element:", child)
childNode.style.display = "none"
}
})
}, [children])

return <>{children}</>
return <div ref={containerRef}>{children}</div>
}

0 comments on commit de5d30d

Please sign in to comment.