Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
fix: 修复原生组件驼峰风格,React风格函数属性转换错误的问题
Browse files Browse the repository at this point in the history
fix #357
  • Loading branch information
Darmody committed Nov 15, 2019
1 parent c825842 commit 11d226f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/remax/src/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { generate } from './instanceId';
import { generate as generateActionId } from './actionId';
import { FiberRoot } from 'react-reconciler';
import Platform from './Platform';
import propsAlias from './propsAlias';
import propsAlias, { isHostComponent } from './propsAlias';

function stringPath(path: Path) {
return path.join('.');
Expand All @@ -12,7 +12,7 @@ function stringPath(path: Path) {
function transformRawNode(item: RawNode): RawNode {
return {
...item,
props: propsAlias(item.props),
props: propsAlias(item.props, !isHostComponent(item.type)),
children: item.children
? item.children.map(transformRawNode)
: item.children,
Expand Down
6 changes: 3 additions & 3 deletions packages/remax/src/VNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Container from './Container';

export interface RawNode {
id?: number;
type: string | symbol;
type: string;
props?: any;
children?: RawNode[];
text?: string;
Expand All @@ -16,7 +16,7 @@ export default class VNode {
container: Container;
children: VNode[];
mounted = false;
type: string | symbol;
type: string;
props?: any;
parent: VNode | null = null;
text?: string;
Expand All @@ -28,7 +28,7 @@ export default class VNode {
container,
}: {
id: number;
type: string | symbol;
type: string;
props?: any;
container: any;
}) {
Expand Down
33 changes: 32 additions & 1 deletion packages/remax/src/__tests__/propsAlias.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import propsAlias, { getAlias } from '../propsAlias';
import propsAlias, { getAlias, isHostComponent } from '../propsAlias';
import { TYPE_TEXT } from '../constants';

describe('propsAlias', () => {
it('transform className prop correctly', () => {
Expand Down Expand Up @@ -32,4 +33,34 @@ describe('propsAlias', () => {
})
).toMatchSnapshot();
});

describe('check host components', () => {
afterEach(() => {
process.env['REMAX_PLATFORM'] = undefined;
});

it('wechat', () => {
process.env['REMAX_PLATFORM'] = 'wechat';
require('../adapters/wechat/components/Button');
expect(isHostComponent(TYPE_TEXT)).toBeTruthy();
expect(isHostComponent('button')).toBeTruthy();
expect(isHostComponent('foo')).toBeFalsy();
});

it('alipay', () => {
process.env['REMAX_PLATFORM'] = 'alipay';
require('../adapters/alipay/components/Button');
expect(isHostComponent(TYPE_TEXT)).toBeTruthy();
expect(isHostComponent('button')).toBeTruthy();
expect(isHostComponent('foo')).toBeFalsy();
});

it('toutiao', () => {
process.env['REMAX_PLATFORM'] = 'toutiao';
require('../adapters/toutiao/components/Button');
expect(isHostComponent(TYPE_TEXT)).toBeTruthy();
expect(isHostComponent('button')).toBeTruthy();
expect(isHostComponent('foo')).toBeFalsy();
});
});
});
7 changes: 7 additions & 0 deletions packages/remax/src/adapters/alipay/components/factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import React, { forwardRef } from 'react';

const hostComponent = new Set();

export function isHostComponent(name: string) {
return hostComponent.has(name);
}

export default function factory<P = any>(component: string) {
hostComponent.add(component);
const Component: React.FC<P> = (props, ref: any) => {
const { children = [] } = props;
return React.createElement(component, { ...props, ref }, children);
Expand Down
7 changes: 7 additions & 0 deletions packages/remax/src/adapters/toutiao/components/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ type defaultProps = {
style?: React.CSSProperties;
};

const hostComponent = new Set();

export function isHostComponent(name: string) {
return hostComponent.has(name);
}

export default function factory<P = any>(component: string) {
hostComponent.add(component);
const Component: React.FC<P & defaultProps> = (props, ref: any) => {
const { children = [] } = props;
return React.createElement(component, { ...props, ref }, children);
Expand Down
8 changes: 8 additions & 0 deletions packages/remax/src/adapters/wechat/components/factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import React, { forwardRef } from 'react';

const hostComponent = new Set();

export function isHostComponent(name: string) {
console.log(name, hostComponent.size);
return hostComponent.has(name);
}

export default function factory<P = any>(component: string) {
hostComponent.add(component);
const Component: React.FC<P> = (props, ref: any) => {
const { children = [] } = props;
return React.createElement(component, { ...props, ref }, children);
Expand Down
21 changes: 21 additions & 0 deletions packages/remax/src/propsAlias.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import { TYPE_TEXT } from './constants';
import kebabCase from 'lodash.kebabcase';
import Platform from './Platform';
import plainStyle from './utils/plainStyle';
import { isHostComponent as isWechatHostComponent } from './adapters/wechat/components/factory';
import { isHostComponent as isAlipayHostComponent } from './adapters/alipay/components/factory';
import { isHostComponent as isToutiaoHostComponent } from './adapters/toutiao/components/factory';

export function isHostComponent(name: string) {
if (name === TYPE_TEXT) {
return true;
}

switch (Platform.current) {
case 'wechat':
return isWechatHostComponent(name);
case 'alipay':
return isAlipayHostComponent(name);
case 'toutiao':
return isToutiaoHostComponent(name);
}

return false;
}

function functionPropAlias(prop: string, platform?: string) {
prop = prop.replace(/Click$/, 'Tap');
Expand Down

0 comments on commit 11d226f

Please sign in to comment.