Skip to content

Commit 25b270a

Browse files
committed
add custom mapping
1 parent ba336e7 commit 25b270a

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,37 @@ const props = {
4444
return <Dropdown {...props} />;
4545
```
4646

47+
### Custom Mapping
48+
49+
You can also define a custom mapping:
50+
51+
```
52+
import React from 'react';
53+
54+
// Web Component Use Case
55+
// install via npm install road-dropdown
56+
import 'road-dropdown';
57+
58+
import useCustomElement from 'use-custom-element';
59+
60+
const Dropdown = props => {
61+
const [customElementProps, ref] = useCustomElement(props, {
62+
option: 'selected',
63+
onChange: 'click',
64+
});
65+
66+
console.log(customElementProps);
67+
68+
// label: "Label"
69+
// options: "{"option1":{"label":"Option 1"},"option2":{"label":"Option 2"}}"
70+
// selected: "option1"
71+
72+
// and "onChange" mapped to "click" event for the event listener
73+
74+
return <my-dropdown {...customElementProps} ref={ref} />;
75+
};
76+
```
77+
4778
## Contribute
4879

4980
* `git clone [email protected]:the-road-to-learn-react/use-custom-element.git`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-custom-element",
3-
"version": "1.0.1",
3+
"version": "1.0.3",
44
"description": "",
55
"main": "lib/index.js",
66
"scripts": {

src/index.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import React from 'react';
22

3-
const useCustomElement = props => {
3+
const useCustomElement = (props, customMapping = {}) => {
44
const ref = React.createRef();
55

66
React.useLayoutEffect(() => {
77
const fns = Object.keys(props)
88
.filter(key => props[key] instanceof Function)
99
.map(key => ({
10-
key,
11-
fn: customEvent => props[key](customEvent.detail),
10+
key: customMapping[key] || key,
11+
fn: customEvent =>
12+
props[key](customEvent.detail, customEvent),
1213
}));
1314

1415
fns.forEach(({ key, fn }) =>
@@ -21,19 +22,19 @@ const useCustomElement = props => {
2122
);
2223
}, []);
2324

24-
const customElementProps = Object.keys(props).reduce((acc, key) => {
25-
const prop = props[key];
25+
const customElementProps = Object.keys(props)
26+
.filter(key => !(props[key] instanceof Function))
27+
.reduce((acc, key) => {
28+
const prop = props[key];
2629

27-
if (prop instanceof Object || prop instanceof Array) {
28-
return { ...acc, [key]: JSON.stringify(prop) };
29-
}
30+
const computedKey = customMapping[key] || key;
3031

31-
if (prop instanceof Function) {
32-
return acc;
33-
}
32+
if (prop instanceof Object || prop instanceof Array) {
33+
return { ...acc, [computedKey]: JSON.stringify(prop) };
34+
}
3435

35-
return { ...acc, [key]: prop };
36-
}, {});
36+
return { ...acc, [computedKey]: prop };
37+
}, {});
3738

3839
return [customElementProps, ref];
3940
};

0 commit comments

Comments
 (0)