Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react): 优化react适配逻辑 #170

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions script/plugin-tdoc/md-to-wc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ function getGitTimestamp(file) {
});
}

export default async function mdToReact(options) {
export default async function mdToWebC(options) {
const mdSegment = await customRender(options);
const { demoDefsStr, demoCodesDefsStr, components } = options;

const reactSource = `
const webCSource = `
import { h, define } from 'omi';
import { signal, effect } from 'reactive-signal'
import Prismjs from 'prismjs';
Expand Down Expand Up @@ -137,7 +137,7 @@ export default async function mdToReact(options) {
}
`;

const result = esbuild.transformSync(reactSource, {
const result = esbuild.transformSync(webCSource, {
loader: 'tsx',
jsxFactory: 'h',
jsxFragment: 'h.f',
Expand Down
22 changes: 21 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { VNode, WeElement } from 'omi';
import { Component, VNode, WeElement } from 'omi';

export type TElement<T = undefined> = T extends undefined ? WeElement : (props: T) => WeElement;
export type TNode<T = any> = VNode<T> | ((props: T) => VNode) | object | string | number | boolean | null;
Expand Down Expand Up @@ -119,3 +119,23 @@ export interface ScrollToElementParams {
export interface ComponentScrollToElementParams extends ScrollToElementParams {
key?: string | number;
}

export type ExtendedElement = (HTMLElement | SVGAElement | HTMLInputElement) & {
receiveProps: Function;
update: Function;
queuedUpdate: Function;
store?: unknown;
className?: string;
props: Record<string, unknown>;
splitText?: Function;
prevProps?: Record<string, unknown> & {
ref?:
| {
current?: unknown;
}
| Function;
};
attributes: NamedNodeMap;
_component?: Component;
_listeners: Record<string, Function>;
} & Record<string, unknown>;
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ export * from './tag-input';
export * from './textarea';
export * from './tooltip';
export * from './upload';
export * from './vue';
export * from './watermark';
22 changes: 2 additions & 20 deletions src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,9 @@
* 在React环境中使用的兼容方法
*/

import { Component, render } from 'omi';
import { render } from 'omi';

export type ExtendedElement = (HTMLElement | SVGAElement | HTMLInputElement) & {
receiveProps: Function;
update: Function;
queuedUpdate: Function;
store?: unknown;
className?: string;
props: Record<string, unknown>;
splitText?: Function;
prevProps?: Record<string, unknown> & {
ref?:
| {
current?: unknown;
}
| Function;
};
attributes: NamedNodeMap;
_component?: Component;
_listeners: Record<string, Function>;
} & Record<string, unknown>;
import { ExtendedElement } from './common';

const convertReactToOmi = (r: any): Omi.ComponentChild => {
if (!r) return r;
Expand Down
Loading