Skip to content

Commit

Permalink
Synchronize constraint values with list options
Browse files Browse the repository at this point in the history
  • Loading branch information
cocopon committed Jan 15, 2023
1 parent 409ae7f commit cff2710
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 46 deletions.
13 changes: 6 additions & 7 deletions packages/core/src/common/controller/list-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {ListConstraint} from '../constraint/list';
import {ValueMap} from '../model/value-map';
import {createValue} from '../model/values';
import {ViewProps} from '../model/view-props';
import {findListItems} from '../util';
import {ListController} from './list';

describe(ListController.name, () => {
Expand All @@ -22,8 +21,8 @@ describe(ListController.name, () => {
});
const doc = createTestWindow().document;
const c = new ListController(doc, {
props: ValueMap.fromObject({
options: findListItems(constraint) ?? [],
props: new ValueMap({
options: constraint.values.value('options'),
}),
value: value,
viewProps: ViewProps.create(),
Expand All @@ -44,8 +43,8 @@ describe(ListController.name, () => {
const win = createTestWindow();
const doc = win.document;
const c = new ListController(doc, {
props: ValueMap.fromObject({
options: findListItems(constraint) ?? [],
props: new ValueMap({
options: constraint.values.value('options'),
}),
value: value,
viewProps: ViewProps.create(),
Expand All @@ -68,8 +67,8 @@ describe(ListController.name, () => {
});
const doc = createTestWindow().document;
const c = new ListController(doc, {
props: ValueMap.fromObject({
options: findListItems(constraint) ?? [],
props: new ValueMap({
options: constraint.values.value('options'),
}),
value: value,
viewProps: ViewProps.create(),
Expand Down
16 changes: 0 additions & 16 deletions packages/core/src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,6 @@ export function createListConstraint<T>(
: null;
}

/**
* @hidden
*/
export function findListItems<T>(
constraint: Constraint<T> | undefined,
): ListItem<T>[] | null {
const c = constraint
? findConstraint<ListConstraint<T>>(constraint, ListConstraint)
: null;
if (!c) {
return null;
}

return c.options;
}

function findStep(constraint: Constraint<number> | undefined): number | null {
const c = constraint ? findConstraint(constraint, StepConstraint) : null;
if (!c) {
Expand Down
13 changes: 5 additions & 8 deletions packages/core/src/input-binding/boolean/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import {ValueMap} from '../../common/model/value-map';
import {BaseInputParams, ListParamsOptions} from '../../common/params';
import {ParamsParsers, parseParams} from '../../common/params-parsers';
import {writePrimitive} from '../../common/primitive';
import {
createListConstraint,
findListItems,
parseListOptions,
} from '../../common/util';
import {createListConstraint, parseListOptions} from '../../common/util';
import {InputBindingPlugin} from '../plugin';
import {CheckboxController} from './controller/checkbox';

Expand Down Expand Up @@ -68,10 +64,11 @@ export const BooleanInputPlugin: InputBindingPlugin<
const value = args.value;
const c = args.constraint;

if (c && findConstraint(c, ListConstraint)) {
const lc = c && findConstraint<ListConstraint<boolean>>(c, ListConstraint);
if (lc) {
return new ListController(doc, {
props: ValueMap.fromObject({
options: findListItems(c) ?? [],
props: new ValueMap({
options: lc.values.value('options'),
}),
value: value,
viewProps: args.viewProps,
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/input-binding/number/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
import {writePrimitive} from '../../common/primitive';
import {
createListConstraint,
findListItems,
getBaseStep,
getSuitableDecimalDigits,
getSuitableDraggingScale,
Expand Down Expand Up @@ -164,10 +163,11 @@ export const NumberInputPlugin: InputBindingPlugin<
const value = args.value;
const c = args.constraint;

if (c && findConstraint(c, ListConstraint)) {
const lc = c && findConstraint<ListConstraint<number>>(c, ListConstraint);
if (lc) {
return new ListController(args.document, {
props: ValueMap.fromObject({
options: findListItems(c) ?? [],
props: new ValueMap({
options: lc.values.value('options'),
}),
value: value,
viewProps: args.viewProps,
Expand Down
13 changes: 5 additions & 8 deletions packages/core/src/input-binding/string/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import {ValueMap} from '../../common/model/value-map';
import {BaseInputParams, ListParamsOptions} from '../../common/params';
import {ParamsParsers, parseParams} from '../../common/params-parsers';
import {writePrimitive} from '../../common/primitive';
import {
createListConstraint,
findListItems,
parseListOptions,
} from '../../common/util';
import {createListConstraint, parseListOptions} from '../../common/util';
import {InputBindingPlugin} from '../plugin';

export interface StringInputParams extends BaseInputParams {
Expand Down Expand Up @@ -68,10 +64,11 @@ export const StringInputPlugin: InputBindingPlugin<
const value = args.value;
const c = args.constraint;

if (c && findConstraint(c, ListConstraint)) {
const lc = c && findConstraint<ListConstraint<string>>(c, ListConstraint);
if (lc) {
return new ListController(doc, {
props: ValueMap.fromObject({
options: findListItems(c) ?? [],
props: new ValueMap({
options: lc.values.value('options'),
}),
value: value,
viewProps: args.viewProps,
Expand Down
13 changes: 10 additions & 3 deletions packages/tweakpane/src/main/ts/blade/list/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BladePlugin,
createValue,
LabeledValueController,
ListConstraint,
ListController,
ListParamsOptions,
normalizeListOptions,
Expand Down Expand Up @@ -41,11 +42,17 @@ export const ListBladePlugin = (function <T>(): BladePlugin<
return result ? {params: result} : null;
},
controller(args) {
const lc = new ListConstraint(
normalizeListOptions<T>(args.params.options),
);
const value = createValue(args.params.value, {
constraint: lc,
});
const ic = new ListController(args.document, {
props: ValueMap.fromObject({
options: normalizeListOptions<T>(args.params.options),
props: new ValueMap({
options: lc.values.value('options'),
}),
value: createValue(args.params.value),
value: value,
viewProps: args.viewProps,
});
return new LabeledValueController<T, ListController<T>>(args.document, {
Expand Down

0 comments on commit cff2710

Please sign in to comment.