diff --git a/README.md b/README.md index 7d401f5..a88d68c 100644 --- a/README.md +++ b/README.md @@ -407,14 +407,14 @@ Returns a function to be used to generate paths for a specific route. Extends Gatsby's [navigate](https://www.gatsbyjs.org/docs/gatsby-link/#how-to-use-the-navigate-helper-function) to allow passing route names and params. ### getActivatedRoute -> `getActivatedRoute(): ActivatedRoute` +> `getActivatedRoute(): Route` Gets the current active route based on `@reach/router` location history. ### getMatchingRoute -> `getMatchingRoute(path: string): ActivatedRoute` +> `getMatchingRoute(path: string, ignorePrefix?: boolean): Route` -Gets the route that matches a given path. *Note: The provided path should be prefixed with `pathPrefix` if any.* +Gets the route that matches a given path. ### isActivatedRoute > `isActivatedRoute(route: string): boolean` @@ -422,19 +422,19 @@ Gets the route that matches a given path. *Note: The provided path should be pre Checks whether a given route is currently active. ### getRoutes -> `getRoutes(): Array` +> `getRoutes(parent?: string): Route[]` -Gets an array of all routes. - -### routeExists -> `routeExists(route: string): boolean` - -Checks if there is a route defined with the given name. +Gets an array of all routes or routes nested under a given parent route. ### getRoute > `getRoute(route: string): Route` -Gets a specific route. +Gets the Route object of a given route name. + +### routeExists +> `routeExists(route: string): boolean` + +Checks if a route is defined with the given name. ## Configuration diff --git a/src/index.js b/src/index.js index af0eeda..ee518ed 100644 --- a/src/index.js +++ b/src/index.js @@ -78,6 +78,7 @@ export function isActivatedRoute (route) { /** * Gets the route that matches a specific path + * use `ignorePrefix` if the path provided already contains `pathPrefix` */ export function getMatchingRoute (path, ignorePrefix) { return pick(getRoutes(), ignorePrefix ? path : withPrefix(path)) @@ -85,6 +86,7 @@ export function getMatchingRoute (path, ignorePrefix) { /** * Returns a function to be used to generate paths for a specific route + * use `ignorePrefix` to ignore adding `pathPrefix` to generated paths */ export function getPathGenerator (route, scope, ignorePrefix) { const ro = getRoute(route) diff --git a/types/index.d.ts b/types/index.d.ts index 8d76aac..94aea9d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,13 +1,15 @@ import React from 'react' import { PathFunction as PathGeneratorFunction } from 'path-to-regexp' +export type RouteScope = 'pagination' + export interface Route { name: string path: string realpath: string parent: { name: string - scope: string + scope: RouteScope } | null } @@ -55,6 +57,6 @@ export function getRoute(route: string): Route export function getActivatedRoute(): Route export function isActivatedRoute(route: string): boolean export function getMatchingRoute(path: string, ignorePrefix?: boolean): Route -export function getPathGenerator(route: string, scope?: string, ignorePrefix?: boolean): PathGeneratorFunction -export function generatePath(route: string, params?: RouteParams, scope?: string, ignorePrefix?: boolean): string -export function navigate(to: string, params?: RouteParams, scope?: string, options?: {}): void +export function getPathGenerator(route: string, scope?: RouteScope, ignorePrefix?: boolean): PathGeneratorFunction +export function generatePath(route: string, params?: RouteParams, scope?: RouteScope, ignorePrefix?: boolean): string +export function navigate(to: string, params?: RouteParams, scope?: RouteScope, options?: {}): void diff --git a/types/index.js.flow b/types/index.js.flow index 553d58e..325cfbb 100644 --- a/types/index.js.flow +++ b/types/index.js.flow @@ -1,65 +1,67 @@ // @flow -import * as React from "react"; +import * as React from "react" + +export type RouteScope = 'pagination' export interface Route { - name: string; - path: string; - realpath: string; + name: string + path: string + realpath: string parent: { - name: string; - scope: string; - }; + name: string + scope: RouteScope + } | null } export interface RouteParams { - [index: string]: string; + [index: string]: string } export interface PaginationInfo { - itemCount: number; - perPage: number; - pageCount: number; - currentPage: number; - hasNextPage: boolean; - hasPreviousPage: boolean; + itemCount: number + perPage: number + pageCount: number + currentPage: number + hasNextPage: boolean + hasPreviousPage: boolean } export interface PaginationProps { - route: string; - params?: RouteParams; - ui?: string; - range?: number; - className?: string; - renderDisabled?: boolean; - pageInfo: PaginationInfo; + route: string + params?: RouteParams + ui?: string + range?: number + className?: string + renderDisabled?: boolean + pageInfo: PaginationInfo labels?: { [index: string]: string | React.Node - }; + } theme?: { [index: string]: string - }; + } } export interface LinkProps { - to: string; - params?: RouteParams; - scope?: string; + to: string + params?: RouteParams + scope?: string } export type PathGeneratorFunction = ( data?: RouteParams, options?: {} -) => string; +) => string -declare export var Pagination: React.ComponentType; -declare export var Link: React.ComponentType; +declare export var Pagination: React.ComponentType +declare export var Link: React.ComponentType -declare export function getRoutes(parent: string): [Route]; -declare export function routeExists(route: string): boolean; -declare export function getRoute(route: string): Route; -declare export function getActivatedRoute(): Route; -declare export function isActivatedRoute(route: string): boolean; -declare export function getMatchingRoute(path: string, ignorePrefix?: boolean): Route; -declare export function getPathGenerator(route: string, scope?: string, ignorePrefix?: boolean): PathGeneratorFunction; -declare export function generatePath(route: string, params?: RouteParams, scope?: string, ignorePrefix?: boolean): string; -declare export function navigate(to: string, params?: RouteParams, scope?: string, options?: {}): void; +declare export function getRoutes(parent: string): [Route] +declare export function routeExists(route: string): boolean +declare export function getRoute(route: string): Route +declare export function getActivatedRoute(): Route +declare export function isActivatedRoute(route: string): boolean +declare export function getMatchingRoute(path: string, ignorePrefix?: boolean): Route +declare export function getPathGenerator(route: string, scope?: RouteScope, ignorePrefix?: boolean): PathGeneratorFunction +declare export function generatePath(route: string, params?: RouteParams, scope?: RouteScope, ignorePrefix?: boolean): string +declare export function navigate(to: string, params?: RouteParams, scope?: RouteScope, options?: {}): void