Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 16, 2025

Problem

When users edited textboxes in the search panel, React would lose cursor position or experience other controlled input issues. This was caused by improper state mutation in the search dialog component.

Root Cause

The updateExpression, addExpression, and deleteExpression methods in packages/sanddance-explorer/src/dialogs/search.tsx were violating React's immutability principle:

  1. Only the top-level groups array was being shallow-copied
  2. Nested group and expressions objects were being directly mutated from state
  3. The updateExpression method used unnecessary deep cloning with SandDance.VegaMorphCharts.util.clone()

Example of the problematic pattern:

// Before - Mutating state directly
const groups = [...this.state.groups];
const group = groups[groupIndex];  // Reference to old state
group.expressions[index] = newExpression;  // Direct mutation ❌
this.setState({ groups });

This caused React's reconciliation to behave unpredictably with controlled inputs, resulting in cursor position loss and focus issues.

Solution

All three methods now properly implement immutable state updates at every nesting level:

// After - Proper immutability
const groups = [...this.state.groups];
const group = { ...groups[groupIndex] };  // New group object
const expressions = [...group.expressions];  // New expressions array
expressions[index] = { ...currentEx, ...partialEx };  // New expression object
group.expressions = expressions;
groups[groupIndex] = group;
this.setState({ groups });  // ✅

Changes

  • updateExpression: Replaced deep clone with spread operators and added proper immutability for all nested objects
  • addExpression: Added proper group and expressions array copying before modifications
  • deleteExpression: Added proper group object copying before modifications

Benefits

  • ✅ Fixes cursor position loss in search panel textboxes
  • ✅ Proper React immutability patterns throughout
  • ✅ Better performance (avoiding unnecessary deep clones)
  • ✅ More maintainable and predictable code
  • ✅ All existing functionality preserved

Testing

  • All builds pass
  • All linting passes
  • All existing tests pass
  • No breaking changes to functionality
Original prompt

There a react bug when user edits textbox in the search panel


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 16, 2025 21:24
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix react bug in search panel textbox editing Fix React state mutation bug causing textbox issues in search panel Oct 16, 2025
Copilot AI requested a review from danmarshall October 16, 2025 21:29
Copilot finished work on behalf of danmarshall October 16, 2025 21:29
@danmarshall danmarshall marked this pull request as ready for review October 17, 2025 03:34
@danmarshall danmarshall reopened this Oct 17, 2025
@danmarshall danmarshall merged commit dae445e into main Oct 17, 2025
7 checks passed
@danmarshall danmarshall deleted the copilot/fix-search-panel-textbox-bug branch October 17, 2025 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants