Commit a layer rename once on Enter or blur (#2396)

Fixes #1269.

Renaming a layer ran the whole rename on every keystroke:
`onLayerIdChange` updated the style, which was validated, saved to
localStorage and pushed to the map for each intermediate ID. The layer
editor's ID field now keeps the draft in the input and commits it on
Enter or blur, which is how `InputString` already behaves for other
string fields. `FieldId` gets a `commitOnBlur` prop for this; the Add
Layer dialog doesn't pass it, so it still updates its draft ID as you
type.

The issue suggests debouncing. I went with commit on Enter/blur instead
because a debounced rename would still apply half-typed IDs whenever
typing pauses, and each of those is a real rename of the layer in the
style.

No visual change: the field looks the same, the new ID is just applied
when you leave the field.

**Performance:** I typed a 20-character ID into the layer ID field with
Playwright (`keyboard.type`, then Tab), 5 runs each in Chromium on the
dev server, counting style writes to localStorage:

| | Style saves per rename | Time to type the 20 characters (median) |
|---|---|---|
| `main` | 20 | 2105 ms |
| this PR | 1 | 92 ms |

The timings are from my Windows laptop with an e2e run going in the
background, so only the ratio matters.

## Launch Checklist

 - [x] Briefly describe the changes in this PR.
 - [x] Link to related issues: fixes #1269.
- [x] Include before/after visuals or gifs if this PR includes visual
changes: no visual change.
- [x] Write tests for all new functionality: the `id` test in
`layer-editor.spec.ts` now checks that the saved style still has the old
ID after typing, before the field loses focus. That check fails on
`main`.
 - [x] Add an entry to `CHANGELOG.md` under the `## main` section.

Tested locally: `npm run lint` clean, `npx tsc --noEmit` clean, `npx
vitest run` (57 passed) and `npx playwright test
e2e/layer-editor.spec.ts` (52 passed). The updated `id` test fails when
I run it against `main`. I didn't run the full e2e suite locally (it
times out on Windows with several workers); it passes in CI on this PR.

Assisted-By: Claude Opus 5.5 (Claude Code). It helped write the change,
the test and this description. I've reviewed and tested the change and
can answer questions about it.
This commit is contained in:
SHAIK KHADEER BASHA authored and GitHub committed 2026-12-04 10:37:57 +02:00
1 parent 1f90a7de65
commit 3e6e558d63
4 files changed
+10 -1

No files matched your search

+1
View File
@@ -15,6 +15,7 @@
- _...Add new stuff here..._
### 🐞 Bug fixes
- Apply layer renames when pressing Enter or leaving the ID field to avoid validating and saving the style on every keystroke.
- Add distinct screen reader labels for the anchor, X offset, and Y offset in each variable anchor offset row.
- Open `icon-padding` expressions in the expression editor so they can be viewed and edited.
- Fix `icon-padding` showing four inputs and only saving once all four were filled; it now uses the same add/remove list as other number arrays, up to four values
+5
View File
@@ -45,6 +45,11 @@ describe("layer editor", () => {
const id = uuid();
await when.setValue("layer-editor.layer-id.input", "foobar:" + id);
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({
layers: [{ id: "background:" + bgId, type: "background" }],
});
await when.click("min-zoom");
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({
+3 -1
View File
@@ -8,6 +8,7 @@ type FieldIdProps = {
wdKey: string
onChange(value: string | undefined): unknown
error?: {message: string}
commitOnBlur?: boolean
};
export const FieldId: React.FC<FieldIdProps> = (props) => {
@@ -18,7 +19,8 @@ export const FieldId: React.FC<FieldIdProps> = (props) => {
>
<InputString
value={props.value}
onInput={props.onChange}
onInput={props.commitOnBlur ? undefined : props.onChange}
onChange={props.commitOnBlur ? props.onChange : undefined}
data-wd-key={props.wdKey + ".input"}
/>
</Block>
+1
View File
@@ -218,6 +218,7 @@ class LayerEditorInternal extends React.Component<LayerEditorInternalProps, Laye
value={this.props.layer.id}
wdKey="layer-editor.layer-id"
error={errorData.id}
commitOnBlur
onChange={newId => this.props.onLayerIdChange(this.props.layerIndex, this.props.layer.id, newId)}
/>
<FieldType