@underverse-ui/underverse 1.0.97 → 1.0.98

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -16934,15 +16934,47 @@ var TREE_NODE_INDENT_REM = 1;
16934
16934
  var TREE_BRANCH_OFFSET_CLASS = "ml-1.5 pl-1.5";
16935
16935
  var TREE_NODE_GAP_CLASS = "gap-1.5";
16936
16936
  var TREE_EXPANDER_PLACEHOLDER_CLASS = "w-5";
16937
- function getInitialExpandedNodes(categories, defaultExpanded, viewOnly, inline) {
16938
- if (!(viewOnly || inline) || !defaultExpanded) return /* @__PURE__ */ new Set();
16939
- const parentIds = /* @__PURE__ */ new Set();
16940
- for (const category of categories) {
16941
- if (typeof category.parent_id === "number") {
16942
- parentIds.add(category.parent_id);
16937
+ function getAncestorPathIds(categories, targetId) {
16938
+ const byId = new Map(categories.map((category) => [category.id, category]));
16939
+ const expanded = /* @__PURE__ */ new Set();
16940
+ let current = byId.get(targetId);
16941
+ let guard = 0;
16942
+ while (current && guard++ < categories.length) {
16943
+ expanded.add(current.id);
16944
+ if (typeof current.parent_id !== "number") break;
16945
+ current = byId.get(current.parent_id);
16946
+ }
16947
+ return expanded;
16948
+ }
16949
+ function getInitialExpandedNodes(categories, {
16950
+ defaultExpanded,
16951
+ defaultExpandedIds,
16952
+ expandToId,
16953
+ viewOnly,
16954
+ inline
16955
+ }) {
16956
+ const expanded = /* @__PURE__ */ new Set();
16957
+ if ((viewOnly || inline) && defaultExpanded) {
16958
+ for (const category of categories) {
16959
+ if (typeof category.parent_id === "number") {
16960
+ expanded.add(category.parent_id);
16961
+ }
16962
+ }
16963
+ }
16964
+ for (const id of defaultExpandedIds ?? []) {
16965
+ if (typeof id === "number") {
16966
+ expanded.add(id);
16943
16967
  }
16944
16968
  }
16945
- return parentIds;
16969
+ if (typeof expandToId === "number") {
16970
+ for (const id of getAncestorPathIds(categories, expandToId)) {
16971
+ expanded.add(id);
16972
+ }
16973
+ }
16974
+ return expanded;
16975
+ }
16976
+ function getExpandedNodesState(expandedIds, uncontrolledExpandedNodes) {
16977
+ return expandedIds !== void 0 ? new Set(expandedIds) : uncontrolledExpandedNodes;
16946
16978
  }
16947
16979
  function CategoryTreeSelect(props) {
16948
16980
  const tv = useSmartTranslations("ValidationInput");
@@ -16961,6 +16993,10 @@ function CategoryTreeSelect(props) {
16961
16993
  helperText,
16962
16994
  viewOnly = false,
16963
16995
  defaultExpanded = false,
16996
+ defaultExpandedIds,
16997
+ expandToId = null,
16998
+ expandedIds,
16999
+ onExpandedChange,
16964
17000
  enableSearch,
16965
17001
  labels,
16966
17002
  inline = false,
@@ -16971,7 +17007,9 @@ function CategoryTreeSelect(props) {
16971
17007
  singleSelect = false
16972
17008
  } = props;
16973
17009
  const [isOpen, setIsOpen] = useState31(false);
16974
- const [expandedNodes, setExpandedNodes] = useState31(() => getInitialExpandedNodes(categories, defaultExpanded, viewOnly, inline));
17010
+ const [expandedNodes, setExpandedNodes] = useState31(
17011
+ () => getInitialExpandedNodes(categories, { defaultExpanded, defaultExpandedIds, expandToId, viewOnly, inline })
17012
+ );
16975
17013
  const [query, setQuery] = useState31("");
16976
17014
  const [localRequiredError, setLocalRequiredError] = useState31();
16977
17015
  const searchInputRef = useRef19(null);
@@ -17008,6 +17046,7 @@ function CategoryTreeSelect(props) {
17008
17046
  const isSearchEnabled = useMemo19(() => enableSearch ?? categories.length > 10, [enableSearch, categories.length]);
17009
17047
  const normalizedQuery = useMemo19(() => query.trim().toLowerCase(), [query]);
17010
17048
  const isSearchMode = isSearchEnabled && normalizedQuery.length > 0;
17049
+ const effectiveExpandedNodes = useMemo19(() => getExpandedNodesState(expandedIds, expandedNodes), [expandedIds, expandedNodes]);
17011
17050
  const visibleIds = useMemo19(() => {
17012
17051
  if (!isSearchMode) return null;
17013
17052
  const matches = categories.filter((c) => c.name.toLowerCase().includes(normalizedQuery));
@@ -17060,13 +17099,16 @@ function CategoryTreeSelect(props) {
17060
17099
  }, [disabled, required, valueArray.length]);
17061
17100
  const toggleExpand = (id2) => {
17062
17101
  if (isSearchMode) return;
17063
- const newExpanded = new Set(expandedNodes);
17102
+ const newExpanded = new Set(effectiveExpandedNodes);
17064
17103
  if (newExpanded.has(id2)) {
17065
17104
  newExpanded.delete(id2);
17066
17105
  } else {
17067
17106
  newExpanded.add(id2);
17068
17107
  }
17069
- setExpandedNodes(newExpanded);
17108
+ if (expandedIds === void 0) {
17109
+ setExpandedNodes(newExpanded);
17110
+ }
17111
+ onExpandedChange?.(Array.from(newExpanded));
17070
17112
  };
17071
17113
  const handleSelect = (categoryId, category) => {
17072
17114
  if (viewOnly) return;
@@ -17108,7 +17150,7 @@ function CategoryTreeSelect(props) {
17108
17150
  const renderCategory = (category, level = 0) => {
17109
17151
  const children = effectiveChildrenMap.get(category.id) || [];
17110
17152
  const hasChildren = children.length > 0;
17111
- const isExpanded = hasChildren && (isSearchMode || expandedNodes.has(category.id));
17153
+ const isExpanded = hasChildren && (isSearchMode || effectiveExpandedNodes.has(category.id));
17112
17154
  const isSelected = selectedIds.has(category.id);
17113
17155
  const isSelectable = !viewOnly && (!leafOnlySelect || !hasChildren);
17114
17156
  return /* @__PURE__ */ jsxs39(