-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure layer tree consistently shows selected item
The expansion state of the LayerTree component is based on the pathData data, which is set by components mounting and calling `registerPath`. However, the pathData object contained selectors with hardcoded indexes for where each item was in their zone. If the item moved, the pathData wouldn't be updated. This was noticed as the LayerTree would sometimes avoid expanding the parent area (i.e. Columns in the demo application) of a selected item because it couldn't find it. This would not occur for components in the default-zone. This fix changes the underlying implementation of pathData, going from storing the selectors to the zoneCompound IDs (i.e. `[MyComponent:zone1,MyComponent:zone2]`), and calulating the selectors dynamically when necessary through a new `use-breadcrumbs` hook. This hook isn't required for the LayerTree component, which relies on `is-child-of-zone`. `is-child-of-zone` can now easily determine whether or not the item is a child by checking for the presence of an area in the pathData. This change also adds test coverage.
- Loading branch information
Showing
10 changed files
with
273 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { DropZoneContext } from "../../components/DropZone/context"; | ||
import { Config, Data } from "../../types/Config"; | ||
import { isChildOfZone } from "../is-child-of-zone"; | ||
|
||
const item1 = { type: "MyComponent", props: { id: "MyComponent-1" } }; | ||
const item2 = { type: "MyComponent", props: { id: "MyComponent-2" } }; | ||
const item3 = { type: "MyComponent", props: { id: "MyComponent-3" } }; | ||
|
||
const data: Data = { | ||
root: { title: "" }, | ||
content: [item1], | ||
zones: { | ||
"MyComponent-1:zone": [item2], | ||
"MyComponent-2:zone": [item3], | ||
}, | ||
}; | ||
|
||
const config: Config = { | ||
components: { | ||
Comp: { | ||
defaultProps: { prop: "example" }, | ||
render: () => <div />, | ||
}, | ||
}, | ||
}; | ||
|
||
const dropzoneContext: DropZoneContext = { | ||
data, | ||
config, | ||
pathData: { | ||
"MyComponent-1": { path: [], label: "MyComponent" }, | ||
"MyComponent-2": { path: ["MyComponent-1:zone"], label: "MyComponent" }, | ||
"MyComponent-3": { | ||
path: ["MyComponent-1:zone", "MyComponent-2:zone"], | ||
label: "MyComponent", | ||
}, | ||
}, | ||
}; | ||
|
||
describe("is-child-of-zone", () => { | ||
it("should return true when item is child of zone for first item", () => { | ||
expect(isChildOfZone(item1, item2, dropzoneContext)).toBe(true); | ||
}); | ||
|
||
it("should return true when item is child of zone for second item", () => { | ||
expect(isChildOfZone(item2, item3, dropzoneContext)).toBe(true); | ||
}); | ||
|
||
it("should return false when item is not child of zone", () => { | ||
expect(isChildOfZone(item2, item1, dropzoneContext)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { DropZoneContext } from "../../components/DropZone/context"; | ||
import { Config, Data } from "../../types/Config"; | ||
import { Breadcrumb, convertPathDataToBreadcrumbs } from "../use-breadcrumbs"; | ||
|
||
const item1 = { type: "MyComponent", props: { id: "MyComponent-1" } }; | ||
const item2 = { type: "MyComponent", props: { id: "MyComponent-2" } }; | ||
const item3 = { type: "MyComponent", props: { id: "MyComponent-3" } }; | ||
|
||
const data: Data = { | ||
root: { title: "" }, | ||
content: [item1], | ||
zones: { | ||
"MyComponent-1:zone": [item2], | ||
"MyComponent-2:zone": [item3], | ||
}, | ||
}; | ||
|
||
const config: Config = { | ||
components: { | ||
Comp: { | ||
defaultProps: { prop: "example" }, | ||
render: () => <div />, | ||
}, | ||
}, | ||
}; | ||
|
||
const dropzoneContext: DropZoneContext = { | ||
data, | ||
config, | ||
pathData: { | ||
"MyComponent-1": { path: [], label: "MyComponent" }, | ||
"MyComponent-2": { path: ["MyComponent-1:zone"], label: "MyComponent" }, | ||
"MyComponent-3": { | ||
path: ["MyComponent-1:zone", "MyComponent-2:zone"], | ||
label: "MyComponent", | ||
}, | ||
}, | ||
}; | ||
|
||
describe("use-breadcrumbs", () => { | ||
describe("convert-path-data-to-breadcrumbs", () => { | ||
it("should convert path data to breadcrumbs", () => { | ||
expect( | ||
convertPathDataToBreadcrumbs(item3, dropzoneContext.pathData, data) | ||
).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"label": "MyComponent", | ||
"selector": { | ||
"index": 0, | ||
"zone": "default-zone", | ||
}, | ||
"zoneCompound": "MyComponent-1:zone", | ||
}, | ||
{ | ||
"label": "MyComponent", | ||
"selector": { | ||
"index": 0, | ||
"zone": "MyComponent-1:zone", | ||
}, | ||
"zoneCompound": "MyComponent-2:zone", | ||
}, | ||
] | ||
`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.