Skip to content

Commit

Permalink
feat(use): 支持在React环境中使用
Browse files Browse the repository at this point in the history
  • Loading branch information
duenyang committed Oct 30, 2024
1 parent 0e510fa commit 9450ca8
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 14 deletions.
2 changes: 1 addition & 1 deletion script/generate-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const componentsPath = path.resolve(__dirname, '../src');
const components = fs.readdirSync(componentsPath).filter((name) => {
if (['style', 'icon'].includes(name) || name.startsWith('_')) return false;
const componentPath = path.resolve(componentsPath, name);
if (fs.statSync(componentPath).isDirectory()) {
if (fs.statSync(componentPath).isDirectory() || /src\/react\.ts|src\/vue\.ts/.test(componentPath)) {
return true;
}
return false;
Expand Down
26 changes: 26 additions & 0 deletions site/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ export default defineConfig({
}
```

### 在React中使用

首先引入`renderReact`

```javascript
import { renderReact } from 'tdesign-web-components';
```

在React项目中使用

```javascript
const App: React.FC = () => {
const ref = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
if (ref.current) {
renderReact(<t-button>BUTTON</t-button>, ref.current)
}
}, [])


return (
<div ref={ref}></div>
)
}
```

### 更改主题

由于原始样式基于 less 编写,需要自行处理 less 文件的编译(例如安装 less、less-loader)
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export * from './popup';
export * from './progress';
export * from './radio';
export * from './range-input';
export * from './react.ts';
export * from './select';
export * from './select-input';
export * from './skeleton';
Expand Down
2 changes: 1 addition & 1 deletion src/menu/MenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'tdesign-web-components/tooltip';
import '../tooltip';

import { bind, Component, tag } from 'omi';

Expand Down
3 changes: 1 addition & 2 deletions src/message/const.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getClassPrefix } from 'tdesign-web-components/_util/classname';

import { getClassPrefix } from '../_util/classname';
import { MessagePlacementList, MessageThemeList } from './type';

const Distance = '32px';
Expand Down
2 changes: 1 addition & 1 deletion src/message/message.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, render, tag } from 'omi';
import { convertToLightDomNode } from 'tdesign-web-components/_util/lightDom';

import classname from '../_util/classname';
import { convertToLightDomNode } from '../_util/lightDom';
import { AttachNodeReturnValue, Styles, TNode } from '../common';
import { getMessageConfig, globalConfig, setGlobalConfig } from './config';
import { PlacementOffset, tdMessageListClass, tdMessagePlacementClassGenerator } from './const';
Expand Down
4 changes: 2 additions & 2 deletions src/message/messageClose.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import 'tdesign-icons-web-components/esm/components/close';

import Omi, { classNames, Component, tag } from 'omi';
import { StyledProps } from 'tdesign-web-components';
import { parseContentTNode } from 'tdesign-web-components/_util/parseTNode';

import { parseContentTNode } from '../_util/parseTNode';
import { StyledProps } from '../common';
import { tdMessageClassGenerator } from './const';
import { TdMessageProps } from './type';

Expand Down
2 changes: 1 addition & 1 deletion src/message/messageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import './messageClose';

import classNames from 'classnames';
import Omi, { Component, createRef, signal, tag } from 'omi';
import MessageIcon from 'tdesign-web-components/message/messageIcon';

import { StyledProps, TNode } from '../common';
import { tdClassIsGenerator, tdMessageClassGenerator, tdMessagePrefix } from './const';
import MessageIcon from './messageIcon';
// 依赖组件引入
import { TdMessageProps } from './type';

Expand Down
2 changes: 1 addition & 1 deletion src/message/messageIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'tdesign-icons-web-components/esm/components/check-circle-filled';
import 'tdesign-icons-web-components/esm/components/info-circle-filled';
import 'tdesign-icons-web-components/esm/components/error-circle-filled';
import 'tdesign-icons-web-components/esm/components/help-circle-filled';
import 'tdesign-web-components/loading';
import '../loading';

import { classPrefix } from './const';
import { TdMessageProps } from './type';
Expand Down
2 changes: 1 addition & 1 deletion src/range-input/RangeInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'tdesign-icons-web-components';
import 'tdesign-web-components/input';
import '../input';
import './RangeInputInner';

import { bind, classNames, Component, signal, tag } from 'omi';
Expand Down
5 changes: 3 additions & 2 deletions src/range-input/RangeInputInner.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { tag } from 'omi';
import { getClassPrefix } from 'tdesign-web-components/_util/classname';
import { Input } from 'tdesign-web-components/input';

import { getClassPrefix } from '../_util/classname';
import { Input } from '../input';

@tag('t-range-input-inner')
export default class RangeInput extends Input {
Expand Down
3 changes: 1 addition & 2 deletions src/range-input/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { InputFormatType, InputValue } from 'tdesign-web-components/input';

import { TNode } from '../common';
import { InputFormatType, InputValue } from '../input';

export interface TdRangeInputProps {
/**
Expand Down
56 changes: 56 additions & 0 deletions src/react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 在React环境中使用的兼容方法
*/

import { render } from 'omi';

const convertReactToOmi = (r: any): Omi.ComponentChild => {
if (!r) return r;
if (typeof r === 'string') return r;
const {
type,
key,
ref,
props: { children, ...rest },
} = r;
const omiVNode = {
nodeName: type,
attributes: { ignoreAttrs: true, ref, ...rest },
key,
children: [],
};

if (!children) {
return omiVNode;
}

if (Array.isArray(children)) {
return {
...omiVNode,
children: children.map((c: any) => convertReactToOmi(c)),
};
}

if (typeof children === 'object') {
return {
...omiVNode,
children: [children].map((c: any) => convertReactToOmi(c)),
};
}

return {
...omiVNode,
children,
};
};

/**
* 在react环境中渲染组件
* @param reactVNode react的vnode结构
* @param root 需要挂载的html
*/
const renderReact = <T = any>(reactVNode: T, root: HTMLElement) => {
render(convertReactToOmi(reactVNode), root);
};

export { renderReact, convertReactToOmi };

0 comments on commit 9450ca8

Please sign in to comment.