Skip to content

Commit

Permalink
Bugfix: clean treeselectionview type and fix a initial state check gr…
Browse files Browse the repository at this point in the history
…id samples issue (#5053)

* adding tree component

* add tree selection plugin view component that renders grouped selections

* clean up

* group id number should starts from 1 not 0

* add an example

* clean up and fix initialize state issue

* remove console
  • Loading branch information
lanzhenw authored Nov 6, 2024
1 parent 3331aa0 commit f1c69dd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import { Box, Checkbox, FormControlLabel, IconButton } from "@mui/material";
import React, { useEffect } from "react";
import { ViewPropsType } from "../utils/types";
import { useUnboundState } from "@fiftyone/state";

interface CheckedState {
[key: string]: {
Expand Down Expand Up @@ -70,6 +71,8 @@ export default function TreeSelectionView(props: ViewPropsType) {
const [checkedState, setCheckedState] =
React.useState<CheckedState>(initialCheckedState);

const unboundState = useUnboundState(checkedState);

// Initialize collapsed state for all parents
const initialCollapsedState: CollapsedState = React.useMemo(() => {
const state: CollapsedState = {};
Expand Down Expand Up @@ -192,6 +195,16 @@ export default function TreeSelectionView(props: ViewPropsType) {
return idx === -1 ? 0 : idx + 1;
};

useEffect(() => {
const sampleIds = view?.data.flatMap(([parentId, children]) => {
return children.map((childId) =>
typeof childId === "string" ? childId : childId[0]
);
});
onChange(path, sampleIds);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

// CheckboxView: Represents a single checkbox (either parent or child)
function CheckboxView({
id,
Expand Down
6 changes: 2 additions & 4 deletions fiftyone/operators/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,18 +610,16 @@ def tuple(self, name, *items, **kwargs):
self.define_property(name, tuple_type, view=tuple_view, **kwargs)
return tuple_type

def tree(self, name, *items, **kwargs):
def tree(self, name, **kwargs):
"""Defines a tree property on the object.
Args:
name: the name of the property
*items: the types of the items in the tree
Returns:
a :class:`Tree`
"""
tree_selection_view = TreeSelectionView(**kwargs)
tree_type = Object(*items)
tree_type = List(String())
self.define_property(
name, tree_type, view=tree_selection_view, **kwargs
)
Expand Down

0 comments on commit f1c69dd

Please sign in to comment.