added readme and demo gif & smaller code cleanup

This commit is contained in:
Kemal Sinanagic 2019-05-24 19:34:58 +02:00
parent c26e0113bd
commit 333d6936d5
13 changed files with 134 additions and 82 deletions

View File

@ -1,26 +1,52 @@
## react-mobx-multiple-stores
# Webpart with React and Mobx using multiple stores
This is where you include your WebPart documentation.
## Summary
A sample webpart that uses the [Mobx](https://mobx.js.org/) library with multiple stores to keep track of the applications state.
### Building the code
<img src="assets/demo.gif"/>
```bash
git clone the repo
npm i
npm i -g gulp
gulp
## Used SharePoint Framework Version
![drop](https://img.shields.io/badge/version-1.8.2-green.svg)
## Applies to
* [SharePoint Framework](https://dev.office.com/sharepoint)
* [SharePoint Framework Webpart Samples](https://github.com/SharePoint/sp-dev-fx-webparts)
* [Office 365 developer tenant](https://dev.office.com/sharepoint/docs/spfx/set-up-your-developer-tenant)
## Solution
Solution|Author(s)
--------|---------
react-mobx-multiple-stores | Kemal Sinanagic / [@kemicza](http://twitter.com/kemicza) / kemicza@gmail.com
## Version history
Version|Date|Comments
-------|----|--------
1.0|May 24, 2019|Initial release
## Disclaimer
**THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.**
---
## Minimal Path to Awesome
```sh
$ git clone https://github.com/SharePoint/sp-dev-fx-webparts
$ cd sp-dev-fx-webparts/samples/react-mobx-multiple-stores
$ npm install
$ gulp serve
```
This package produces the following:
## Features
* lib/* - intermediate-stage commonjs build artifacts
* dist/* - the bundled script, along with other resources
* deploy/* - all resources which should be uploaded to a CDN.
* Enforces that the state always needs be updated in **actions**, using the <em>always</em> flag for <em>enforceActions</em>.
* Demonstrates the **toJS** method to convert an observable array to a javascript structure. This is used to render the items in a DetailsList.
* Out-of-the-box MobX **decorators** to keep the code clean.
* **Asynchronous** actions
* MobX **computed** values
* **Typescript** version 3.3.4 using <em>@microsoft/rush-stack-compiler-3.3</em> for compatibility with the latest MobX version and typings
### Build options
gulp clean - TODO
gulp test - TODO
gulp serve - TODO
gulp bundle - TODO
gulp package-solution - TODO
<img src="https://telemetry.sharepointpnp.com/sp-dev-fx-webparts/samples/react-mobx-multiple-stores" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 449 KiB

View File

@ -7025,7 +7025,8 @@
"ansi-regex": {
"version": "2.1.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"aproba": {
"version": "1.2.0",
@ -7440,7 +7441,8 @@
"safe-buffer": {
"version": "5.1.2",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"safer-buffer": {
"version": "2.1.2",
@ -7496,6 +7498,7 @@
"version": "3.0.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"ansi-regex": "^2.0.0"
}
@ -7539,12 +7542,14 @@
"wrappy": {
"version": "1.0.2",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"yallist": {
"version": "3.0.3",
"bundled": true,
"dev": true
"dev": true,
"optional": true
}
}
},

View File

@ -2,7 +2,6 @@ import { action, computed, observable, runInAction } from "mobx";
import { RootStore } from "./RootStore";
export enum ApplicationStatus {
Loading = "Loading",
CreateList = "Create List",
CreateItems = "Create Items",
Completed = "Completed"
@ -69,8 +68,18 @@ export class AppStore {
}
@action
public addListItem(item: IFakeItem): void {
public async addListItem(item: IFakeItem): Promise<void> {
return new Promise<void>((resolve, reject) => {
// Mock adding item a new item
setTimeout(() => {
// Make sure we change our state in an action.
runInAction(() => {
this.items.push(item);
resolve();
});
}, 500);
});
}
@action

View File

@ -19,7 +19,7 @@
"group": { "default": "Other" },
"title": { "default": "MobxTutorial" },
"description": { "default": "An example of a webpart using mobx for managing the application state" },
"officeFabricIconFontName": "Page",
"iconImageUrl": "data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM\/rhtAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZc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cIDvh3J0GYtmimNgqvsdP57rcgJdIV7hfodPN93zbEqd91fgNoWMT89WD1\/qaVseuE8N39kY\/O8NPHCpgF6+HqMCL3bHtvjOjwigzkEKu09\/xfaf\/xk0fI2xChy5LNqKOOejYXjiT7Vt9FJFgxKKNd\/8Zzvb6o8+CXjA5vCaVQrc+seekFUL8LVJZv8DKLJX7WNRDjkAAAAASUVORK5CYII=",
"properties": {
"ApplicationTitle": "Mobx Tutorial Title (change in webpart properties) 😎",
"AllowImportantItems": true

View File

@ -3,7 +3,7 @@ import { DetailsList, DetailsListLayoutMode, IColumn } from 'office-ui-fabric-re
import * as React from 'react';
import { IFakeItem } from '../../../stores/AppStore';
export type DetailedFakeItemViewerProps = {
type DetailedFakeItemViewerProps = {
items: IFakeItem[];
};

View File

@ -7,12 +7,12 @@ import { DetailedFakeItemViewer } from "./DetailedFakeItemViewer";
import { FakeItemCreator } from "./FakeItemCreator";
import styles from "./MobxTutorial.module.scss";
export type FakeItemContainerStoreProps = {
type FakeItemContainerStoreProps = {
appStore: AppStore;
};
export type FakeItemContainerOwnProps = {};
export type FakeItemContainerProps = Partial<FakeItemContainerStoreProps> & FakeItemContainerOwnProps;
type FakeItemContainerOwnProps = {};
type FakeItemContainerProps = Partial<FakeItemContainerStoreProps> & FakeItemContainerOwnProps;
@inject(Stores.AppStore)
@observer
@ -22,14 +22,6 @@ export class FakeItemContainer extends React.Component<FakeItemContainerProps, {
return (
<div className={styles.grid}>
<div className={styles.row}>
<PrimaryButton
text="Confirm items"
onClick={() => { appStore.confirmItems(); }}
className={styles.inputElement}
/>
</div>
<div className={styles.row}>
<div className={`${styles.columnCreateItems}`}>
<FakeItemCreator></FakeItemCreator>
@ -41,6 +33,14 @@ export class FakeItemContainer extends React.Component<FakeItemContainerProps, {
</div>
</div>
<div className={styles.row}>
<PrimaryButton
text="Confirm items"
onClick={() => { appStore.confirmItems(); }}
className={styles.inputElement}
/>
</div>
</div>
);
}

View File

@ -8,27 +8,27 @@ import { Stores } from '../../../stores/RootStore';
import styles from "./MobxTutorial.module.scss";
import { ConfigStore } from "../../../stores/ConfigStore";
export type FakeItemCreatorStoreProps = {
type FakeItemCreatorStoreProps = {
appStore: AppStore;
configStore: ConfigStore;
};
export type FakeItemCreatorOwnProps = {
};
type FakeItemCreatorOwnProps = {};
export type FakeItemCreatorState = {
type FakeItemCreatorState = {
itemTitle: string;
isImportant: boolean;
requiredTitle: string;
isLoading: boolean;
};
export type FakeItemCreatorProps = Partial<FakeItemCreatorStoreProps> & FakeItemCreatorOwnProps;
type FakeItemCreatorProps = Partial<FakeItemCreatorStoreProps> & FakeItemCreatorOwnProps;
const initialState: FakeItemCreatorState = {
itemTitle: "",
isImportant: false,
requiredTitle: undefined
requiredTitle: undefined,
isLoading: false
};
@inject(Stores.AppStore, Stores.ConfigurationStore)
@ -39,8 +39,7 @@ export class FakeItemCreator extends React.Component<FakeItemCreatorProps, FakeI
public render(): React.ReactElement<FakeItemCreatorProps> {
const { configStore } = this.props;
return (
<div className={styles.row}>
<>
<TextField
label="Title"
errorMessage={this.state.requiredTitle}
@ -59,24 +58,28 @@ export class FakeItemCreator extends React.Component<FakeItemCreatorProps, FakeI
/>
<DefaultButton
onClick={this._onAddFakeItem}
onClick={() => this._onAddFakeItem()}
iconProps={{ iconName: 'Add' }}
allowDisabledFocus={true}
className={styles.inputElement}
>Add</DefaultButton>
</div>
disabled={this.state.isLoading}
>{this.state.isLoading ? "Adding..." : "Add"}</DefaultButton>
</>
);
}
private _onAddFakeItem = (): void => {
private async _onAddFakeItem(): Promise<void> {
if (this.state.itemTitle === "" && this.state.itemTitle.length === 0) {
this.setState({ ...this.state, requiredTitle: "Required" });
return;
}
const { appStore, configStore } = this.props;
appStore.addListItem({ title: this.state.itemTitle, important: configStore.allowImportantItems && this.state.isImportant });
this.setState({ ...this.state, isLoading: true });
await appStore.addListItem({
title: this.state.itemTitle,
important: configStore.allowImportantItems && this.state.isImportant
});
this.setState(initialState);
}

View File

@ -7,12 +7,12 @@ import { AppStore } from '../../../stores/AppStore';
import { Stores } from "../../../stores/RootStore";
import styles from './MobxTutorial.module.scss';
export type ListCreatorStoreProps = {
type ListCreatorStoreProps = {
appStore: AppStore;
};
export type ListCreatorProps = Partial<ListCreatorStoreProps>;
export type ListCreatorState = {
type ListCreatorProps = Partial<ListCreatorStoreProps>;
type ListCreatorState = {
loading: boolean;
errorMessage: string;
listTitle: string;
@ -31,6 +31,7 @@ export class ListCreator extends React.Component<ListCreatorProps, ListCreatorSt
const spinner = (<Spinner size={SpinnerSize.xSmall} label="Creating list ..." labelPosition="right" />);
return (
<div className={styles.grid}>
<div className={styles.row}>
<TextField
label="List title"
@ -49,6 +50,7 @@ export class ListCreator extends React.Component<ListCreatorProps, ListCreatorSt
{this.state.loading ? spinner : "Create List"}
</PrimaryButton>
</div>
</div>
);
}

View File

@ -4,6 +4,7 @@ import * as React from 'react';
import { ApplicationStatus, AppStore } from '../../../stores/AppStore';
import { ConfigStore } from '../../../stores/ConfigStore';
import { Stores } from '../../../stores/RootStore';
import { DetailedFakeItemViewer } from './DetailedFakeItemViewer';
import { FakeItemContainer } from './FakeItemContainer';
import { ListCreator } from './ListCreator';
import styles from './MobxTutorial.module.scss';
@ -37,8 +38,7 @@ export class MobxTutorial extends React.Component<MobxTutorialProps, {}> {
<div className={styles.subTitle}>1) Create List</div>
<ListCreator></ListCreator>
</div>
:
null
: null
}
{appStore.status === ApplicationStatus.CreateItems ?
@ -46,6 +46,13 @@ export class MobxTutorial extends React.Component<MobxTutorialProps, {}> {
<div className={styles.subTitle}>2) Create Items</div>
<FakeItemContainer></FakeItemContainer>
</div>
: null
}
{appStore.status === ApplicationStatus.Completed ?
<div className={styles.row}>
<DetailedFakeItemViewer items={appStore.items}></DetailedFakeItemViewer>
</div>
:
null
}

View File

@ -2,7 +2,7 @@ import { Provider } from "mobx-react";
import * as React from 'react';
import { MobxTutorial } from './MobxTutorial';
export type MobxTutorialProviderOwnProps = {
type MobxTutorialProviderOwnProps = {
stores: {};
};

View File

@ -3,12 +3,12 @@ import * as React from 'react';
import { AppStore } from "../../../stores/AppStore";
import { Stores } from '../../../stores/RootStore';
export type ProgressIndicatorStoreProps = {
type ProgressIndicatorStoreProps = {
appStore: AppStore;
};
export type ProgressIndicatorOwnProps = {};
export type ProgressIndicatorProps = Partial<ProgressIndicatorStoreProps> & ProgressIndicatorOwnProps;
type ProgressIndicatorOwnProps = {};
type ProgressIndicatorProps = Partial<ProgressIndicatorStoreProps> & ProgressIndicatorOwnProps;
@inject(Stores.AppStore)
@observer

View File

@ -1,8 +1,8 @@
define([], function() {
define([], function () {
return {
"PropertyPaneDescription": "Application configuration",
"BasicGroupName": "Group Name",
"AppTitleFieldLabel": "Application Title Field",
"BasicGroupName": "Global Settings",
"AppTitleFieldLabel": "Application Title",
"AllowImportantItemsLabel": "Allow important items"
}
});