8.1.0 • Published 2 years ago

@woocommerce/navigation v8.1.0

Weekly downloads
736
License
GPL-3.0-or-later
Repository
github
Last release
2 years ago

Navigation

A collection of navigation-related functions for handling query parameter objects, serializing query parameters, updating query parameters, and triggering path changes.

Installation

Install the module

pnpm install @woocommerce/navigation --save

Usage

getHistory

A single history object used to perform path changes. This needs to be passed into ReactRouter to use the other path functions from this library.

import { getHistory } from '@woocommerce/navigation';

render() {
	return (
		<Router history={ getHistory() }>
			…
		</Router>
	);
}

getPath() ⇒ String

Get the current path from history.

Returns: String - Current path.

getTimeRelatedQuery(query) ⇒ Object

Gets time related parameters from a query.

Returns: Object - Object containing the time related queries.

ParamTypeDescription
queryObjectQuery containing the parameters.

getIdsFromQuery(queryString) ⇒ Array

Get an array of IDs from a comma-separated query parameter.

Returns: Array - List of IDs converted to an array of unique integers.

ParamTypeDescription
queryStringstringstring value extracted from URL.

getNewPath(query, path, currentQuery) ⇒ String

Return a URL with set query parameters.

Returns: String - Updated URL merging query params into existing params.

ParamTypeDescription
queryObjectobject of params to be updated.
pathStringRelative path (defaults to current path).
currentQueryObjectobject of current query params (defaults to current querystring).

getQuery() ⇒ Object

Get the current query string, parsed into an object, from history.

Returns: Object - Current query object, defaults to empty object.

onQueryChange(param, path, query) ⇒ function

This function returns an event handler for the given param

Returns: function - A callback which will update param to the passed value when called.

ParamTypeDescription
paramstringThe parameter in the querystring which should be updated (ex page, per_page)
pathstringRelative path (defaults to current path).
querystringobject of current query params (defaults to current querystring).

updateQueryString(query, path, currentQuery)

Updates the query parameters of the current page.

ParamTypeDescription
queryObjectobject of params to be updated.
pathStringRelative path (defaults to current path).
currentQueryObjectobject of current query params (defaults to current querystring).

flattenFilters(filters) ⇒ Array

Collapse an array of filter values with subFilters into a 1-dimensional array.

Returns: Array - Flattened array of all filters.

ParamTypeDescription
filtersArraySet of filters with possible subfilters.

getActiveFiltersFromQuery(query, config) ⇒ Array.<activeFilters>

Given a query object, return an array of activeFilters, if any.

Returns: Array.<activeFilters> - - array of activeFilters

ParamTypeDescription
queryobjectquery oject
configobjectconfig object

getDefaultOptionValue(config, options) ⇒ string | undefined

Get the default option's value from the configuration object for a given filter. The first option is used as default if no defaultOption is provided.

Returns: string | undefined - - the value of the default option.

ParamTypeDescription
configobjecta filter config object.
optionsarrayselect options.

getQueryFromActiveFilters(activeFilters, query, config) ⇒ object

Given activeFilters, create a new query object to update the url. Use previousFilters to Remove unused params.

Returns: object - - query object representing the new parameters

ParamTypeDescription
activeFiltersArray.<activeFilters>activeFilters shown in the UI
queryobjectthe current url query object
configobjectconfig object

getUrlKey(key, rule) ⇒ string

Get the url query key from the filter key and rule.

Returns: string - - url query key.

ParamTypeDescription
keystringfilter key.
rulestringfilter rule.

activeFilter : Object

Describe activeFilter object.

Properties

NameTypeDescription
keystringfilter key.
rulestringa modifying rule for a filter, eg 'includes' or 'is_not'.
valuestringfilter value(s).