✨ Added the withStorage component
This commit is contained in:
parent
6aa14f0613
commit
00c32a966c
4 changed files with 50 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -16,6 +16,7 @@ packages/components/Robot
|
||||||
packages/components/SampleConfigurator
|
packages/components/SampleConfigurator
|
||||||
packages/components/withGist
|
packages/components/withGist
|
||||||
packages/components/withLanguage
|
packages/components/withLanguage
|
||||||
|
packages/components/withStorage
|
||||||
packages/components/Workbench
|
packages/components/Workbench
|
||||||
packages/core/coverage
|
packages/core/coverage
|
||||||
packages/workbench
|
packages/workbench
|
||||||
|
|
|
@ -44,7 +44,11 @@ const Example = props => {
|
||||||
rendertest,
|
rendertest,
|
||||||
tutorial
|
tutorial
|
||||||
};
|
};
|
||||||
const settings = { options: { ...props.options } };
|
const settings = {
|
||||||
|
options: { ...props.options },
|
||||||
|
measurements: { headCircumference: 390 },
|
||||||
|
...props.settings
|
||||||
|
};
|
||||||
if (props.part !== "") settings.only = [props.part];
|
if (props.part !== "") settings.only = [props.part];
|
||||||
const pattern = new patterns[props.pattern](settings);
|
const pattern = new patterns[props.pattern](settings);
|
||||||
|
|
||||||
|
|
|
@ -13,5 +13,6 @@ export default [
|
||||||
"SampleConfigurator",
|
"SampleConfigurator",
|
||||||
"withGist",
|
"withGist",
|
||||||
"withLanguage",
|
"withLanguage",
|
||||||
|
"withStorage",
|
||||||
"Workbench"
|
"Workbench"
|
||||||
];
|
];
|
||||||
|
|
43
packages/components/src/withStorage/index.js
Normal file
43
packages/components/src/withStorage/index.js
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
import React from "react";
|
||||||
|
import { storage } from "@freesewing/utils";
|
||||||
|
|
||||||
|
const withStorage = (WrappedComponent, name) => {
|
||||||
|
return class extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.setStorageData = this.setStorageData.bind(this);
|
||||||
|
this.updateStorageData = this.updateStorageData.bind(this);
|
||||||
|
this.state = { data: storage.get(name) || {} };
|
||||||
|
}
|
||||||
|
|
||||||
|
setStorageData(data) {
|
||||||
|
storage.set(name, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
updateStorageData(value, l1 = false, l2 = false, l3 = false) {
|
||||||
|
if (!l1) return;
|
||||||
|
let data = this.state.data;
|
||||||
|
|
||||||
|
if (l2 && typeof data[l1] === "undefined") data[l1] = {};
|
||||||
|
if (l3 && typeof data[l1][l2] === "undefined") data[l1][l2] = {};
|
||||||
|
|
||||||
|
if (l3) data[l1][l2][l3] = value;
|
||||||
|
else if (l2) data[l1][l2] = value;
|
||||||
|
else data[l1] = value;
|
||||||
|
this.setState({ data });
|
||||||
|
storage.set(name, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<WrappedComponent
|
||||||
|
storageData={this.state.data}
|
||||||
|
updateStorageData={this.updateStorageData}
|
||||||
|
{...this.props}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default withStorage;
|
Loading…
Add table
Add a link
Reference in a new issue