Skip to content

Commit

Permalink
feat: add possibility to define default value for parent filter on Tr…
Browse files Browse the repository at this point in the history
…eeView (#815)

* RM#86491
  • Loading branch information
vhu-axelor authored and lme-axelor committed Nov 29, 2024
1 parent 31e4c3a commit 2063d79
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions changelogs/unreleased/86491.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": "SearchTreeView: add possibility to define default value for parent filter",
"type": "feat",
"packages": "core"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -66,6 +68,7 @@ interface SearchTreeViewProps {
actionList?: ActionType[];
verticalActions?: boolean;
displayBreadcrumb?: boolean;
defaultParent?: any;
}

const SearchTreeView = ({
Expand Down Expand Up @@ -103,6 +106,7 @@ const SearchTreeView = ({
actionList,
verticalActions,
displayBreadcrumb = false,
defaultParent,
}: SearchTreeViewProps) => {
const I18n = useTranslator();
const dispatch = useDispatch();
Expand All @@ -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];
Expand All @@ -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;
Expand Down Expand Up @@ -253,10 +264,15 @@ const SearchTreeView = ({
{displayBreadcrumb && (
<Breadcrumb
style={styles.breadcrumb}
items={parent.map((item, index) => ({
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([])}
/>
)}
Expand Down

0 comments on commit 2063d79

Please sign in to comment.