Skip to content

Commit

Permalink
fix: 调整jsx的代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Jan 6, 2021
1 parent 6634a0e commit 0a9e43d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
27 changes: 1 addition & 26 deletions packages/jsx/jsx-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
import { batch2hd, isArray } from './src/util';
import jsx from './src/jsx';
import Fragment from './src/fragment';

// 实现jsx-runtime 入口,需要使用es5语法
var jsx = function(type: string | Function, props: any, key?: string) {
// f2组件,需要再外部实例化
// @ts-ignore
if (type.prototype && type.prototype.isComponent) {
return {
type,
props,
};
}
if (typeof type === 'function') {
return type(props);
}
const { style, attrs, children } = props;

return {
type,
props,
style: batch2hd(style) || {},
attrs: batch2hd(attrs),
children: children && !isArray(children) ? [ children ] : children,
key,
};
};

export { Fragment, jsx, jsx as jsxs };
28 changes: 28 additions & 0 deletions packages/jsx/src/jsx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { batch2hd, isArray, isFunction } from './util';

// 实现jsx-runtime 入口
export default function(type: string | Function, props: any, key?: string) {
if (isFunction(type)) {
// f2组件,需要在外部实例化
// @ts-ignore
if (type.prototype && type.prototype.isComponent) {
return {
type,
props,
key,
};
}
// @ts-ignore
return type(props);
}
const { style, attrs, children } = props;

return {
type,
props,
style: batch2hd(style) || {},
attrs: batch2hd(attrs),
children: children && !isArray(children) ? [ children ] : children,
key,
};
};
3 changes: 2 additions & 1 deletion packages/jsx/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const isNumber = is('Number');
const isString = is('String');
const isArray = is('Array');
const isObject = is('Object');
const isFunction = is('Function')

function parsePadding(padding: any) {
if (isNumber(padding) || isString(padding)) {
Expand Down Expand Up @@ -77,4 +78,4 @@ function batch2hd(value: any) {
}


export { isString, isArray, isObject, batch2hd };
export { isString, isArray, isObject, isFunction, batch2hd };
3 changes: 2 additions & 1 deletion packages/react/src/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, Children, createElement } from 'react';
import { useEffect, Children } from 'react';
import F2 from '@antv/f2';
import { render } from '../../jsx/src/index';

Expand Down Expand Up @@ -59,6 +59,7 @@ export default ({ canvasRef, pixelRatio, data, children }: any) => {
components.push(component);
return;
}
// function component
let subChildren = type(props);
subChildren = subChildren && !Array.isArray(subChildren) ? [ subChildren ] : subChildren;

Expand Down
1 change: 1 addition & 0 deletions packages/react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react",
"esModuleInterop": true
},
"extends": "../../tsconfig.json"
Expand Down

0 comments on commit 0a9e43d

Please sign in to comment.