Skip to content

Commit 5480e38

Browse files
authored
change props values from type constructors to strings (#102)
* initial commit * prettier fomatting * replace object and array with json * update core test
1 parent a2f0e2f commit 5480e38

File tree

5 files changed

+48
-35
lines changed

5 files changed

+48
-35
lines changed

docs/api.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ console.log(document.body.firstElementChild.innerHTML) // "<h1>Hello, Jane</h1>"
109109

110110
If `options.props` is an object, the keys are the camelCased React props and the values are any one of the following built in javascript types, or the string "ref":
111111

112-
`String | Number | Boolean | Function | Object | Array | "ref"`
112+
`"string" | "number" | "boolean" | "function" | "json" | "ref"`
113113

114-
### String | Number | Boolean | Object | Array props
114+
"json" can be an array or object. The string passed into the attribute must pass `JSON.parse()` requirements.
115+
116+
### "string" | "number" | "boolean" | "function" | "json" props
115117

116118
```js
117119
function AttrPropTypeCasting(props) {
@@ -123,13 +125,13 @@ customElements.define(
123125
"attr-prop-type-casting",
124126
reactToWebComponent(AttrPropTypeCasting, React, ReactDOM, {
125127
props: {
126-
stringProp: String,
127-
numProp: Number,
128-
floatProp: Number,
129-
trueProp: Boolean,
130-
falseProp: Boolean,
131-
arrayProp: Array,
132-
objProp: Object,
128+
stringProp: "string",
129+
numProp: "number",
130+
floatProp: "number",
131+
trueProp: "boolean",
132+
falseProp: "boolean",
133+
arrayProp: "json",
134+
objProp: "json",
133135
},
134136
}),
135137
)
@@ -177,7 +179,7 @@ function ThemeSelect({ handleClick }) {
177179

178180
const WebThemeSelect = reactToWebComponent(ThemeSelect, React, ReactDOM, {
179181
props: {
180-
handleClick: Function,
182+
handleClick: "function",
181183
},
182184
})
183185

packages/core/src/core.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ describe("core", () => {
110110
ButtonWithDifferentPropTypes,
111111
{
112112
props: {
113-
text: String,
114-
numProp: Number,
115-
boolProp: Boolean,
116-
arrProp: Array,
117-
objProp: Object,
118-
funcProp: Function,
113+
text: "string",
114+
numProp: "number",
115+
boolProp: "boolean",
116+
arrProp: "json",
117+
objProp: "json",
118+
funcProp: "function",
119119
},
120120
},
121121
{ mount, unmount, onUpdated },

packages/core/src/core.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { ComponentClass, FC, R2WCOptions, Renderer } from "./types"
1+
import type {
2+
ComponentClass,
3+
FC,
4+
PropOptionTypes,
5+
R2WCOptions,
6+
Renderer,
7+
} from "./types"
28

39
import React from "react"
410

@@ -261,12 +267,12 @@ function handleTypeCasting(
261267
this: HTMLElement,
262268
key: string,
263269
value: any,
264-
obj: Record<string, unknown>,
270+
obj: Record<string, PropOptionTypes>,
265271
) {
266272
let attributeToAdd = value
267273
switch (obj[key]) {
268274
case "ref":
269-
case Function:
275+
case "function":
270276
if (obj[key] === "ref") {
271277
attributeToAdd = React.createRef()
272278
Reflect.set(this, key, attributeToAdd)
@@ -282,19 +288,16 @@ function handleTypeCasting(
282288
attributeToAdd = attributeToAdd.bind(this)
283289
}
284290
break
285-
case Number:
291+
case "number":
286292
attributeToAdd = parseFloat(attributeToAdd)
287293
break
288-
case Boolean:
294+
case "boolean":
289295
attributeToAdd = /^[ty1-9]/i.test(attributeToAdd)
290296
break
291-
case Object:
297+
case "json":
292298
attributeToAdd = JSON.parse(attributeToAdd)
293299
break
294-
case Array:
295-
attributeToAdd = JSON.parse(attributeToAdd)
296-
break
297-
case String:
300+
case "string":
298301
default:
299302
break
300303
}

packages/core/src/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,17 @@ export interface CustomElementConstructor {
5151
new (...params: any[]): HTMLElement
5252
}
5353

54+
export type PropOptionTypes =
55+
| "string"
56+
| "number"
57+
| "boolean"
58+
| "json"
59+
| "function"
60+
| "ref"
61+
5462
export interface R2WCOptions {
5563
shadow?: "open" | "closed" | boolean
56-
props?: string[] | Record<string, unknown>
64+
props?: string[] | Record<string, PropOptionTypes>
5765
}
5866

5967
export interface Renderer<T> {

tests/react-to-webcomponent.test.jsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,13 @@ describe("react-to-webcomponent", () => {
324324
OptionsPropsTypeCasting,
325325
{
326326
props: {
327-
stringProp: String,
328-
numProp: Number,
329-
floatProp: Number,
330-
trueProp: Boolean,
331-
falseProp: Boolean,
332-
arrayProp: Array,
333-
objProp: Object,
327+
stringProp: "string",
328+
numProp: "number",
329+
floatProp: "number",
330+
trueProp: "boolean",
331+
falseProp: "boolean",
332+
arrayProp: "array",
333+
objProp: "object",
334334
},
335335
},
336336
)
@@ -403,7 +403,7 @@ describe("react-to-webcomponent", () => {
403403

404404
const WebThemeSelect = reactToWebComponent(ThemeSelect, {
405405
props: {
406-
handleClick: Function,
406+
handleClick: "function",
407407
},
408408
})
409409

0 commit comments

Comments
 (0)