@sitecore-feaas/clientside 0.1.52 → 0.1.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,12 +20,38 @@ const convertSchemaPropertiesToKebabCase = (schema: any) => {
20
20
  return { ...schema, properties: kebabSchemaProperties, required: kebabRequiredProperties }
21
21
  }
22
22
 
23
+ // TODO: parse object, number, array, boolean etc props
24
+
25
+ export const parseProps = (propName: string, propValue: string, properties: any) => {
26
+ switch (properties?.[propName]?.type) {
27
+ case 'string':
28
+ return propValue
29
+ case 'object':
30
+ return JSON.parse(propValue)
31
+ case 'array':
32
+ return JSON.parse(propValue)
33
+ case 'number':
34
+ return +propValue
35
+ case 'integer':
36
+ return +propValue
37
+ case 'boolean':
38
+ return propValue === 'true'
39
+ default:
40
+ return propValue
41
+ }
42
+ }
43
+
23
44
  export function FEAASExternalProps(element: FEAASBase = this) {
24
45
  const attributes = element.getAttributeNames()
46
+ const id = element.getAttribute('data-external-id')
47
+ const schema: any = FEAASExternal.registered[id]?.schema
25
48
  const props = attributes.reduce((prev, curr) => {
26
- if (!curr.startsWith('data-attribute') && !['class', 'id', 'contenteditable'].includes(curr))
27
- return { ...prev, [reverseKebabCase(curr)]: element.getAttribute(curr) }
28
- else return prev
49
+ if (!curr.startsWith('data-attribute') && !['class', 'id', 'contenteditable'].includes(curr)) {
50
+ const value = element.getAttribute(curr)
51
+ return { ...prev, [reverseKebabCase(curr)]: parseProps(curr, value, schema?.properties) }
52
+ } else {
53
+ return prev
54
+ }
29
55
  }, {})
30
56
  return props
31
57
  }
@@ -63,8 +89,8 @@ export default class FEAASExternal extends FEAASBase<FEAASExternalProps, ReactMo
63
89
  const components = Object.keys(FEAASExternal.registered).map((name) => ({
64
90
  ...FEAASExternal.registered[name],
65
91
  name,
66
- schema: convertSchemaPropertiesToKebabCase(FEAASExternal.registered[name].schema),
67
- component: FEAASExternal.registered[name].component.toString()
92
+ schema: convertSchemaPropertiesToKebabCase(FEAASExternal.registered[name].schema)
93
+ //component: FEAASExternal.registered[name].component.toString()
68
94
  }))
69
95
 
70
96
  // send components from iframe to parent window
@@ -79,7 +105,7 @@ export default class FEAASExternal extends FEAASBase<FEAASExternalProps, ReactMo
79
105
  }
80
106
  }
81
107
 
82
- static loadComponents() {
108
+ /*static loadComponents() {
83
109
  const components = JSON.parse(sessionStorage.getItem('feaas-external-components'))
84
110
  components.forEach((c: { name: string; schema: any; component: React.FC }) => {
85
111
  const name = c.name
@@ -87,7 +113,7 @@ export default class FEAASExternal extends FEAASBase<FEAASExternalProps, ReactMo
87
113
  const component = eval('(' + c.component + ')')
88
114
  FEAASExternal.registerComponent(component, schema, name)
89
115
  })
90
- }
116
+ }*/
91
117
 
92
118
  static Preview(props: Partial<FEAASComponentProps<any>> & { src: string }) {
93
119
  const src =
@@ -1,17 +1,27 @@
1
1
  import { beforeEach, describe, expect, it } from 'vitest'
2
2
  import React from 'react'
3
3
  import * as Clientside from '../src/ui/react.js'
4
- import FEAASExternal from "../src/ui/FEAASExternal.js";
4
+ import FEAASExternal from '../src/ui/FEAASExternal.js'
5
5
 
6
6
  describe('External', () => {
7
7
  function Hello(props: { firstName: string; telephone: string }) {
8
8
  return React.createElement('div', {}, `Hello my name is ${props.firstName} and my tel is ${props.telephone}`)
9
9
  }
10
+ const helloSchema = {
11
+ properties: {
12
+ firstName: {
13
+ type: 'string'
14
+ },
15
+ telephone: {
16
+ type: 'number'
17
+ }
18
+ }
19
+ }
10
20
  function Hello2(props: { firstName: string; address: string }) {
11
21
  return React.createElement('div', {}, `Hello my name is ${props.firstName} and my address is ${props.address}`)
12
22
  }
13
23
  it('can register and render component', async () => {
14
- Clientside.External.registerComponent(Hello, {}, 'MyTestComponent')
24
+ Clientside.External.registerComponent(Hello, helloSchema, 'MyTestComponent')
15
25
  document.body.innerHTML =
16
26
  '<feaas-external id="external-component" class="-component" data-attributes-keys="first-name,telephone" first-name="Chuck" telephone="45657890875643" data-attributes="{&quot;first-name&quot;:&quot;Chuck&quot;,&quot;telephone&quot;:&quot;45657890875643&quot;}" data-external-id="MyTestComponent" contenteditable="false"></feaas-external>'
17
27
  const component = document.getElementById('external-component') as FEAASExternal
@@ -46,4 +56,5 @@ describe('External', () => {
46
56
  await expect(component2.whenRendered).resolves.toEqual(component2)
47
57
  expect(component2.innerHTML).toEqual('<div>Hello my name is George and my address is Test street 12</div>')
48
58
  })
59
+ // TODO: add test for props parsing
49
60
  })