8 changed files with 2017 additions and 93 deletions
@ -0,0 +1,5 @@ |
|||
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */ |
|||
module.exports = { |
|||
preset: 'ts-jest', |
|||
testEnvironment: 'node', |
|||
}; |
File diff suppressed because it is too large
@ -0,0 +1,50 @@ |
|||
import { useLocation } from "react-router-dom" |
|||
export function foundRoute(route: any, name: string, arrIndex?: number[]): any { |
|||
if (!arrIndex) { |
|||
arrIndex = [] |
|||
} |
|||
for (let i = 0; i < route.length; i++) { |
|||
const element = route[i] |
|||
if (element.root) { |
|||
arrIndex = [] |
|||
} |
|||
arrIndex.push(i) |
|||
let lena = element.path.split("/").length |
|||
let lenb = name.split("/").length |
|||
if (element.path == name && lena == lenb) { |
|||
return arrIndex |
|||
} else if (element.children) { |
|||
let a = foundRoute(element.children, name, arrIndex) |
|||
if (a) { |
|||
return a |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
export default function useRoute(routes:any){ |
|||
const { pathname } = useLocation() |
|||
let oroute = JSON.parse(JSON.stringify(routes)) |
|||
let index = foundRoute(routes, pathname) |
|||
let isLayout = true |
|||
let curA = null |
|||
let curB = null |
|||
if (index && index.length) { |
|||
let res = oroute[index[0]] |
|||
let cur = res |
|||
for (let i = 1; i < index.length; i++) { |
|||
const element = index[i] |
|||
cur.children = [cur.children[element]] |
|||
cur = cur.children[element] |
|||
} |
|||
if (cur) { |
|||
cur.children = [] |
|||
} |
|||
if (cur.layout != undefined) { |
|||
isLayout = !!cur.layout |
|||
} |
|||
curA = cur |
|||
curB = res |
|||
} |
|||
return [curA,curB] |
|||
} |
Loading…
Reference in new issue