Skip to content

Commit bc2b23e

Browse files
authored
Merge pull request #983 from 7770824/7770824/fix-issue-964
fix: fix issue#964
2 parents 72cd980 + 6eee2b4 commit bc2b23e

File tree

2 files changed

+117
-17
lines changed

2 files changed

+117
-17
lines changed

packages/omi-reactify/src/demo/index.tsx

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import '../../dist/omi-component.es.js';
2+
import './omi-tree';
23
import React, { useEffect, useRef, useState } from 'react';
34
import { createRoot } from 'react-dom/client';
45
import reactify from '../index';
56

67
const OmiReactComponent = reactify('omi-component-demo') as any;
8+
const OmiTree = reactify('omi-tree') as any;
79

810
class ClassComponent extends React.Component<any, any> {
911
constructor(props) {
@@ -13,7 +15,7 @@ class ClassComponent extends React.Component<any, any> {
1315
};
1416
}
1517

16-
ref = null;
18+
ref: HTMLHeadingElement | null = null;
1719

1820
componentDidMount(): void {
1921
console.log('componentDidMount', this.ref);
@@ -79,21 +81,51 @@ const App = (): any => {
7981
console.log('ref', ref.current)
8082
}, [])
8183
console.log('=====App render=====');
82-
return <div>
83-
<OmiReactComponent
84-
show={true}
85-
label='React Component'
86-
complex={complex}
87-
ref={ref}
88-
camelCase="camelCase"
89-
style={{ color: 'red' }}
90-
onMockClick={(_e: React.MouseEvent) => { setComplex({ name: 'Omi' }) }}
91-
renderFunction={MockComponent}
92-
>
93-
<span className='content'>content</span>
94-
</OmiReactComponent>
95-
<button className='content' onClick={btnClick}>change name</button>
96-
</div>
84+
const [treeData] = useState([
85+
{
86+
name: 'Root',
87+
children: [
88+
{ name: 'Child 1' },
89+
{
90+
name: 'Child 2',
91+
children: [
92+
{ name: 'Grandchild 1' },
93+
{ name: 'Grandchild 2' }
94+
]
95+
}
96+
]
97+
}
98+
]);
99+
100+
const handleNodeClick = (e: CustomEvent) => {
101+
console.log('Node clicked:', e.detail);
102+
};
103+
104+
return (
105+
<div style={{ padding: '20px' }}>
106+
<OmiReactComponent
107+
show={true}
108+
label='React Component'
109+
complex={complex}
110+
ref={ref}
111+
camelCase="camelCase"
112+
style={{ color: 'red' }}
113+
onMockClick={(_e: React.MouseEvent) => { setComplex({ name: 'Omi' }) }}
114+
renderFunction={MockComponent}
115+
>
116+
<span className='content'>content</span>
117+
</OmiReactComponent>
118+
<button className='content' onClick={btnClick}>change name</button>
119+
120+
<div style={{ marginTop: '20px', padding: '20px', border: '1px solid #eee', borderRadius: '4px' }}>
121+
<h2>Tree Demo</h2>
122+
<OmiTree
123+
nodes={treeData}
124+
onNodeClick={handleNodeClick}
125+
/>
126+
</div>
127+
</div>
128+
)
97129
}
98130

99-
createRoot(document.getElementById('app')!).render(<App /> as any);
131+
createRoot(document.getElementById('app')!).render(<App />);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { Component, tag, h } from 'omi'
2+
3+
interface TreeNode {
4+
name: string
5+
children?: TreeNode[]
6+
}
7+
8+
interface TreeProps {
9+
nodes: TreeNode[]
10+
}
11+
12+
@tag('omi-tree')
13+
export default class OmiTree extends Component<TreeProps> {
14+
static css = `
15+
.tree-node {
16+
padding-left: 20px;
17+
margin: 5px 0;
18+
}
19+
.tree-node-title {
20+
cursor: pointer;
21+
padding: 2px 5px;
22+
border-radius: 3px;
23+
}
24+
.tree-node-title:hover {
25+
background: #f0f0f0;
26+
}
27+
`
28+
29+
static propTypes = {
30+
nodes: Array
31+
}
32+
33+
static defaultProps = {
34+
nodes: []
35+
}
36+
37+
render() {
38+
const { nodes = [] } = this.props
39+
40+
console.log('OmiTree render, props:', this.props);
41+
console.log('OmiTree render, nodes:', nodes);
42+
console.log('OmiTree render, nodes type:', typeof nodes);
43+
console.log('OmiTree render, nodes isArray:', Array.isArray(nodes));
44+
45+
const renderNode = (node: TreeNode, index: number) => (
46+
h('div', { className: 'tree-node', key: index }, [
47+
h('div', {
48+
className: 'tree-node-title',
49+
onClick: () => this.fire('nodeClick', node)
50+
}, node.name),
51+
...(node.children ? node.children.map((child, childIndex) => renderNode(child, childIndex)) : [])
52+
])
53+
)
54+
55+
if (!Array.isArray(nodes) || nodes.length === 0) {
56+
return (
57+
h('div', {}, [
58+
h('p', {}, 'No nodes to display'),
59+
h('p', {}, `Debug: nodes = ${JSON.stringify(nodes)}`)
60+
])
61+
)
62+
}
63+
64+
return (
65+
h('div', {}, nodes.map((node, index) => renderNode(node, index)))
66+
)
67+
}
68+
}

0 commit comments

Comments
 (0)