From 7b01cba045f535ee507adb5470e2aa9ed2baa109 Mon Sep 17 00:00:00 2001 From: Aaron_Zhou Date: Wed, 4 Nov 2026 17:18:21 +0800 Subject: [PATCH] fix: preserve pending collapse on parent hover --- src/js/components/DataTypes/Object.js | 9 +- .../js/components/DataTypes/Object-test.js | 98 +++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/src/js/components/DataTypes/Object.js b/src/js/components/DataTypes/Object.js index d4aab601..77ec71da 100644 --- a/src/js/components/DataTypes/Object.js +++ b/src/js/components/DataTypes/Object.js @@ -67,11 +67,18 @@ class RjvObject extends React.PureComponent { static getDerivedStateFromProps (nextProps, prevState) { const { prevProps } = prevState + // Parent renders create new arrays for the same path. Preserve pending toggles. + const namespaceChanged = + nextProps.namespace !== prevProps.namespace && + (!prevProps.namespace || + nextProps.namespace.length !== prevProps.namespace.length || + nextProps.namespace.some((name, i) => name !== prevProps.namespace[i])) if ( nextProps.src !== prevProps.src || nextProps.collapsed !== prevProps.collapsed || + nextProps.shouldCollapse !== prevProps.shouldCollapse || nextProps.name !== prevProps.name || - nextProps.namespace !== prevProps.namespace || + namespaceChanged || nextProps.rjvId !== prevProps.rjvId ) { const newState = RjvObject.getState(nextProps) diff --git a/test/tests/js/components/DataTypes/Object-test.js b/test/tests/js/components/DataTypes/Object-test.js index fcc9beac..b2a00ea7 100644 --- a/test/tests/js/components/DataTypes/Object-test.js +++ b/test/tests/js/components/DataTypes/Object-test.js @@ -1,4 +1,5 @@ import React from 'react' +import { unstable_batchedUpdates as batchedUpdates } from 'react-dom' import { shallow, render, mount } from 'enzyme' import { expect } from 'chai' @@ -7,6 +8,103 @@ import JsonObject from './../../../../../src/js/components/DataTypes/Object' describe('', function () { const rjvId = 1 + for (const src of [{ branch: { leaf: true } }, { branch: [true] }]) { + const type = Array.isArray(src.branch) ? 'array' : 'object' + it(`preserves ${type} collapse during parent hover`, function () { + const wrapper = mount( + + ) + try { + const branch = () => wrapper.find(JsonObject).filter({ name: 'branch' }) + expect(branch().find('.object-content')).to.have.length(1) + + batchedUpdates(() => { + wrapper.find('.object-key-val').first().simulate('mouseEnter') + branch().find('.icon-container').first().simulate('click') + }) + wrapper.update() + + expect(branch().find('.object-content')).to.have.length(0) + expect(branch().find('.collapsed-icon').hostNodes()).to.have.length(1) + branch().find('.icon-container').first().simulate('click') + expect(branch().find('.object-content')).to.have.length(1) + } finally { + wrapper.unmount() + } + }) + } + + for (const namespace of [['other', 'branch'], ['root', 'nested', 'branch']]) { + it(`restores cached state for ${namespace.join('.')}`, function () { + const src = { leaf: true } + const wrapper = mount( + + ) + try { + wrapper.find('.icon-container').first().simulate('click') + expect(wrapper.find('.object-content')).to.have.length(0) + + wrapper.setProps({ namespace: ['root', 'branch'], src: { ...src } }) + expect(wrapper.find('.object-content')).to.have.length(0) + + wrapper.setProps({ namespace }) + expect(wrapper.find('.object-content')).to.have.length(1) + + wrapper.setProps({ namespace: ['root', 'branch'] }) + expect(wrapper.find('.object-content')).to.have.length(0) + } finally { + wrapper.unmount() + } + }) + } + + it('updates the collapse rule for the same path', function () { + const wrapper = mount( + + ) + try { + expect(wrapper.find('.object-content')).to.have.length(1) + wrapper.setProps({ + namespace: ['collapse-rule'], + shouldCollapse: () => true + }) + expect(wrapper.find('.object-content')).to.have.length(0) + wrapper.setProps({ + namespace: ['collapse-rule'], + shouldCollapse: () => false + }) + expect(wrapper.find('.object-content')).to.have.length(1) + } finally { + wrapper.unmount() + } + }) + it('Object component should have a data type label', function () { const src = { test: true