refactor: flat onChange

This commit is contained in:
cha0s 2020-06-22 05:47:28 -05:00
parent 6b52c18a0d
commit abb7420ce7
11 changed files with 31 additions and 47 deletions

View File

@ -13,7 +13,7 @@ const decorate = compose(
const Actions = ({ const Actions = ({
context, context,
dispatchers, onChange = () => {},
value, value,
}) => ( }) => (
<div className="actions"> <div className="actions">
@ -24,7 +24,7 @@ const Actions = ({
(traversal, i) => ( (traversal, i) => (
// eslint-disable-next-line react/no-array-index-key // eslint-disable-next-line react/no-array-index-key
<li key={i}> <li key={i}>
<Value.Component context={context} dispatchers={dispatchers} value={traversal} /> <Value.Component context={context} onChange={onChange} value={traversal} />
</li> </li>
), ),
) )

View File

@ -4,9 +4,7 @@ import React from 'react';
import propTypes from './prop-types'; import propTypes from './prop-types';
const Bool = ({ const Bool = ({
dispatchers: { onChange = () => {},
onChange = () => {},
} = {},
value, value,
}) => ( }) => (
<select onChange={(event) => onChange(!!event.target.value, event)} value={value ? '1' : ''}> <select onChange={(event) => onChange(!!event.target.value, event)} value={value ? '1' : ''}>

View File

@ -23,11 +23,11 @@ const decorate = compose(
const Condition = ({ const Condition = ({
context, context,
dispatchers: parentDispatchers, onChange: parentOnChange = () => {},
value, value,
}) => { }) => {
const onChange = (change) => { const onChange = (change) => {
parentDispatchers.onChange({ parentOnChange({
...value, ...value,
...change, ...change,
}); });
@ -41,15 +41,13 @@ const Condition = ({
...operands.slice(index + 1), ...operands.slice(index + 1),
]; ];
}; };
const makeDispatchers = (index) => ({ const makeOnChange = (index) => (newValue, event) => {
onChange: (newValue, event) => { onChange(
onChange( operatorRef.current === event.target
operatorRef.current === event.target ? {operator: newValue}
? {operator: newValue} : {operands: mergeToOperand(index, newValue)},
: {operands: mergeToOperand(index, newValue)}, );
); };
},
});
return ( return (
<span className="condition"> <span className="condition">
{ {
@ -63,13 +61,13 @@ const Condition = ({
case '<=': { case '<=': {
return value.operands.slice(1).reduce( return value.operands.slice(1).reduce(
(r, operand, i) => { (r, operand, i) => {
const opDispatchers = makeDispatchers(i + 1); const opOnChange = makeOnChange(i);
return ( return (
<> <>
{r} {r}
<select <select
className="operator" className="operator"
onChange={(event) => opDispatchers.onChange(event.target.value, event)} onChange={(event) => opOnChange(event.target.value, event)}
ref={operatorRef} ref={operatorRef}
value={value.operator} value={value.operator}
> >
@ -78,7 +76,7 @@ const Condition = ({
<span className="operand"> <span className="operand">
<Value.Component <Value.Component
context={context} context={context}
dispatchers={opDispatchers} onChange={opOnChange}
value={operand} value={operand}
/> />
</span> </span>
@ -89,7 +87,7 @@ const Condition = ({
<span className="operand"> <span className="operand">
<Value.Component <Value.Component
context={context} context={context}
dispatchers={makeDispatchers(0)} onChange={makeOnChange(0)}
value={value.operands[0]} value={value.operands[0]}
/> />
</span> </span>

View File

@ -13,7 +13,7 @@ const decorate = compose(
const Literal = ({ const Literal = ({
context, context,
dispatchers, onChange = () => {},
value, value,
}) => { }) => {
const typeRenderers = useTypeRenderers(); const typeRenderers = useTypeRenderers();
@ -30,7 +30,7 @@ const Literal = ({
.map((option) => <option key={option}>{option}</option>) .map((option) => <option key={option}>{option}</option>)
} }
</select> </select>
{Component ? <Component dispatchers={dispatchers} value={value.value} /> : null} {Component ? <Component onChange={onChange} value={value.value} /> : null}
</div> </div>
); );
}; };

View File

@ -10,9 +10,7 @@ const decorate = compose(
); );
const Number = ({ const Number = ({
dispatchers: { onChange = () => {},
onChange = () => {},
} = {},
options, options,
value, value,
}) => { }) => {

View File

@ -12,7 +12,7 @@ const decorate = compose(
const Routines = ({ const Routines = ({
context, context,
dispatchers, onChange = () => {},
value, value,
}) => { }) => {
const entries = value.routines ? Object.entries(value.routines) : []; const entries = value.routines ? Object.entries(value.routines) : [];
@ -28,7 +28,7 @@ const Routines = ({
<span className="text">{name}</span> <span className="text">{name}</span>
<Actions.Component <Actions.Component
context={context} context={context}
dispatchers={dispatchers} onChange={onChange}
value={routine.routine} value={routine.routine}
/> />
</label> </label>

View File

@ -4,9 +4,7 @@ import React from 'react';
import propTypes from './prop-types'; import propTypes from './prop-types';
const String = ({ const String = ({
dispatchers: { onChange = () => {},
onChange = () => {},
} = {},
options, options,
value, value,
}) => ( }) => (

View File

@ -14,7 +14,7 @@ const decorate = compose(
const Traversal = (props) => { const Traversal = (props) => {
const { const {
context, context,
dispatchers, onChange = () => {},
value: {steps, value}, value: {steps, value},
} = props; } = props;
const stepsType = typeFromSteps(context, steps); const stepsType = typeFromSteps(context, steps);
@ -50,7 +50,7 @@ const Traversal = (props) => {
{arg.label} {arg.label}
<Value.Component <Value.Component
context={context} context={context}
dispatchers={dispatchers} onChange={onChange}
value={arg} value={arg}
/> />
</label> </label>
@ -72,7 +72,7 @@ const Traversal = (props) => {
<span className="op">=</span> <span className="op">=</span>
<Value.Component <Value.Component
context={context} context={context}
dispatchers={dispatchers} onChange={onChange}
value={value} value={value}
/> />
</span> </span>

View File

@ -11,23 +11,17 @@ const decorate = compose(
); );
const Vector = ({ const Vector = ({
dispatchers: { onChange = () => {},
onChange = () => {},
} = {},
value, value,
}) => ( }) => (
<span className="vector"> <span className="vector">
<Number.Component <Number.Component
dispatchers={{ onChange={(axe) => onChange([axe, value[1]])}
onChange: (axe) => onChange([axe, value[1]]),
}}
value={value[0]} value={value[0]}
/> />
<span className="separator">{' x '}</span> <span className="separator">{' x '}</span>
<Number.Component <Number.Component
dispatchers={{ onChange={(axe) => onChange([value[0], axe])}
onChange: (axe) => onChange([value[0], axe]),
}}
value={value[1]} value={value[1]}
/> />
</span> </span>

View File

@ -14,7 +14,7 @@ const decorate = compose(
const Value = ({ const Value = ({
context, context,
dispatchers, onChange = () => {},
options, options,
type: typeHint, type: typeHint,
value, value,
@ -36,7 +36,7 @@ const Value = ({
? ( ? (
<Component <Component
context={context} context={context}
dispatchers={dispatchers} onChange={onChange}
options={options} options={options}
type={type} type={type}
value={value} value={value}

View File

@ -49,9 +49,7 @@ const makeTraitPaneRenderer = (context, dispatch, target, traitType) => (trait)
<div className="invisible-separator" /> <div className="invisible-separator" />
<Value.Component <Value.Component
context={context} context={context}
dispatchers={{ onChange={(value) => dispatch(setTraitProperty({...payload, value}))}
onChange: (value) => dispatch(setTraitProperty({...payload, value})),
}}
options={options} options={options}
type={type} type={type}
value={values[key]} value={values[key]}