Skip to content

Conversation

@Sidnioulz
Copy link
Member

@Sidnioulz Sidnioulz commented Dec 10, 2025

Closes #24316

What I did

Forced JSON mode Object control to re-render when upstream changes value, as the underlying textarea doesn't seem to want to behave in controlled mode, and we can't rely on the ref being mounted to programmatically update it.

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

I could not find a way to automate tests. I've tried setting up an ArgsTable story with a mocked Docs context and a functioning resetArgs function but did not manage.

Manual testing

  1. Go to http://localhost:6006/?path=/story/addons-docs-blocks-controls-object--object
  2. Switch the object to JSON mode
  3. Replace "Michael" with "Vanessa"
  4. Click the reset button; on this branch, the string will be reset, but not on next

Documentation

ø

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook publish.yml --field pr=<PR_NUMBER>

Summary by CodeRabbit

  • Bug Fixes
    • Fixed the JSON object editor to properly update its display when underlying data changes, ensuring the textarea reliably reflects the current content.

✏️ Tip: You can customize this high-level summary in your review settings.

@nx-cloud
Copy link

nx-cloud bot commented Dec 10, 2025

View your CI Pipeline Execution ↗ for commit 023ee26

Command Status Duration Result
nx run-many -t compile,check,knip,test,pretty-d... ✅ Succeeded 11m 10s View ↗

☁️ Nx Cloud last updated this comment at 2025-12-10 17:23:37 UTC

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 10, 2025

📝 Walkthrough

Walkthrough

Adds a memoized jsonString value to the Object control component and uses it as the key and defaultValue for the RawInput component, ensuring proper re-renders when the underlying data changes instead of relying on direct JSON stringification.

Changes

Cohort / File(s) Summary
Object control re-render optimization
code/addons/docs/src/blocks/controls/Object.tsx
Introduces memoized jsonString value computed from value object. Uses this memoized string as both the key and defaultValue for the RawInput component, replacing direct JSON.stringify(value, null, 2) usage to guarantee textarea re-renders when the underlying data changes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5–10 minutes

✨ Finishing touches
  • 📝 Generate docstrings

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
code/addons/docs/src/blocks/controls/Object.tsx (1)

210-213: Consider using null instead of '' as the fallback value.

The fallback to '' is semantically imprecise: JSON.stringify('') produces '""' rather than a representation of absence. While this computed value is never used in practice (due to the early return at line 215 when hasData is false), using null as the fallback would be more semantically correct.

Apply this diff for better semantic clarity:

-    return JSON.stringify(data ?? '', null, 2);
+    return JSON.stringify(data ?? null, null, 2);
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1f0e751 and 023ee26.

📒 Files selected for processing (1)
  • code/addons/docs/src/blocks/controls/Object.tsx (2 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
**/*.{js,jsx,json,html,ts,tsx,mjs}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Use ESLint and Prettier configurations that are enforced in the codebase

Files:

  • code/addons/docs/src/blocks/controls/Object.tsx
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Enable TypeScript strict mode

Files:

  • code/addons/docs/src/blocks/controls/Object.tsx
code/**/*.{ts,tsx,js,jsx,mjs}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

code/**/*.{ts,tsx,js,jsx,mjs}: Use server-side logger from 'storybook/internal/node-logger' for Node.js code
Use client-side logger from 'storybook/internal/client-logger' for browser code
Do not use console.log, console.warn, or console.error directly unless in isolated files where importing loggers would significantly increase bundle size

Files:

  • code/addons/docs/src/blocks/controls/Object.tsx
code/**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Export functions that need to be tested from their modules

Files:

  • code/addons/docs/src/blocks/controls/Object.tsx
code/**/*.{js,jsx,json,html,ts,tsx,mjs}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

code/**/*.{js,jsx,json,html,ts,tsx,mjs}: Run Prettier with --write flag to format code before committing
Run ESLint with yarn lint:js:cmd to check for linting issues and fix errors before committing

Files:

  • code/addons/docs/src/blocks/controls/Object.tsx
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: normal
  • GitHub Check: nx
  • GitHub Check: Core Unit Tests, windows-latest
🔇 Additional comments (1)
code/addons/docs/src/blocks/controls/Object.tsx (1)

234-235: The uncontrolled component reset pattern using jsonString as key and defaultValue is sound.

The implementation correctly handles the reset behavior by leveraging React's key-based remounting. When the parent value prop changes, the derived jsonString changes, triggering a remount of the textarea with the new defaultValue. This ensures the component reflects external resets immediately while allowing user edits to accumulate in the uncontrolled textarea state until blur.

The pattern handles edge cases correctly:

  • User edits while reset occurs are discarded (expected behavior for a reset)
  • Mode switching between raw JSON and tree view works correctly since the key ensures clean state transitions
  • Multiple rapid resets are efficiently handled by React's reconciliation
  • External value updates while focused are captured on the next blur via updateRaw
  • Performance is optimized with memoized computations (jsonString, updateRaw, onForceVisible)

This intentionally differs from the controlled textarea approach used in the Text control, allowing JSON edits to be batched on blur rather than updated on every keystroke. The choice to use an uncontrolled component here is appropriate given the need to debounce edits and provide validation feedback before committing changes.

@valentinpalkovic valentinpalkovic merged commit 63585ce into next Dec 10, 2025
70 of 75 checks passed
@valentinpalkovic valentinpalkovic deleted the sidnioulz/issue-24316 branch December 10, 2025 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Controls are not resettable if 'Reset control’ is pressed in RAW object opened mode

3 participants