diff --git a/changelogs/unreleased/86491.json b/changelogs/unreleased/86491.json new file mode 100644 index 000000000..69c16cb7c --- /dev/null +++ b/changelogs/unreleased/86491.json @@ -0,0 +1,5 @@ +{ + "title": "SearchTreeView: add possibility to define default value for parent filter", + "type": "feat", + "packages": "core" +} diff --git a/packages/core/src/components/templates/SearchTreeView/SearchTreeView.tsx b/packages/core/src/components/templates/SearchTreeView/SearchTreeView.tsx index 810e13056..76c07f860 100644 --- a/packages/core/src/components/templates/SearchTreeView/SearchTreeView.tsx +++ b/packages/core/src/components/templates/SearchTreeView/SearchTreeView.tsx @@ -31,6 +31,8 @@ import {useDispatch} from '../../../redux/hooks'; import useTranslator from '../../../i18n/hooks/use-translator'; import {useIsFocused} from '../../../hooks/use-navigation'; +const EMPTY_PARENT = {title: '...'}; + interface SearchTreeViewProps { parentList: any[]; list: any[]; @@ -66,6 +68,7 @@ interface SearchTreeViewProps { actionList?: ActionType[]; verticalActions?: boolean; displayBreadcrumb?: boolean; + defaultParent?: any; } const SearchTreeView = ({ @@ -103,6 +106,7 @@ const SearchTreeView = ({ actionList, verticalActions, displayBreadcrumb = false, + defaultParent, }: SearchTreeViewProps) => { const I18n = useTranslator(); const dispatch = useDispatch(); @@ -111,6 +115,10 @@ const SearchTreeView = ({ const [filter, setFilter] = useState(null); const [parent, setParent] = useState([]); + useEffect(() => { + setParent([EMPTY_PARENT, defaultParent]); + }, [defaultParent]); + const handleChangeParent = value => { setParent(current => { const _parent = [...current]; @@ -119,6 +127,9 @@ const SearchTreeView = ({ _parent.at(-1)?.id !== value?.id && _parent.push(value); } else { _parent.pop(); + if (_parent.at(-1) === EMPTY_PARENT) { + _parent.pop(); + } } return _parent; @@ -253,10 +264,15 @@ const SearchTreeView = ({ {displayBreadcrumb && ( ({ - title: displayParentSearchValue(item), - onPress: () => setParent(current => current.slice(0, index + 1)), - }))} + items={parent.map((item, index) => + item === EMPTY_PARENT + ? EMPTY_PARENT + : { + title: displayParentSearchValue(item), + onPress: () => + setParent(current => current.slice(0, index + 1)), + }, + )} onHomePress={() => setParent([])} /> )}