Files
webstudio/apps/builder/app/builder/shared/bindable-expression.tsx
T
Oleg Isonen d8491ea736 fix: Make bound fields read-only (#6028)
Closes #6027

## Summary

Bound values currently remain editable when the expression directly
references a mutable variable. Editing the field writes a temporary
runtime override while leaving the binding in place, which makes the
source of truth unclear and behaves differently across Builder fields.

This change makes every expression-bound field read-only. To enter a
literal value, remove the binding first.

## Implementation

- Simplify binding state to two states: unbound/editable and
bound/read-only.
- Remove temporary data-variable override writes and the
overwritten/reset binding UI.
- Remove `allowBindingOverwrite` and bound-value parsing APIs and their
call-site exceptions.
- Guard pending draft saves so a draft started before binding cannot
replace the new binding.
- Add query-builder regression coverage for string and numeric filters
bound to mutable variables.

## Checklist

- [x] Add regression tests and confirm they fail with the previous
behavior.
- [x] Make all expression-bound controls read-only.
- [x] Remove temporary override state and UI.
- [x] Verify literal controls remain editable.
- [x] Run the full Builder unit test suite.
- [x] Run Builder typecheck and repository lint.
- [x] Review documentation impact: no documentation change is needed;
this makes the existing remove-binding-before-edit interaction
consistent.
- [x] Review fixture impact: no fixture inputs or generated fixture
outputs are affected.

## Verification

- `pnpm --filter @webstudio-is/builder test` — 2,509 tests passed across
217 files.
- `pnpm --filter @webstudio-is/builder typecheck` — passed.
- `pnpm lint` — passed.
- Agent evaluations were not run because they require separate explicit
approval and this change does not modify agent-facing behavior.
2026-08-07 12:34:11 +02:00

102 lines
2.4 KiB
TypeScript

import type { ReactNode } from "react";
import {
BindingControl,
BindingPopover,
type BindingVariant,
} from "./binding-popover";
export type BindingState = {
overwritable: boolean;
variant: BindingVariant;
};
export const useBindingState = (expression: string | undefined) => {
return expression === undefined
? ({ overwritable: true, variant: "default" } satisfies BindingState)
: ({ overwritable: false, variant: "bound" } satisfies BindingState);
};
export const BindableExpressionControl = <Value,>({
expression,
value,
bound,
scope,
aliases,
validate,
showBinding = true,
renderControl,
onChangeValue,
onChangeExpression,
onRemove,
bindingState,
}: {
expression: string;
value: Value;
bound: boolean;
scope: Record<string, unknown>;
aliases: Map<string, string>;
validate?: (value: unknown) => string | undefined;
showBinding?: boolean;
renderControl: (props: {
value: Value;
readOnly: boolean;
onChangeValue: (value: Value) => void;
}) => ReactNode;
onChangeValue: (value: Value) => void;
onChangeExpression: (expression: string) => void;
onRemove?: (evaluatedValue: unknown) => void;
bindingState?: BindingState;
}) => {
if (bindingState === undefined) {
return (
<BindableExpressionControlWithState
expression={expression}
value={value}
bound={bound}
scope={scope}
aliases={aliases}
validate={validate}
showBinding={showBinding}
renderControl={renderControl}
onChangeValue={onChangeValue}
onChangeExpression={onChangeExpression}
onRemove={onRemove}
/>
);
}
const { overwritable, variant } = bindingState;
return (
<BindingControl>
{renderControl({
value,
readOnly: overwritable === false,
onChangeValue,
})}
{showBinding && (
<BindingPopover
scope={scope}
aliases={aliases}
validate={validate}
variant={variant}
value={expression}
onChange={onChangeExpression}
onRemove={onRemove}
/>
)}
</BindingControl>
);
};
const BindableExpressionControlWithState = <Value,>(
props: Omit<
Parameters<typeof BindableExpressionControl<Value>>[0],
"bindingState"
>
) => {
const bindingState = useBindingState(
props.bound ? props.expression : undefined
);
return <BindableExpressionControl {...props} bindingState={bindingState} />;
};