@remvst/configurable-react 1.0.0 → 1.2.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.js +1 -1
- package/lib/color-configurable-component.d.ts +5 -4
- package/lib/color-configurable-component.js +12 -11
- package/lib/component-state.d.ts +4 -0
- package/lib/component-state.js +2 -0
- package/lib/enum-configurable-component.d.ts +2 -0
- package/lib/enum-configurable-component.js +20 -7
- package/lib/group-configurable-component.d.ts +11 -0
- package/lib/group-configurable-component.js +16 -0
- package/lib/index.js +7 -2
- package/lib/input-component.d.ts +14 -0
- package/lib/input-component.js +51 -0
- package/lib/number-configurable-component.d.ts +5 -3
- package/lib/number-configurable-component.js +19 -2
- package/lib/string-configurable-component.d.ts +5 -3
- package/lib/string-configurable-component.js +9 -3
- package/package.json +5 -2
- package/src/boolean-configurable-component.tsx +1 -1
- package/src/color-configurable-component.tsx +13 -11
- package/src/component-state.ts +4 -0
- package/src/enum-configurable-component.tsx +23 -8
- package/src/group-configurable-component.tsx +28 -0
- package/src/index.tsx +7 -3
- package/src/input-component.tsx +64 -0
- package/src/number-configurable-component.tsx +35 -9
- package/src/string-configurable-component.tsx +12 -6
|
@@ -7,7 +7,7 @@ const react_1 = __importDefault(require("react"));
|
|
|
7
7
|
class BooleanConfigurableComponent extends react_1.default.Component {
|
|
8
8
|
render() {
|
|
9
9
|
const { configurable } = this.props;
|
|
10
|
-
return (react_1.default.createElement("input", { type: "checkbox",
|
|
10
|
+
return (react_1.default.createElement("input", { type: "checkbox", checked: configurable.read(), onChange: (event) => configurable.write(event.target.checked) }));
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
exports.default = BooleanConfigurableComponent;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="react" />
|
|
2
2
|
import { ColorConfigurable } from '@remvst/configurable';
|
|
3
|
-
import
|
|
4
|
-
export
|
|
5
|
-
|
|
3
|
+
import InputComponent from './input-component';
|
|
4
|
+
export default class ColorConfigurableComponent extends InputComponent<number, ColorConfigurable> {
|
|
5
|
+
protected mapValueToConfigurableFormat(value: string): number;
|
|
6
|
+
protected mapValueToInputFormat(value: number): string;
|
|
6
7
|
render(): JSX.Element;
|
|
7
8
|
}
|
|
@@ -3,20 +3,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.leftPad = void 0;
|
|
7
6
|
const react_1 = __importDefault(require("react"));
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
const input_component_1 = __importDefault(require("./input-component"));
|
|
8
|
+
class ColorConfigurableComponent extends input_component_1.default {
|
|
9
|
+
mapValueToConfigurableFormat(value) {
|
|
10
|
+
const parsed = parseInt(value.slice(1), 16);
|
|
11
|
+
if (isNaN(parsed)) {
|
|
12
|
+
throw new Error();
|
|
13
|
+
}
|
|
14
|
+
return parsed;
|
|
15
|
+
}
|
|
16
|
+
mapValueToInputFormat(value) {
|
|
17
|
+
return '#' + value.toString(16).padStart(6, '0');
|
|
11
18
|
}
|
|
12
|
-
return s;
|
|
13
|
-
}
|
|
14
|
-
exports.leftPad = leftPad;
|
|
15
|
-
class ColorConfigurableComponent extends react_1.default.Component {
|
|
16
19
|
render() {
|
|
17
|
-
|
|
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
|
+
return (react_1.default.createElement("input", { type: "color", ...this.inputProperties() }));
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
exports.default = ColorConfigurableComponent;
|
|
@@ -2,5 +2,7 @@ import React from 'react';
|
|
|
2
2
|
import { EnumConfigurable } from '@remvst/configurable';
|
|
3
3
|
import { ComponentProps } from './component-props';
|
|
4
4
|
export default class EnumConfigurableComponent extends React.Component<ComponentProps<EnumConfigurable<any>>> {
|
|
5
|
+
private onChange;
|
|
6
|
+
private get mapping();
|
|
5
7
|
render(): JSX.Element;
|
|
6
8
|
}
|
|
@@ -5,20 +5,33 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const react_1 = __importDefault(require("react"));
|
|
7
7
|
class EnumConfigurableComponent extends react_1.default.Component {
|
|
8
|
-
|
|
8
|
+
onChange(event) {
|
|
9
|
+
const { configurable } = this.props;
|
|
10
|
+
const { mapping } = this;
|
|
11
|
+
configurable.write(mapping.get(event.target.value));
|
|
12
|
+
event.target.blur();
|
|
13
|
+
}
|
|
14
|
+
get mapping() {
|
|
9
15
|
const { configurable } = this.props;
|
|
10
|
-
const value = configurable.read();
|
|
11
|
-
let defaultValue = '';
|
|
12
16
|
const mapping = new Map();
|
|
13
17
|
for (const category of configurable.categories) {
|
|
14
18
|
for (const item of category.items) {
|
|
15
19
|
mapping.set(item.key, item.value);
|
|
16
|
-
if (item.value === value) {
|
|
17
|
-
defaultValue = item.key;
|
|
18
|
-
}
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
|
-
return
|
|
22
|
+
return mapping;
|
|
23
|
+
}
|
|
24
|
+
render() {
|
|
25
|
+
const { configurable } = this.props;
|
|
26
|
+
const configurableValue = configurable.read();
|
|
27
|
+
const { mapping } = this;
|
|
28
|
+
let defaultValue = '';
|
|
29
|
+
for (const [key, value] of mapping) {
|
|
30
|
+
if (value === configurableValue) {
|
|
31
|
+
defaultValue = key;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return (react_1.default.createElement("select", { defaultValue: defaultValue, onChange: (e) => this.onChange(e) }, Array.from(configurable.categories).map((category) => {
|
|
22
35
|
return (react_1.default.createElement("optgroup", { label: category.label || '', key: category.label }, category.items.map((item) => {
|
|
23
36
|
return (react_1.default.createElement("option", { key: item.key, value: item.key }, item.key));
|
|
24
37
|
})));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { GroupConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
import { ConfigurableToComponent } from './mapping';
|
|
5
|
+
interface Props extends ComponentProps<GroupConfigurable> {
|
|
6
|
+
readonly mapper: ConfigurableToComponent;
|
|
7
|
+
}
|
|
8
|
+
export default class CompositeConfigurableComponent extends React.Component<Props> {
|
|
9
|
+
render(): JSX.Element;
|
|
10
|
+
}
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
render() {
|
|
9
|
+
const { configurable, mapper } = this.props;
|
|
10
|
+
const subComponents = configurable.entries.map((subConfigurable, i) => {
|
|
11
|
+
return (react_1.default.createElement("div", { key: i, className: "group-configurable-item" }, mapper(subConfigurable, mapper)));
|
|
12
|
+
});
|
|
13
|
+
return (react_1.default.createElement("div", { className: "group-configurable" }, subComponents));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.default = CompositeConfigurableComponent;
|
package/lib/index.js
CHANGED
|
@@ -14,11 +14,15 @@ const boolean_configurable_component_1 = __importDefault(require("./boolean-conf
|
|
|
14
14
|
const color_configurable_component_1 = __importDefault(require("./color-configurable-component"));
|
|
15
15
|
const enum_configurable_component_1 = __importDefault(require("./enum-configurable-component"));
|
|
16
16
|
const button_configurable_component_1 = __importDefault(require("./button-configurable-component"));
|
|
17
|
+
const group_configurable_component_1 = __importDefault(require("./group-configurable-component"));
|
|
17
18
|
const invalidatable_component_1 = __importDefault(require("./invalidatable-component"));
|
|
18
19
|
function defaultComponents(configurable, mapper) {
|
|
19
20
|
if (configurable instanceof configurable_1.CompositeConfigurable) {
|
|
20
21
|
return (react_1.default.createElement(composite_configurable_component_1.default, { configurable: configurable, mapper: mapper }));
|
|
21
22
|
}
|
|
23
|
+
if (configurable instanceof configurable_1.GroupConfigurable) {
|
|
24
|
+
return (react_1.default.createElement(group_configurable_component_1.default, { configurable: configurable, mapper: mapper }));
|
|
25
|
+
}
|
|
22
26
|
if (configurable instanceof configurable_1.ColorConfigurable) {
|
|
23
27
|
return (react_1.default.createElement(color_configurable_component_1.default, { configurable: configurable }));
|
|
24
28
|
}
|
|
@@ -37,10 +41,11 @@ function defaultComponents(configurable, mapper) {
|
|
|
37
41
|
if (configurable instanceof configurable_1.ButtonConfigurable) {
|
|
38
42
|
return (react_1.default.createElement(button_configurable_component_1.default, { configurable: configurable }));
|
|
39
43
|
}
|
|
40
|
-
throw new Error(
|
|
44
|
+
throw new Error(`Unrecognized component type ${configurable.constructor.name}`);
|
|
41
45
|
}
|
|
42
46
|
exports.defaultComponents = defaultComponents;
|
|
43
47
|
function configurableToComponents(configurable, mapper = defaultComponents) {
|
|
44
|
-
return react_1.default.createElement(
|
|
48
|
+
return react_1.default.createElement("div", { className: 'configurable' },
|
|
49
|
+
react_1.default.createElement(invalidatable_component_1.default, { configurable: configurable, mapper: mapper }));
|
|
45
50
|
}
|
|
46
51
|
exports.configurableToComponents = configurableToComponents;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ReadWriteConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
import { ComponentState } from './component-state';
|
|
5
|
+
export default abstract class InputComponent<ConfigurableValueType, ConfigurableType extends ReadWriteConfigurable<ConfigurableValueType>> extends React.Component<ComponentProps<ConfigurableType>, ComponentState<string>> {
|
|
6
|
+
constructor(props: ComponentProps<ConfigurableType>);
|
|
7
|
+
protected abstract mapValueToConfigurableFormat(value: string): ConfigurableValueType;
|
|
8
|
+
protected abstract mapValueToInputFormat(value: ConfigurableValueType): string;
|
|
9
|
+
protected onChange(event: React.ChangeEvent<HTMLInputElement>, submit?: boolean): void;
|
|
10
|
+
protected onBlur(event: React.FocusEvent<HTMLInputElement>): void;
|
|
11
|
+
protected onKeyDown(event: React.KeyboardEvent<HTMLInputElement>): void;
|
|
12
|
+
protected submit(): void;
|
|
13
|
+
protected inputProperties(fastSubmit?: boolean): React.InputHTMLAttributes<HTMLInputElement>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
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 InputComponent extends react_1.default.Component {
|
|
8
|
+
constructor(props) {
|
|
9
|
+
super(props);
|
|
10
|
+
this.state = {
|
|
11
|
+
'value': this.mapValueToInputFormat(props.configurable.read()),
|
|
12
|
+
'dirty': false,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
onChange(event, submit = false) {
|
|
16
|
+
this.setState({ 'value': event.target.value, 'dirty': true }, () => {
|
|
17
|
+
if (submit) {
|
|
18
|
+
this.submit();
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
onBlur(event) {
|
|
23
|
+
this.submit();
|
|
24
|
+
}
|
|
25
|
+
onKeyDown(event) {
|
|
26
|
+
if (event.key === 'Enter') {
|
|
27
|
+
event.target.blur();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
submit() {
|
|
31
|
+
const { configurable } = this.props;
|
|
32
|
+
const { value } = this.state;
|
|
33
|
+
try {
|
|
34
|
+
const parsed = this.mapValueToConfigurableFormat(value);
|
|
35
|
+
configurable.write(parsed);
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
}
|
|
39
|
+
this.setState({ 'value': this.mapValueToInputFormat(configurable.read()), 'dirty': false });
|
|
40
|
+
}
|
|
41
|
+
inputProperties(fastSubmit = false) {
|
|
42
|
+
return {
|
|
43
|
+
'className': this.state.dirty ? 'dirty' : '',
|
|
44
|
+
'value': this.state.value,
|
|
45
|
+
'onChange': (event) => this.onChange(event, fastSubmit),
|
|
46
|
+
'onBlur': (event) => this.onBlur(event),
|
|
47
|
+
'onKeyDown': (event) => this.onKeyDown(event),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.default = InputComponent;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="react" />
|
|
2
2
|
import { NumberConfigurable } from '@remvst/configurable';
|
|
3
|
-
import
|
|
4
|
-
export default class NumberConfigurableComponent extends
|
|
3
|
+
import InputComponent from './input-component';
|
|
4
|
+
export default class NumberConfigurableComponent extends InputComponent<number, NumberConfigurable> {
|
|
5
|
+
protected mapValueToConfigurableFormat(value: string): number;
|
|
6
|
+
protected mapValueToInputFormat(value: number): string;
|
|
5
7
|
render(): JSX.Element;
|
|
6
8
|
}
|
|
@@ -4,10 +4,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const react_1 = __importDefault(require("react"));
|
|
7
|
-
|
|
7
|
+
const input_component_1 = __importDefault(require("./input-component"));
|
|
8
|
+
class NumberConfigurableComponent extends input_component_1.default {
|
|
9
|
+
mapValueToConfigurableFormat(value) {
|
|
10
|
+
const parsed = parseFloat(value);
|
|
11
|
+
if (isNaN(parsed)) {
|
|
12
|
+
throw new Error();
|
|
13
|
+
}
|
|
14
|
+
return parsed;
|
|
15
|
+
}
|
|
16
|
+
mapValueToInputFormat(value) {
|
|
17
|
+
return value.toString();
|
|
18
|
+
}
|
|
8
19
|
render() {
|
|
9
20
|
const { configurable } = this.props;
|
|
10
|
-
|
|
21
|
+
let slider;
|
|
22
|
+
if (configurable.min !== undefined && configurable.max !== undefined) {
|
|
23
|
+
slider = (react_1.default.createElement("input", { type: "range", min: configurable.min, max: configurable.max, step: configurable.step, ...this.inputProperties(true) }));
|
|
24
|
+
}
|
|
25
|
+
return (react_1.default.createElement("div", { className: 'number-configurable' },
|
|
26
|
+
slider,
|
|
27
|
+
react_1.default.createElement("input", { type: "number", step: configurable.step, min: configurable.min, max: configurable.max, ...this.inputProperties() })));
|
|
11
28
|
}
|
|
12
29
|
}
|
|
13
30
|
exports.default = NumberConfigurableComponent;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="react" />
|
|
2
2
|
import { StringConfigurable } from '@remvst/configurable';
|
|
3
|
-
import
|
|
4
|
-
export default class StringConfigurableComponent extends
|
|
3
|
+
import InputComponent from './input-component';
|
|
4
|
+
export default class StringConfigurableComponent extends InputComponent<string, StringConfigurable> {
|
|
5
|
+
protected mapValueToConfigurableFormat(value: string): string;
|
|
6
|
+
protected mapValueToInputFormat(value: string): string;
|
|
5
7
|
render(): JSX.Element;
|
|
6
8
|
}
|
|
@@ -4,10 +4,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const react_1 = __importDefault(require("react"));
|
|
7
|
-
|
|
7
|
+
const input_component_1 = __importDefault(require("./input-component"));
|
|
8
|
+
class StringConfigurableComponent extends input_component_1.default {
|
|
9
|
+
mapValueToConfigurableFormat(value) {
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
mapValueToInputFormat(value) {
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
8
15
|
render() {
|
|
9
|
-
|
|
10
|
-
return (react_1.default.createElement("input", { type: "text", defaultValue: configurable.read(), onBlur: event => configurable.write(event.target.value) }));
|
|
16
|
+
return (react_1.default.createElement("input", { type: "text", ...this.inputProperties() }));
|
|
11
17
|
}
|
|
12
18
|
}
|
|
13
19
|
exports.default = StringConfigurableComponent;
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remvst/configurable-react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./lib/index.js"
|
|
8
|
+
},
|
|
6
9
|
"scripts": {
|
|
7
10
|
"build": "rm -rf lib && tsc",
|
|
8
11
|
"prepublishOnly": "npm i && npm run build"
|
|
@@ -10,7 +13,7 @@
|
|
|
10
13
|
"author": "",
|
|
11
14
|
"license": "ISC",
|
|
12
15
|
"dependencies": {
|
|
13
|
-
"@remvst/configurable": "1.
|
|
16
|
+
"@remvst/configurable": "^1.3.0",
|
|
14
17
|
"react": "^18.2.0",
|
|
15
18
|
"react-dom": "^18.2.0",
|
|
16
19
|
"@types/react-dom": "^18.0.6",
|
|
@@ -7,7 +7,7 @@ export default class BooleanConfigurableComponent extends React.Component<Compon
|
|
|
7
7
|
const { configurable } = this.props;
|
|
8
8
|
return (<input
|
|
9
9
|
type="checkbox"
|
|
10
|
-
|
|
10
|
+
checked={configurable.read()}
|
|
11
11
|
onChange={(event) => configurable.write(event.target.checked)} />);
|
|
12
12
|
}
|
|
13
13
|
}
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { ColorConfigurable } from '@remvst/configurable';
|
|
3
|
-
import
|
|
3
|
+
import InputComponent from './input-component';
|
|
4
4
|
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
export default class ColorConfigurableComponent extends InputComponent<number, ColorConfigurable> {
|
|
6
|
+
protected mapValueToConfigurableFormat(value: string) {
|
|
7
|
+
const parsed = parseInt(value.slice(1), 16);
|
|
8
|
+
if (isNaN(parsed)) {
|
|
9
|
+
throw new Error();
|
|
10
|
+
}
|
|
11
|
+
return parsed;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected mapValueToInputFormat(value: number): string {
|
|
15
|
+
return '#' + value.toString(16).padStart(6, '0');
|
|
8
16
|
}
|
|
9
|
-
return s;
|
|
10
|
-
}
|
|
11
17
|
|
|
12
|
-
export default class ColorConfigurableComponent extends React.Component<ComponentProps<ColorConfigurable>> {
|
|
13
18
|
render() {
|
|
14
|
-
const { configurable } = this.props;
|
|
15
|
-
const colorAsHex = '#' + configurable.read().toString(16).padStart(6, '0');
|
|
16
19
|
return (<input
|
|
17
20
|
type="color"
|
|
18
|
-
|
|
19
|
-
onBlur={event => configurable.write(parseInt(event.target.value.slice(1), 16))} />);
|
|
21
|
+
{...this.inputProperties()} />);
|
|
20
22
|
}
|
|
21
23
|
}
|
|
@@ -4,24 +4,39 @@ import { ComponentProps } from './component-props';
|
|
|
4
4
|
|
|
5
5
|
export default class EnumConfigurableComponent extends React.Component<ComponentProps<EnumConfigurable<any>>> {
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
private onChange(event: React.ChangeEvent<HTMLSelectElement>) {
|
|
8
8
|
const { configurable } = this.props;
|
|
9
|
+
const { mapping } = this;
|
|
10
|
+
configurable.write(mapping.get(event.target.value));
|
|
11
|
+
event.target.blur();
|
|
12
|
+
}
|
|
9
13
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
private get mapping(): Map<string, any> {
|
|
15
|
+
const { configurable } = this.props;
|
|
13
16
|
const mapping = new Map<string, any>();
|
|
14
17
|
for (const category of configurable.categories) {
|
|
15
18
|
for (const item of category.items) {
|
|
16
19
|
mapping.set(item.key, item.value);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return mapping;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
render() {
|
|
26
|
+
const { configurable } = this.props;
|
|
27
|
+
|
|
28
|
+
const configurableValue = configurable.read();
|
|
29
|
+
const { mapping } = this;
|
|
30
|
+
|
|
31
|
+
let defaultValue = '';
|
|
32
|
+
for (const [key, value] of mapping) {
|
|
33
|
+
if (value === configurableValue) {
|
|
34
|
+
defaultValue = key;
|
|
20
35
|
}
|
|
21
36
|
}
|
|
22
37
|
|
|
23
38
|
return (
|
|
24
|
-
<select defaultValue={defaultValue} onChange={
|
|
39
|
+
<select defaultValue={defaultValue} onChange={(e) => this.onChange(e)}>
|
|
25
40
|
{Array.from(configurable.categories).map((category) => {
|
|
26
41
|
return (<optgroup label={category.label || ''} key={category.label}>
|
|
27
42
|
{category.items.map((item) => {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { GroupConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
import { ConfigurableToComponent } from './mapping';
|
|
5
|
+
|
|
6
|
+
interface Props extends ComponentProps<GroupConfigurable> {
|
|
7
|
+
readonly mapper: ConfigurableToComponent;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default class CompositeConfigurableComponent extends React.Component<Props> {
|
|
11
|
+
render() {
|
|
12
|
+
const { configurable, mapper } = this.props;
|
|
13
|
+
|
|
14
|
+
const subComponents = configurable.entries.map((subConfigurable, i) => {
|
|
15
|
+
return (
|
|
16
|
+
<div key={i} className="group-configurable-item">
|
|
17
|
+
{mapper(subConfigurable, mapper)}
|
|
18
|
+
</div>
|
|
19
|
+
);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<div className="group-configurable">
|
|
24
|
+
{subComponents}
|
|
25
|
+
</div>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { ReactElement } from 'react';
|
|
2
|
-
import { BooleanConfigurable, ButtonConfigurable, ColorConfigurable, CompositeConfigurable, Configurable, EnumConfigurable, NumberConfigurable, StringConfigurable } from '@remvst/configurable';
|
|
2
|
+
import { BooleanConfigurable, ButtonConfigurable, ColorConfigurable, CompositeConfigurable, Configurable, EnumConfigurable, NumberConfigurable, StringConfigurable, GroupConfigurable } from '@remvst/configurable';
|
|
3
3
|
import CompositeConfigurableComponent from './composite-configurable-component';
|
|
4
4
|
import NumberConfigurableComponent from './number-configurable-component';
|
|
5
5
|
import StringConfigurableComponent from './string-configurable-component';
|
|
@@ -7,6 +7,7 @@ import BooleanConfigurableComponent from './boolean-configurable-component';
|
|
|
7
7
|
import ColorConfigurableComponent from './color-configurable-component';
|
|
8
8
|
import EnumConfigurableComponent from './enum-configurable-component';
|
|
9
9
|
import ButtonConfigurableComponent from './button-configurable-component';
|
|
10
|
+
import GroupConfigurableComponent from './group-configurable-component';
|
|
10
11
|
import InvalidatableComponent from './invalidatable-component';
|
|
11
12
|
import { ConfigurableToComponent } from './mapping';
|
|
12
13
|
|
|
@@ -14,6 +15,9 @@ export function defaultComponents(configurable: Configurable, mapper: Configurab
|
|
|
14
15
|
if (configurable instanceof CompositeConfigurable) {
|
|
15
16
|
return (<CompositeConfigurableComponent configurable={configurable} mapper={mapper} />);
|
|
16
17
|
}
|
|
18
|
+
if (configurable instanceof GroupConfigurable) {
|
|
19
|
+
return (<GroupConfigurableComponent configurable={configurable} mapper={mapper} />);
|
|
20
|
+
}
|
|
17
21
|
if (configurable instanceof ColorConfigurable) {
|
|
18
22
|
return (<ColorConfigurableComponent configurable={configurable} />);
|
|
19
23
|
}
|
|
@@ -32,14 +36,14 @@ export function defaultComponents(configurable: Configurable, mapper: Configurab
|
|
|
32
36
|
if (configurable instanceof ButtonConfigurable) {
|
|
33
37
|
return (<ButtonConfigurableComponent configurable={configurable} />);
|
|
34
38
|
}
|
|
35
|
-
throw new Error(
|
|
39
|
+
throw new Error(`Unrecognized component type ${configurable.constructor.name}`);
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
export function configurableToComponents(
|
|
39
43
|
configurable: () => Configurable,
|
|
40
44
|
mapper: ConfigurableToComponent = defaultComponents,
|
|
41
45
|
) {
|
|
42
|
-
return <InvalidatableComponent configurable={configurable} mapper={mapper}
|
|
46
|
+
return <div className='configurable'><InvalidatableComponent configurable={configurable} mapper={mapper} /></div>;
|
|
43
47
|
}
|
|
44
48
|
|
|
45
49
|
export {
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ReadWriteConfigurable } from '@remvst/configurable';
|
|
3
|
+
import { ComponentProps } from './component-props';
|
|
4
|
+
import { ComponentState } from './component-state';
|
|
5
|
+
|
|
6
|
+
export default abstract class InputComponent<
|
|
7
|
+
ConfigurableValueType,
|
|
8
|
+
ConfigurableType extends ReadWriteConfigurable<ConfigurableValueType>,
|
|
9
|
+
> extends React.Component<
|
|
10
|
+
ComponentProps<ConfigurableType>,
|
|
11
|
+
ComponentState<string>
|
|
12
|
+
> {
|
|
13
|
+
constructor(props: ComponentProps<ConfigurableType>) {
|
|
14
|
+
super(props);
|
|
15
|
+
this.state = {
|
|
16
|
+
'value': this.mapValueToInputFormat(props.configurable.read()),
|
|
17
|
+
'dirty': false,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
protected abstract mapValueToConfigurableFormat(value: string): ConfigurableValueType;
|
|
22
|
+
protected abstract mapValueToInputFormat(value: ConfigurableValueType): string;
|
|
23
|
+
|
|
24
|
+
protected onChange(event: React.ChangeEvent<HTMLInputElement>, submit: boolean = false) {
|
|
25
|
+
this.setState({'value': event.target.value, 'dirty': true}, () =>{
|
|
26
|
+
if (submit) {
|
|
27
|
+
this.submit();
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
protected onBlur(event: React.FocusEvent<HTMLInputElement>) {
|
|
33
|
+
this.submit();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
protected onKeyDown(event: React.KeyboardEvent<HTMLInputElement>) {
|
|
37
|
+
if (event.key === 'Enter') {
|
|
38
|
+
(event.target as HTMLInputElement).blur();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected submit() {
|
|
43
|
+
const { configurable } = this.props;
|
|
44
|
+
const { value } = this.state;
|
|
45
|
+
try {
|
|
46
|
+
const parsed = this.mapValueToConfigurableFormat(value);
|
|
47
|
+
configurable.write(parsed);
|
|
48
|
+
} catch (e) {
|
|
49
|
+
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.setState({'value': this.mapValueToInputFormat(configurable.read()), 'dirty': false});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected inputProperties(fastSubmit: boolean = false): React.InputHTMLAttributes<HTMLInputElement> {
|
|
56
|
+
return {
|
|
57
|
+
'className': this.state.dirty ? 'dirty' : '',
|
|
58
|
+
'value': this.state.value,
|
|
59
|
+
'onChange': (event) => this.onChange(event, fastSubmit),
|
|
60
|
+
'onBlur': (event) => this.onBlur(event),
|
|
61
|
+
'onKeyDown': (event) => this.onKeyDown(event),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -1,16 +1,42 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { NumberConfigurable } from '@remvst/configurable';
|
|
3
|
-
import
|
|
3
|
+
import InputComponent from './input-component';
|
|
4
|
+
|
|
5
|
+
export default class NumberConfigurableComponent extends InputComponent<number, NumberConfigurable> {
|
|
6
|
+
protected mapValueToConfigurableFormat(value: string) {
|
|
7
|
+
const parsed = parseFloat(value);
|
|
8
|
+
if (isNaN(parsed)) {
|
|
9
|
+
throw new Error();
|
|
10
|
+
}
|
|
11
|
+
return parsed;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected mapValueToInputFormat(value: number): string {
|
|
15
|
+
return value.toString();
|
|
16
|
+
}
|
|
4
17
|
|
|
5
|
-
export default class NumberConfigurableComponent extends React.Component<ComponentProps<NumberConfigurable>> {
|
|
6
18
|
render() {
|
|
7
19
|
const { configurable } = this.props;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
20
|
+
|
|
21
|
+
let slider;
|
|
22
|
+
if (configurable.min !== undefined && configurable.max !== undefined) {
|
|
23
|
+
slider = (<input
|
|
24
|
+
type="range"
|
|
25
|
+
min={configurable.min}
|
|
26
|
+
max={configurable.max}
|
|
27
|
+
step={configurable.step}
|
|
28
|
+
{...this.inputProperties(true)} />
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return (<div className='number-configurable'>
|
|
33
|
+
{slider}
|
|
34
|
+
<input
|
|
35
|
+
type="number"
|
|
36
|
+
step={configurable.step}
|
|
37
|
+
min={configurable.min}
|
|
38
|
+
max={configurable.max}
|
|
39
|
+
{...this.inputProperties()} />
|
|
40
|
+
</div>);
|
|
15
41
|
}
|
|
16
42
|
}
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { StringConfigurable } from '@remvst/configurable';
|
|
3
|
-
import
|
|
3
|
+
import InputComponent from './input-component';
|
|
4
|
+
|
|
5
|
+
export default class StringConfigurableComponent extends InputComponent<string, StringConfigurable> {
|
|
6
|
+
protected mapValueToConfigurableFormat(value: string): string {
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
protected mapValueToInputFormat(value: string): string {
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
4
13
|
|
|
5
|
-
export default class StringConfigurableComponent extends React.Component<ComponentProps<StringConfigurable>> {
|
|
6
14
|
render() {
|
|
7
|
-
const { configurable } = this.props;
|
|
8
15
|
return (<input
|
|
9
|
-
type="text"
|
|
10
|
-
|
|
11
|
-
onBlur={event => configurable.write(event.target.value)} />);
|
|
16
|
+
type="text"
|
|
17
|
+
{...this.inputProperties()} />);
|
|
12
18
|
}
|
|
13
19
|
}
|