Skip to content

Commit aa646bb

Browse files
committed
refactor!: unified createElement signature
1 parent 42e05f2 commit aa646bb

File tree

5 files changed

+59
-12
lines changed

5 files changed

+59
-12
lines changed

README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Use with any framework
2020
import { formatElements } from 'string-format-jsx';
2121

2222
formatElements('Hello, <a>@u3u</a>', {
23-
createElement: (type, children, key) => yourFramework.createElement(type, { key }, children),
23+
createElement: (type, props, children) => yourFramework.createElement(type, props, children),
2424
});
2525
```
2626

@@ -97,6 +97,39 @@ const result = interpolation('Hello, {name} ({count})', {
9797
});
9898
```
9999

100+
## Examples
101+
102+
### Vue2
103+
104+
```js
105+
import { formatElements, interpolation } from 'string-format-jsx';
106+
107+
export const Trans = {
108+
functional: true,
109+
110+
name: 'Trans',
111+
112+
props: {
113+
tag: { default: 'span', type: String | Object | Function },
114+
text: String,
115+
values: Object,
116+
},
117+
118+
render: (h, { data, props, scopedSlots: slots }) => {
119+
const { tag, text, values } = props;
120+
121+
return h(
122+
tag,
123+
data,
124+
formatElements(interpolation(text, { values }), {
125+
components: slots,
126+
createElement: h,
127+
}),
128+
);
129+
},
130+
};
131+
```
132+
100133
## Thanks
101134

102135
Core extracted from [next-translate](https://github.com/aralroca/next-translate).

src/core.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
export interface FormatElementsOptions<T> {
2-
components?: Record<string, (children: T | string, key: number) => T>;
3-
createElement?(type: string, children: T | string, key: number): T;
2+
components?: Record<string, (children: (T | string)[] | string, key: number) => T>;
3+
createElement?(
4+
type: string,
5+
props: {
6+
key: number;
7+
},
8+
children: (T | string)[] | string,
9+
): T;
410
}
511

612
export const formatElements = <T>(text: string, options: FormatElementsOptions<T> = {}) => {
@@ -20,15 +26,15 @@ export const formatElements = <T>(text: string, options: FormatElementsOptions<T
2026

2127
for (const [key, [type, children, after]] of elements.entries()) {
2228
const render = components?.[type];
23-
const child = children && (formatElements<T>(children, options) as T | string);
29+
const child = children && formatElements<T>(children, options);
2430

2531
tree.push(
2632
render
2733
? render?.(child, key)
2834
: //
2935
createElement
30-
? createElement?.(type, child, key)
31-
: child,
36+
? createElement?.(type, { key }, child)
37+
: (child as string),
3238
);
3339

3440
if (after) tree.push(after);

src/react.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface FormatElementsOptions extends Options<ReactNode> {}
2020
*/
2121
export const formatElements = (text: string, options: FormatElementsOptions = {}) => {
2222
return format<ReactNode>(text, {
23-
createElement: (type, children, key) => createElement(type, { key }, children),
23+
createElement,
2424
...options,
2525
});
2626
};

test/__snapshots__/core.test.ts.snap

+12-4
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,30 @@ exports[`should match snapshots 1`] = `
99
"The number is ",
1010
{
1111
"children": "{count}",
12-
"key": 0,
12+
"props": {
13+
"key": 0,
14+
},
1315
"type": "b",
1416
},
1517
],
16-
"key": 0,
18+
"props": {
19+
"key": 0,
20+
},
1721
"type": "component",
1822
},
1923
{
2024
"children": undefined,
21-
"key": 1,
25+
"props": {
26+
"key": 1,
27+
},
2228
"type": "br",
2329
},
2430
"The number is ",
2531
{
2632
"children": "{count}",
27-
"key": 2,
33+
"props": {
34+
"key": 2,
35+
},
2836
"type": "b",
2937
},
3038
".",

test/core.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ it('should match snapshots', () => {
1111
The number is <b>{count}</b>.
1212
`,
1313
{
14-
createElement: (type, children, key) => ({ children, key, type }),
14+
createElement: (type, props, children) => ({ children, props, type }),
1515
},
1616
),
1717

0 commit comments

Comments
 (0)