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

【wip】mobx+WComp测试 #189

Closed
wants to merge 7 commits into from
Closed
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
14 changes: 12 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"copy-to-clipboard": "^3.3.3",
"htm": "^3.1.1",
"lodash-es": "^4.17.21",
"mobx": "^6.13.6",
"omi": "^7.7.5",
"omi-transition": "^0.1.11",
"tailwind-merge": "^2.2.1",
Expand Down
4 changes: 4 additions & 0 deletions src/button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ usage: { title: '', description: '' }
spline: base
---

### 测试mobx使用

{{ mobx }}

### 基础使用

{{ base }}
Expand Down
46 changes: 46 additions & 0 deletions src/button/_example/mobx.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'tdesign-web-components/button';
import 'tdesign-web-components/space';
import 'tdesign-web-components/input';
import './mobxChild';

import { autorun } from 'mobx';
import { Component } from 'omi';

import { myStore } from '../store';

export default class TestMobx extends Component {
btnClick = () => {
console.log('btn click');
myStore.update({
message: '修改后',
});
};

inputChange = (v: string) => {
console.log(v);
myStore.update({
message: v,
});
};

installed() {
console.log('installed');
autorun(() => {
this.update();
console.log('触发autorun');
});
}

render() {
return (
<t-space direction="vertical">
<t-button theme="default" variant="text" onClick={this.btnClick}>
修改message
</t-button>
<t-input value={myStore.message} onChange={this.inputChange} />
{myStore.message}
<t-mobx-child />
</t-space>
);
}
}
23 changes: 23 additions & 0 deletions src/button/_example/mobxChild.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'tdesign-web-components/button';
import 'tdesign-web-components/space';

import { autorun } from 'mobx';
import { Component, tag } from 'omi';

import { myStore } from '../store';

@tag('t-mobx-child')
export default class TestMobxChild extends Component {
// 子组件的autorun可以注掉
installed() {
// 如果父子组件都调了autorun,会多次触发,看是否优化
autorun(() => {
this.update();
console.log('子组件触发autorun');
});
}

render() {
return <t-space>mobx子组件:{myStore.message}</t-space>;
}
}
15 changes: 15 additions & 0 deletions src/button/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { makeAutoObservable } from 'mobx';

export class Store {
message = 'Initial message';

constructor() {
makeAutoObservable(this);
}

update = (props: Partial<Store>): void => {
Object.assign(this, props);
};
}

export const myStore = new Store();
Loading