-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jump to current content in reference picker (#1534)
* contextPath prop with sample div * jump to current Content * remove console.log * remove console logs * add navigation to tree Picker * before error * reference picker helper * fix tests * picker helper tests * picker helper refactor * unit Test for Picker Helper * test fix * unit Test for helper * bug fixes and ui fixes * root fix * picker tests * reference picker bug fixes * unit test fix * reference helper * tooltip and remove virtualRoot * test * unit test fix * test fix --------- Co-authored-by: Ádám Hassan <[email protected]>
- Loading branch information
1 parent
14d7b49
commit f072985
Showing
11 changed files
with
424 additions
and
83 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
68 changes: 68 additions & 0 deletions
68
packages/sn-pickers-react/src/components/picker/picker-helper.hook.tsx
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,68 @@ | ||
import { ODataResponse, Repository } from '@sensenet/client-core' | ||
import { PathHelper } from '@sensenet/client-utils' | ||
import { GenericContent } from '@sensenet/default-content-types' | ||
import { useEffect, useState } from 'react' | ||
import { TReferemceSelectionHelperPath } from './picker-props' | ||
|
||
type ReferenceFieldHelperProps = { | ||
contextPath?: string | ||
selectionRoots?: string[] | ||
repository: Repository | ||
} | ||
|
||
export const usePickerHelper = ({ contextPath, selectionRoots, repository }: ReferenceFieldHelperProps) => { | ||
const [helperPaths, setHelperPaths] = useState<TReferemceSelectionHelperPath[]>([]) | ||
const [isAncestorOfRoot, setIsAncestorOfRoot] = useState(false) | ||
|
||
const [isLoading, setIsLoading] = useState(true) | ||
|
||
useEffect(() => { | ||
const getReferencePickerHelperData = async () => { | ||
const SelectionRootQueries = [] | ||
|
||
let isContextPathInTree = false | ||
|
||
for (const root of selectionRoots || []) { | ||
if (PathHelper.isInSubTree(root, contextPath!)) { | ||
isContextPathInTree = true | ||
} | ||
|
||
if (!repository.load) { | ||
continue | ||
} | ||
|
||
const promise = repository?.load<GenericContent>({ | ||
idOrPath: root, | ||
oDataOptions: { | ||
select: ['Name', 'DisplayName', 'Path'], | ||
}, | ||
}) | ||
|
||
SelectionRootQueries.push(promise) | ||
} | ||
|
||
try { | ||
const promiseResult = await Promise.allSettled(SelectionRootQueries) | ||
|
||
const fulfilledResults: TReferemceSelectionHelperPath[] = promiseResult | ||
.filter((result) => result.status === 'fulfilled' && result.value?.d.Path !== contextPath) | ||
.map( | ||
(result) => | ||
(result as PromiseFulfilledResult<ODataResponse<GenericContent>>).value | ||
.d as TReferemceSelectionHelperPath, | ||
) | ||
|
||
setHelperPaths(fulfilledResults) | ||
setIsLoading(false) | ||
setIsAncestorOfRoot(isContextPathInTree) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} | ||
|
||
getReferencePickerHelperData() | ||
}, [selectionRoots, contextPath, repository]) | ||
|
||
return { helperPaths, isAncestorOfRoot, isLoading } | ||
} | ||
export default usePickerHelper |
87 changes: 87 additions & 0 deletions
87
packages/sn-pickers-react/src/components/picker/picker-helper.tsx
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,87 @@ | ||
import { CircularProgress, Link, Tooltip } from '@material-ui/core' | ||
import { Repository } from '@sensenet/client-core' | ||
import React, { memo } from 'react' | ||
import { usePickerHelper } from './picker-helper.hook' | ||
|
||
type ReferenceFieldHelperProps = { | ||
contextPath?: string | ||
handleJumpToCurrentPath: (path: string) => void | ||
styles?: string | ||
selectionRoots?: string[] | ||
currentContentText?: string | ||
repository: Repository | ||
} | ||
|
||
const containerStyle: React.CSSProperties = { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
justifyContent: 'flex-start', | ||
alignItems: 'flex-start', | ||
rowGap: '10px', | ||
paddingTop: '15px', | ||
width: '240px', | ||
paddingInline: '10px', | ||
} | ||
|
||
export const PickerHelper = ({ | ||
handleJumpToCurrentPath, | ||
contextPath, | ||
styles, | ||
currentContentText, | ||
selectionRoots, | ||
repository, | ||
}: ReferenceFieldHelperProps) => { | ||
const { helperPaths, isAncestorOfRoot, isLoading } = usePickerHelper({ | ||
contextPath, | ||
selectionRoots, | ||
repository, | ||
}) | ||
|
||
if (isLoading) { | ||
return ( | ||
<div style={containerStyle}> | ||
<CircularProgress color="primary" /> | ||
</div> | ||
) | ||
} | ||
|
||
if (!isAncestorOfRoot && helperPaths.length === 0) { | ||
return null | ||
} | ||
|
||
return ( | ||
<div style={containerStyle}> | ||
{isAncestorOfRoot && contextPath && ( | ||
<Tooltip title={contextPath}> | ||
<Link | ||
data-test="current-content" | ||
variant="body2" | ||
onClick={() => handleJumpToCurrentPath(contextPath)} | ||
className={styles}> | ||
{currentContentText || 'Current Content'} | ||
</Link> | ||
</Tooltip> | ||
)} | ||
|
||
{helperPaths.length > 0 && ( | ||
<div data-test="path-helpers"> | ||
{helperPaths.map((path) => { | ||
return ( | ||
<Tooltip key={path.Path} title={path.Path}> | ||
<Link | ||
data-test={`path-helper-${path.Path}`} | ||
variant="body2" | ||
onClick={() => handleJumpToCurrentPath(path.Path)} | ||
className={styles}> | ||
{path.DisplayName || path.Name} | ||
</Link> | ||
</Tooltip> | ||
) | ||
})} | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default memo(PickerHelper, (prevProps, nextProps) => prevProps.contextPath === nextProps.contextPath) |
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.