@remvst/configurable-react 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/boolean-configurable-component.d.ts +6 -0
- package/lib/boolean-configurable-component.js +13 -0
- package/lib/button-configurable-component.d.ts +6 -0
- package/lib/button-configurable-component.js +13 -0
- package/lib/color-configurable-component.d.ts +7 -0
- package/lib/color-configurable-component.js +22 -0
- package/lib/component-props.d.ts +4 -0
- package/lib/component-props.js +2 -0
- package/lib/composite-configurable-component.d.ts +12 -0
- package/lib/composite-configurable-component.js +22 -0
- package/lib/enum-configurable-component.d.ts +6 -0
- package/lib/enum-configurable-component.js +28 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +46 -0
- package/lib/invalidatable-component.d.ts +17 -0
- package/lib/invalidatable-component.js +27 -0
- package/lib/mapping.d.ts +3 -0
- package/lib/mapping.js +2 -0
- package/lib/number-configurable-component.d.ts +6 -0
- package/lib/number-configurable-component.js +13 -0
- package/lib/string-configurable-component.d.ts +6 -0
- package/lib/string-configurable-component.js +13 -0
- package/package.json +19 -0
- package/src/boolean-configurable-component.tsx +13 -0
- package/src/button-configurable-component.tsx +14 -0
- package/src/color-configurable-component.tsx +21 -0
- package/src/component-props.ts +5 -0
- package/src/composite-configurable-component.tsx +34 -0
- package/src/enum-configurable-component.tsx +37 -0
- package/src/index.tsx +48 -0
- package/src/invalidatable-component.tsx +39 -0
- package/src/mapping.ts +7 -0
- package/src/number-configurable-component.tsx +16 -0
- package/src/string-configurable-component.tsx +13 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { BooleanConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
export default class BooleanConfigurableComponent extends React.Component<ComponentProps<BooleanConfigurable>> {
|
|
5
|
+
render(): JSX.Element;
|
|
6
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const react_1 = __importDefault(require("react"));
|
|
7
|
+
class BooleanConfigurableComponent extends react_1.default.Component {
|
|
8
|
+
render() {
|
|
9
|
+
const { configurable } = this.props;
|
|
10
|
+
return (react_1.default.createElement("input", { type: "checkbox", defaultChecked: configurable.read(), onChange: (event) => configurable.write(event.target.checked) }));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.default = BooleanConfigurableComponent;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ButtonConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
export default class ButtonConfigurableComponent extends React.Component<ComponentProps<ButtonConfigurable>> {
|
|
5
|
+
render(): JSX.Element;
|
|
6
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const react_1 = __importDefault(require("react"));
|
|
7
|
+
class ButtonConfigurableComponent extends react_1.default.Component {
|
|
8
|
+
render() {
|
|
9
|
+
const { configurable } = this.props;
|
|
10
|
+
return (react_1.default.createElement("input", { type: "button", value: configurable.label, onClick: () => configurable.onClick(configurable) }));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.default = ButtonConfigurableComponent;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ColorConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
export declare function leftPad(s: string, pad: string, length: number): string;
|
|
5
|
+
export default class ColorConfigurableComponent extends React.Component<ComponentProps<ColorConfigurable>> {
|
|
6
|
+
render(): JSX.Element;
|
|
7
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.leftPad = void 0;
|
|
7
|
+
const react_1 = __importDefault(require("react"));
|
|
8
|
+
function leftPad(s, pad, length) {
|
|
9
|
+
while (s.length < length) {
|
|
10
|
+
s = pad + s;
|
|
11
|
+
}
|
|
12
|
+
return s;
|
|
13
|
+
}
|
|
14
|
+
exports.leftPad = leftPad;
|
|
15
|
+
class ColorConfigurableComponent extends react_1.default.Component {
|
|
16
|
+
render() {
|
|
17
|
+
const { configurable } = this.props;
|
|
18
|
+
const colorAsHex = '#' + configurable.read().toString(16).padStart(6, '0');
|
|
19
|
+
return (react_1.default.createElement("input", { type: "color", defaultValue: colorAsHex, onBlur: event => configurable.write(parseInt(event.target.value.slice(1), 16)) }));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.default = ColorConfigurableComponent;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { CompositeConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
import { ConfigurableToComponent } from './mapping';
|
|
5
|
+
interface Props extends ComponentProps<CompositeConfigurable> {
|
|
6
|
+
readonly mapper: ConfigurableToComponent;
|
|
7
|
+
}
|
|
8
|
+
export default class CompositeConfigurableComponent extends React.Component<Props> {
|
|
9
|
+
onClickLabel(event: React.MouseEvent): void;
|
|
10
|
+
render(): JSX.Element;
|
|
11
|
+
}
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const react_1 = __importDefault(require("react"));
|
|
7
|
+
class CompositeConfigurableComponent extends react_1.default.Component {
|
|
8
|
+
onClickLabel(event) {
|
|
9
|
+
event.target.parentElement?.querySelector('input,select')?.focus();
|
|
10
|
+
}
|
|
11
|
+
render() {
|
|
12
|
+
const { configurable, mapper } = this.props;
|
|
13
|
+
const subComponents = configurable.entries.map(subConfigurable => {
|
|
14
|
+
const id = `id-${Math.floor(Math.random() * 100000)}`;
|
|
15
|
+
return (react_1.default.createElement("div", { key: subConfigurable.key, className: "composite-configurable-row" },
|
|
16
|
+
react_1.default.createElement("label", { onClick: event => this.onClickLabel(event) }, subConfigurable.key),
|
|
17
|
+
mapper(subConfigurable.configurable, mapper)));
|
|
18
|
+
});
|
|
19
|
+
return (react_1.default.createElement("div", { className: "composite-configurable" }, subComponents));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.default = CompositeConfigurableComponent;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { EnumConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
export default class EnumConfigurableComponent extends React.Component<ComponentProps<EnumConfigurable<any>>> {
|
|
5
|
+
render(): JSX.Element;
|
|
6
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const react_1 = __importDefault(require("react"));
|
|
7
|
+
class EnumConfigurableComponent extends react_1.default.Component {
|
|
8
|
+
render() {
|
|
9
|
+
const { configurable } = this.props;
|
|
10
|
+
const value = configurable.read();
|
|
11
|
+
let defaultValue = '';
|
|
12
|
+
const mapping = new Map();
|
|
13
|
+
for (const category of configurable.categories) {
|
|
14
|
+
for (const item of category.items) {
|
|
15
|
+
mapping.set(item.key, item.value);
|
|
16
|
+
if (item.value === value) {
|
|
17
|
+
defaultValue = item.key;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return (react_1.default.createElement("select", { defaultValue: defaultValue, onChange: event => configurable.write(mapping.get(event.target.value)) }, Array.from(configurable.categories).map((category) => {
|
|
22
|
+
return (react_1.default.createElement("optgroup", { label: category.label || '', key: category.label }, category.items.map((item) => {
|
|
23
|
+
return (react_1.default.createElement("option", { key: item.key, value: item.key }, item.key));
|
|
24
|
+
})));
|
|
25
|
+
})));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.default = EnumConfigurableComponent;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ReactElement } from 'react';
|
|
2
|
+
import { Configurable } from '@remvst/configurable';
|
|
3
|
+
import CompositeConfigurableComponent from './composite-configurable-component';
|
|
4
|
+
import { ConfigurableToComponent } from './mapping';
|
|
5
|
+
export declare function defaultComponents(configurable: Configurable, mapper: ConfigurableToComponent): ReactElement;
|
|
6
|
+
export declare function configurableToComponents(configurable: () => Configurable, mapper?: ConfigurableToComponent): JSX.Element;
|
|
7
|
+
export { CompositeConfigurableComponent, ConfigurableToComponent, };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CompositeConfigurableComponent = exports.configurableToComponents = exports.defaultComponents = void 0;
|
|
7
|
+
const react_1 = __importDefault(require("react"));
|
|
8
|
+
const configurable_1 = require("@remvst/configurable");
|
|
9
|
+
const composite_configurable_component_1 = __importDefault(require("./composite-configurable-component"));
|
|
10
|
+
exports.CompositeConfigurableComponent = composite_configurable_component_1.default;
|
|
11
|
+
const number_configurable_component_1 = __importDefault(require("./number-configurable-component"));
|
|
12
|
+
const string_configurable_component_1 = __importDefault(require("./string-configurable-component"));
|
|
13
|
+
const boolean_configurable_component_1 = __importDefault(require("./boolean-configurable-component"));
|
|
14
|
+
const color_configurable_component_1 = __importDefault(require("./color-configurable-component"));
|
|
15
|
+
const enum_configurable_component_1 = __importDefault(require("./enum-configurable-component"));
|
|
16
|
+
const button_configurable_component_1 = __importDefault(require("./button-configurable-component"));
|
|
17
|
+
const invalidatable_component_1 = __importDefault(require("./invalidatable-component"));
|
|
18
|
+
function defaultComponents(configurable, mapper) {
|
|
19
|
+
if (configurable instanceof configurable_1.CompositeConfigurable) {
|
|
20
|
+
return (react_1.default.createElement(composite_configurable_component_1.default, { configurable: configurable, mapper: mapper }));
|
|
21
|
+
}
|
|
22
|
+
if (configurable instanceof configurable_1.ColorConfigurable) {
|
|
23
|
+
return (react_1.default.createElement(color_configurable_component_1.default, { configurable: configurable }));
|
|
24
|
+
}
|
|
25
|
+
if (configurable instanceof configurable_1.NumberConfigurable) {
|
|
26
|
+
return (react_1.default.createElement(number_configurable_component_1.default, { configurable: configurable }));
|
|
27
|
+
}
|
|
28
|
+
if (configurable instanceof configurable_1.StringConfigurable) {
|
|
29
|
+
return (react_1.default.createElement(string_configurable_component_1.default, { configurable: configurable }));
|
|
30
|
+
}
|
|
31
|
+
if (configurable instanceof configurable_1.BooleanConfigurable) {
|
|
32
|
+
return (react_1.default.createElement(boolean_configurable_component_1.default, { configurable: configurable }));
|
|
33
|
+
}
|
|
34
|
+
if (configurable instanceof configurable_1.EnumConfigurable) {
|
|
35
|
+
return (react_1.default.createElement(enum_configurable_component_1.default, { configurable: configurable }));
|
|
36
|
+
}
|
|
37
|
+
if (configurable instanceof configurable_1.ButtonConfigurable) {
|
|
38
|
+
return (react_1.default.createElement(button_configurable_component_1.default, { configurable: configurable }));
|
|
39
|
+
}
|
|
40
|
+
throw new Error('Unrecognized component type');
|
|
41
|
+
}
|
|
42
|
+
exports.defaultComponents = defaultComponents;
|
|
43
|
+
function configurableToComponents(configurable, mapper = defaultComponents) {
|
|
44
|
+
return react_1.default.createElement(invalidatable_component_1.default, { configurable: configurable, mapper: mapper });
|
|
45
|
+
}
|
|
46
|
+
exports.configurableToComponents = configurableToComponents;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Configurable } from '@remvst/configurable';
|
|
3
|
+
import { ConfigurableToComponent } from "./mapping";
|
|
4
|
+
interface Props {
|
|
5
|
+
readonly configurable: () => Configurable;
|
|
6
|
+
readonly mapper: ConfigurableToComponent;
|
|
7
|
+
}
|
|
8
|
+
interface State {
|
|
9
|
+
readonly configurable: Configurable;
|
|
10
|
+
}
|
|
11
|
+
export default class InvalidatableComponent extends React.Component<Props, State> {
|
|
12
|
+
constructor(props: Props);
|
|
13
|
+
private renew;
|
|
14
|
+
private prepareConfigurable;
|
|
15
|
+
render(): React.ReactNode;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const react_1 = __importDefault(require("react"));
|
|
7
|
+
class InvalidatableComponent extends react_1.default.Component {
|
|
8
|
+
constructor(props) {
|
|
9
|
+
super(props);
|
|
10
|
+
this.state = {
|
|
11
|
+
'configurable': this.prepareConfigurable(),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
renew() {
|
|
15
|
+
const configurable = this.prepareConfigurable();
|
|
16
|
+
this.setState({ configurable });
|
|
17
|
+
}
|
|
18
|
+
prepareConfigurable() {
|
|
19
|
+
const configurable = this.props.configurable();
|
|
20
|
+
configurable.onInvalidate = () => this.renew();
|
|
21
|
+
return configurable;
|
|
22
|
+
}
|
|
23
|
+
render() {
|
|
24
|
+
return (react_1.default.createElement("div", null, this.props.mapper(this.state.configurable, this.props.mapper)));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.default = InvalidatableComponent;
|
package/lib/mapping.d.ts
ADDED
package/lib/mapping.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { NumberConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
export default class NumberConfigurableComponent extends React.Component<ComponentProps<NumberConfigurable>> {
|
|
5
|
+
render(): JSX.Element;
|
|
6
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const react_1 = __importDefault(require("react"));
|
|
7
|
+
class NumberConfigurableComponent extends react_1.default.Component {
|
|
8
|
+
render() {
|
|
9
|
+
const { configurable } = this.props;
|
|
10
|
+
return (react_1.default.createElement("input", { type: "number", defaultValue: configurable.read(), step: configurable.step, min: configurable.min, max: configurable.max, onBlur: event => configurable.write(parseFloat(event.target.value)) }));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.default = NumberConfigurableComponent;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { StringConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
export default class StringConfigurableComponent extends React.Component<ComponentProps<StringConfigurable>> {
|
|
5
|
+
render(): JSX.Element;
|
|
6
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const react_1 = __importDefault(require("react"));
|
|
7
|
+
class StringConfigurableComponent extends react_1.default.Component {
|
|
8
|
+
render() {
|
|
9
|
+
const { configurable } = this.props;
|
|
10
|
+
return (react_1.default.createElement("input", { type: "text", defaultValue: configurable.read(), onBlur: event => configurable.write(event.target.value) }));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.default = StringConfigurableComponent;
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@remvst/configurable-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "rm -rf lib && tsc",
|
|
8
|
+
"prepublishOnly": "npm i && npm run build"
|
|
9
|
+
},
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@remvst/configurable": "1.1.2",
|
|
14
|
+
"react": "^18.2.0",
|
|
15
|
+
"react-dom": "^18.2.0",
|
|
16
|
+
"@types/react-dom": "^18.0.6",
|
|
17
|
+
"typescript": "^4.1.3"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { BooleanConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
|
|
5
|
+
export default class BooleanConfigurableComponent extends React.Component<ComponentProps<BooleanConfigurable>> {
|
|
6
|
+
render() {
|
|
7
|
+
const { configurable } = this.props;
|
|
8
|
+
return (<input
|
|
9
|
+
type="checkbox"
|
|
10
|
+
defaultChecked={configurable.read()}
|
|
11
|
+
onChange={(event) => configurable.write(event.target.checked)} />);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ButtonConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
|
|
5
|
+
export default class ButtonConfigurableComponent extends React.Component<ComponentProps<ButtonConfigurable>> {
|
|
6
|
+
render() {
|
|
7
|
+
const { configurable } = this.props;
|
|
8
|
+
return (<input
|
|
9
|
+
type="button"
|
|
10
|
+
value={configurable.label}
|
|
11
|
+
onClick={() => configurable.onClick(configurable)} />
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ColorConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
|
|
5
|
+
export function leftPad(s: string, pad: string, length: number): string {
|
|
6
|
+
while (s.length < length) {
|
|
7
|
+
s = pad + s;
|
|
8
|
+
}
|
|
9
|
+
return s;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default class ColorConfigurableComponent extends React.Component<ComponentProps<ColorConfigurable>> {
|
|
13
|
+
render() {
|
|
14
|
+
const { configurable } = this.props;
|
|
15
|
+
const colorAsHex = '#' + configurable.read().toString(16).padStart(6, '0');
|
|
16
|
+
return (<input
|
|
17
|
+
type="color"
|
|
18
|
+
defaultValue={colorAsHex}
|
|
19
|
+
onBlur={event => configurable.write(parseInt(event.target.value.slice(1), 16))} />);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { CompositeConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
import { ConfigurableToComponent } from './mapping';
|
|
5
|
+
|
|
6
|
+
interface Props extends ComponentProps<CompositeConfigurable> {
|
|
7
|
+
readonly mapper: ConfigurableToComponent;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default class CompositeConfigurableComponent extends React.Component<Props> {
|
|
11
|
+
onClickLabel(event: React.MouseEvent) {
|
|
12
|
+
((event.target as HTMLElement).parentElement?.querySelector('input,select') as unknown as HTMLOrSVGElement)?.focus();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
render() {
|
|
16
|
+
const { configurable, mapper } = this.props;
|
|
17
|
+
|
|
18
|
+
const subComponents = configurable.entries.map(subConfigurable => {
|
|
19
|
+
const id = `id-${Math.floor(Math.random() * 100000)}`;
|
|
20
|
+
return (
|
|
21
|
+
<div key={subConfigurable.key} className="composite-configurable-row">
|
|
22
|
+
<label onClick={event => this.onClickLabel(event)}>{subConfigurable.key}</label>
|
|
23
|
+
{mapper(subConfigurable.configurable, mapper)}
|
|
24
|
+
</div>
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<div className="composite-configurable">
|
|
30
|
+
{subComponents}
|
|
31
|
+
</div>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { EnumConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
|
|
5
|
+
export default class EnumConfigurableComponent extends React.Component<ComponentProps<EnumConfigurable<any>>> {
|
|
6
|
+
|
|
7
|
+
render() {
|
|
8
|
+
const { configurable } = this.props;
|
|
9
|
+
|
|
10
|
+
const value = configurable.read();
|
|
11
|
+
let defaultValue = '';
|
|
12
|
+
|
|
13
|
+
const mapping = new Map<string, any>();
|
|
14
|
+
for (const category of configurable.categories) {
|
|
15
|
+
for (const item of category.items) {
|
|
16
|
+
mapping.set(item.key, item.value);
|
|
17
|
+
if (item.value === value) {
|
|
18
|
+
defaultValue = item.key;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<select defaultValue={defaultValue} onChange={event => configurable.write(mapping.get(event.target.value))}>
|
|
25
|
+
{Array.from(configurable.categories).map((category) => {
|
|
26
|
+
return (<optgroup label={category.label || ''} key={category.label}>
|
|
27
|
+
{category.items.map((item) => {
|
|
28
|
+
return (
|
|
29
|
+
<option key={item.key} value={item.key}>{item.key}</option>
|
|
30
|
+
);
|
|
31
|
+
})}
|
|
32
|
+
</optgroup>);
|
|
33
|
+
})}
|
|
34
|
+
</select>
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import React, { ReactElement } from 'react';
|
|
2
|
+
import { BooleanConfigurable, ButtonConfigurable, ColorConfigurable, CompositeConfigurable, Configurable, EnumConfigurable, NumberConfigurable, StringConfigurable } from '@remvst/configurable';
|
|
3
|
+
import CompositeConfigurableComponent from './composite-configurable-component';
|
|
4
|
+
import NumberConfigurableComponent from './number-configurable-component';
|
|
5
|
+
import StringConfigurableComponent from './string-configurable-component';
|
|
6
|
+
import BooleanConfigurableComponent from './boolean-configurable-component';
|
|
7
|
+
import ColorConfigurableComponent from './color-configurable-component';
|
|
8
|
+
import EnumConfigurableComponent from './enum-configurable-component';
|
|
9
|
+
import ButtonConfigurableComponent from './button-configurable-component';
|
|
10
|
+
import InvalidatableComponent from './invalidatable-component';
|
|
11
|
+
import { ConfigurableToComponent } from './mapping';
|
|
12
|
+
|
|
13
|
+
export function defaultComponents(configurable: Configurable, mapper: ConfigurableToComponent): ReactElement {
|
|
14
|
+
if (configurable instanceof CompositeConfigurable) {
|
|
15
|
+
return (<CompositeConfigurableComponent configurable={configurable} mapper={mapper} />);
|
|
16
|
+
}
|
|
17
|
+
if (configurable instanceof ColorConfigurable) {
|
|
18
|
+
return (<ColorConfigurableComponent configurable={configurable} />);
|
|
19
|
+
}
|
|
20
|
+
if (configurable instanceof NumberConfigurable) {
|
|
21
|
+
return (<NumberConfigurableComponent configurable={configurable} />);
|
|
22
|
+
}
|
|
23
|
+
if (configurable instanceof StringConfigurable) {
|
|
24
|
+
return (<StringConfigurableComponent configurable={configurable} />);
|
|
25
|
+
}
|
|
26
|
+
if (configurable instanceof BooleanConfigurable) {
|
|
27
|
+
return (<BooleanConfigurableComponent configurable={configurable} />);
|
|
28
|
+
}
|
|
29
|
+
if (configurable instanceof EnumConfigurable) {
|
|
30
|
+
return (<EnumConfigurableComponent configurable={configurable} />);
|
|
31
|
+
}
|
|
32
|
+
if (configurable instanceof ButtonConfigurable) {
|
|
33
|
+
return (<ButtonConfigurableComponent configurable={configurable} />);
|
|
34
|
+
}
|
|
35
|
+
throw new Error('Unrecognized component type');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function configurableToComponents(
|
|
39
|
+
configurable: () => Configurable,
|
|
40
|
+
mapper: ConfigurableToComponent = defaultComponents,
|
|
41
|
+
) {
|
|
42
|
+
return <InvalidatableComponent configurable={configurable} mapper={mapper} />;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export {
|
|
46
|
+
CompositeConfigurableComponent,
|
|
47
|
+
ConfigurableToComponent,
|
|
48
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { ComponentProps } from "./component-props";
|
|
3
|
+
|
|
4
|
+
import { Configurable } from '@remvst/configurable';
|
|
5
|
+
import { ConfigurableToComponent } from "./mapping";
|
|
6
|
+
|
|
7
|
+
interface Props {
|
|
8
|
+
readonly configurable: () => Configurable;
|
|
9
|
+
readonly mapper: ConfigurableToComponent;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface State {
|
|
13
|
+
readonly configurable: Configurable;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default class InvalidatableComponent extends React.Component<Props, State> {
|
|
17
|
+
|
|
18
|
+
constructor(props: Props) {
|
|
19
|
+
super(props);
|
|
20
|
+
this.state = {
|
|
21
|
+
'configurable': this.prepareConfigurable(),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private renew() {
|
|
26
|
+
const configurable = this.prepareConfigurable();
|
|
27
|
+
this.setState({ configurable });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private prepareConfigurable(): Configurable {
|
|
31
|
+
const configurable = this.props.configurable();
|
|
32
|
+
configurable.onInvalidate = () => this.renew();
|
|
33
|
+
return configurable;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
render(): React.ReactNode {
|
|
37
|
+
return (<div>{this.props.mapper(this.state.configurable, this.props.mapper)}</div>);
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/mapping.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { NumberConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
|
|
5
|
+
export default class NumberConfigurableComponent extends React.Component<ComponentProps<NumberConfigurable>> {
|
|
6
|
+
render() {
|
|
7
|
+
const { configurable } = this.props;
|
|
8
|
+
return (<input
|
|
9
|
+
type="number"
|
|
10
|
+
defaultValue={configurable.read()}
|
|
11
|
+
step={configurable.step}
|
|
12
|
+
min={configurable.min}
|
|
13
|
+
max={configurable.max}
|
|
14
|
+
onBlur={event => configurable.write(parseFloat(event.target.value))} />);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { StringConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
|
|
5
|
+
export default class StringConfigurableComponent extends React.Component<ComponentProps<StringConfigurable>> {
|
|
6
|
+
render() {
|
|
7
|
+
const { configurable } = this.props;
|
|
8
|
+
return (<input
|
|
9
|
+
type="text"
|
|
10
|
+
defaultValue={configurable.read()}
|
|
11
|
+
onBlur={event => configurable.write(event.target.value)} />);
|
|
12
|
+
}
|
|
13
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2020",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "./lib",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"allowJs": false,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"jsx": "react"
|
|
11
|
+
},
|
|
12
|
+
"include": ["src/index.tsx"],
|
|
13
|
+
"exclude": ["node_modules"]
|
|
14
|
+
}
|