@remvst/configurable-react 1.1.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.
@@ -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", defaultChecked: configurable.read(), onChange: (event) => configurable.write(event.target.checked) }));
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
- import React from 'react';
1
+ /// <reference types="react" />
2
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>> {
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
- function leftPad(s, pad, length) {
9
- while (s.length < length) {
10
- s = pad + s;
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
- 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
+ return (react_1.default.createElement("input", { type: "color", ...this.inputProperties() }));
20
21
  }
21
22
  }
22
23
  exports.default = ColorConfigurableComponent;
@@ -0,0 +1,4 @@
1
+ export interface ComponentState<T> {
2
+ readonly value: T;
3
+ readonly dirty: boolean;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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
- render() {
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 (react_1.default.createElement("select", { defaultValue: defaultValue, onChange: event => configurable.write(mapping.get(event.target.value)) }, Array.from(configurable.categories).map((category) => {
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
  })));
package/lib/index.js CHANGED
@@ -41,11 +41,11 @@ function defaultComponents(configurable, mapper) {
41
41
  if (configurable instanceof configurable_1.ButtonConfigurable) {
42
42
  return (react_1.default.createElement(button_configurable_component_1.default, { configurable: configurable }));
43
43
  }
44
- console.log(configurable_1.GroupConfigurable, configurable.constructor, configurable_1.GroupConfigurable === configurable.constructor);
45
44
  throw new Error(`Unrecognized component type ${configurable.constructor.name}`);
46
45
  }
47
46
  exports.defaultComponents = defaultComponents;
48
47
  function configurableToComponents(configurable, mapper = defaultComponents) {
49
- return react_1.default.createElement(invalidatable_component_1.default, { configurable: configurable, mapper: mapper });
48
+ return react_1.default.createElement("div", { className: 'configurable' },
49
+ react_1.default.createElement(invalidatable_component_1.default, { configurable: configurable, mapper: mapper }));
50
50
  }
51
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
- import React from 'react';
1
+ /// <reference types="react" />
2
2
  import { NumberConfigurable } from '@remvst/configurable';
3
- import { ComponentProps } from './component-props';
4
- export default class NumberConfigurableComponent extends React.Component<ComponentProps<NumberConfigurable>> {
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
- class NumberConfigurableComponent extends react_1.default.Component {
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
- 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)) }));
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
- import React from 'react';
1
+ /// <reference types="react" />
2
2
  import { StringConfigurable } from '@remvst/configurable';
3
- import { ComponentProps } from './component-props';
4
- export default class StringConfigurableComponent extends React.Component<ComponentProps<StringConfigurable>> {
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
- class StringConfigurableComponent extends react_1.default.Component {
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
- const { configurable } = this.props;
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.1.0",
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.2.0",
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
- defaultChecked={configurable.read()}
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 { ComponentProps } from './component-props';
3
+ import InputComponent from './input-component';
4
4
 
5
- export function leftPad(s: string, pad: string, length: number): string {
6
- while (s.length < length) {
7
- s = pad + s;
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
- defaultValue={colorAsHex}
19
- onBlur={event => configurable.write(parseInt(event.target.value.slice(1), 16))} />);
21
+ {...this.inputProperties()} />);
20
22
  }
21
23
  }
@@ -0,0 +1,4 @@
1
+ export interface ComponentState<T> {
2
+ readonly value: T;
3
+ readonly dirty: boolean;
4
+ }
@@ -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
- render() {
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
- const value = configurable.read();
11
- let defaultValue = '';
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
- if (item.value === value) {
18
- defaultValue = item.key;
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={event => configurable.write(mapping.get(event.target.value))}>
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) => {
package/src/index.tsx CHANGED
@@ -36,7 +36,6 @@ export function defaultComponents(configurable: Configurable, mapper: Configurab
36
36
  if (configurable instanceof ButtonConfigurable) {
37
37
  return (<ButtonConfigurableComponent configurable={configurable} />);
38
38
  }
39
- console.log(GroupConfigurable, configurable.constructor, GroupConfigurable === configurable.constructor)
40
39
  throw new Error(`Unrecognized component type ${configurable.constructor.name}`);
41
40
  }
42
41
 
@@ -44,7 +43,7 @@ export function configurableToComponents(
44
43
  configurable: () => Configurable,
45
44
  mapper: ConfigurableToComponent = defaultComponents,
46
45
  ) {
47
- return <InvalidatableComponent configurable={configurable} mapper={mapper} />;
46
+ return <div className='configurable'><InvalidatableComponent configurable={configurable} mapper={mapper} /></div>;
48
47
  }
49
48
 
50
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 { ComponentProps } from './component-props';
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
- 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))} />);
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 { ComponentProps } from './component-props';
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
- defaultValue={configurable.read()}
11
- onBlur={event => configurable.write(event.target.value)} />);
16
+ type="text"
17
+ {...this.inputProperties()} />);
12
18
  }
13
19
  }