Skip to content

Commit

Permalink
fix: 支持slot,支持所有的html标签
Browse files Browse the repository at this point in the history
  • Loading branch information
Leman-li committed May 8, 2022
1 parent f674129 commit ad383e7
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 211 deletions.
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tanfu-react-plugin",
"version": "1.0.0",
"version": "1.0.1",
"scripts": {
"start": "dumi dev",
"docs:build": "dumi build",
Expand Down
14 changes: 14 additions & 0 deletions packages/react/src/Foo/a-component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from "react"

export type Aprops = {
text?: string,
aElement?: React.ReactNode
}

const AComponent = function ({ text, aElement }: Aprops) {
return <div>{aElement}这是A组件{text}
<input></input>
</div>
}

export default AComponent
11 changes: 11 additions & 0 deletions packages/react/src/Foo/b-component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react"

const BComponent = function ({ onClick }: Bprops) {
return <div onClick={onClick}>这是B组件</div>
}

export default BComponent

export type Bprops = {
onClick?: () => void
}
25 changes: 13 additions & 12 deletions packages/react/src/Foo/index.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BView, ViewModel } from '.';
import { Controller, EventListener, Injectable, Inject, LifeCycle, HostLifeCycle, Component, ChildView } from 'tanfu-core';
import { ViewModel } from '.';
import { Controller, EventListener, Inject, LifeCycle, HostLifeCycle, Component, ChildView } from 'tanfu-core';
import Tanfu, { Engine } from '../index'
import { BModel, RootModel } from './index.model';

Expand All @@ -8,34 +8,35 @@ export class RootController {

@Inject('engine') engine!: Engine<ViewModel>


@ChildView('b-view') view!: BView
value = 1

constructor(private rootModel: RootModel, private b: string, private bModel: BModel) { }

@HostLifeCycle('willMount')
initData() {
this.engine.setState({
a: {
text: this.rootModel.a + this.bModel.a + ''
text: this.value + ''
}
})
}

@EventListener('b', 'onClick')
handleClick() {
console.log(this.rootModel, this)
console.log('b点击了')
async handleClick() {
this.engine.setState({
a: {
text: this.rootModel.a + ''
text: ++this.value + ''
}
})

}

@HostLifeCycle('didMount')
didMount(){
this.view.update()
delay(num: number) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true)
}, num)
})
}

@LifeCycle('b', 'didMount')
Expand Down
73 changes: 14 additions & 59 deletions packages/react/src/Foo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,94 +1,49 @@
import React, {useEffect } from 'react';
import React, { useEffect } from 'react';
import Tanfu from 'tanfu-core';
import {Component, Controller, EventListener, HostLifeCycle } from 'tanfu-core';
import { Component, Controller, EventListener, HostLifeCycle } from 'tanfu-core';
import { RootController } from './index.controller';
import { BModel, RootModel } from './index.model';
import { html, TanfuView } from 'tanfu-core';
import TanfuReactPlugin from '..';
import AComponent, { Aprops } from './a-component';
import BComponent, { Bprops } from './b-component';

Tanfu.use(new TanfuReactPlugin())
export default ({ title }: { title: string }) => <Root />;



const AComponent = function ({ text }: Aprops) {
return <div>这是A组件{text}</div>
}



const BComponent = function ({ onClick }: Bprops) {
return <div onClick={onClick}>这是B组件</div>
}

@Controller()
class BController{

@HostLifeCycle('didMount')
didMount(){
console.log('Bview加载完成了')
}

@EventListener('div','onClick')
updateDataSource(){
console.log('更新了dataSource')
}
export type ViewModel = {
a: Aprops,
b: Bprops,
myelementId: Aprops
}

@Component({ controllers: [BController]})
export class BView extends TanfuView {


update(){
console.log('触发了自view的方法')
this.dispatchEvent({type: 'div/onClick',payload: undefined })
}

template(){
return html`
<div element-id='div'>自定义的view</div>
`
}
}


@Component({
controllers: [RootController],
providers: [RootModel, BModel],
declarations: [AComponent, BComponent,BView],
declarations: [AComponent, BComponent],
})
class AView extends Tanfu.View {



template() {
return html`
<a-component element-id="a">
<a-component t-id="a">
<div t-slot="aElement">aelemen111t</div>
</a-component>
SFAF
<b-component element-id="b"></b-component>
<b-view element-id="b-view"/>
<b-component t-id="b"></b-component>
<button>button</button>
`
}
}

const Root = Tanfu.mountView(AView)

export default ({ title }: { title: string }) => Root;




export type ViewModel = {
a: Aprops,
b: Bprops,
myelementId: Aprops
}

type Aprops = {
text?: string
}

type Bprops = {
onClick?: () => void
}

16 changes: 16 additions & 0 deletions packages/react/src/declarations.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ComponentArguments } from ".";
import React from 'react'
import htmlTag from "./html-tag";


const reactDeclarations: ComponentArguments['declarations'] = Object.keys(htmlTag).map(key => createDeclaration(htmlTag[key]))

export default reactDeclarations

function createDeclaration(tag: keyof JSX.IntrinsicElements) {
const Tag = htmlTag[tag]
return {
name: tag,
value: (props: any) => <Tag {...props} />
}
}
21 changes: 0 additions & 21 deletions packages/react/src/defaultl-declarations.tsx

This file was deleted.

Loading

0 comments on commit ad383e7

Please sign in to comment.