Helpers for reading and writing URL query parameters.
In :url_params mode, PhoenixSelect stores the selection in the URL query
string so it survives navigation and refreshes and can be shared as a link.
These helpers manipulate query params on a URI string and return paths
suitable for Phoenix.LiveView.push_patch/2.
Keys support single-level bracket notation (e.g. "filter[genre]"), which
encodes to nested query params.
Summary
Functions
Sets a query param, or removes it when the value is nil, "", or [].
Sets a query param on the given URI string, replacing any existing value.
Removes a query param from the given URI string.
Returns the path, query, and fragment of a URI as a single string, stripping
scheme and host. Suitable for push_patch/2.
Returns the decoded value of a query param, or nil when absent.
Parses a bracket-notation key like "filter[genre]" into a list of access
keys. Simple keys return a single-element list.
Functions
Sets a query param, or removes it when the value is nil, "", or [].
Sets a query param on the given URI string, replacing any existing value.
Examples
iex> PhoenixSelect.Helpers.URI.create_or_update_query_param("/books", "sort", "title")
"/books?sort=title"
iex> PhoenixSelect.Helpers.URI.create_or_update_query_param("/books?sort=title", "sort", "year")
"/books?sort=year"
iex> PhoenixSelect.Helpers.URI.create_or_update_query_param("/books", "filter[genre]", ["scifi", "horror"])
"/books?filter[genre][]=scifi&filter[genre][]=horror"
Removes a query param from the given URI string.
Examples
iex> PhoenixSelect.Helpers.URI.delete_query_param("/books?sort=title&page=2", "sort")
"/books?page=2"
iex> PhoenixSelect.Helpers.URI.delete_query_param("/books?sort=title", "sort")
"/books"
Returns the path, query, and fragment of a URI as a single string, stripping
scheme and host. Suitable for push_patch/2.
Examples
iex> PhoenixSelect.Helpers.URI.extract_full_path("https://example.com/books?sort=title#top")
"/books?sort=title#top"
Returns the decoded value of a query param, or nil when absent.
Array params (genre[]=a&genre[]=b) decode to lists, bracket params decode
to maps.
Examples
iex> PhoenixSelect.Helpers.URI.get_query_param("/books?sort=title", "sort")
"title"
iex> PhoenixSelect.Helpers.URI.get_query_param("/books?genre[]=scifi&genre[]=horror", "genre")
["scifi", "horror"]
iex> PhoenixSelect.Helpers.URI.get_query_param("/books", "sort")
nil
Parses a bracket-notation key like "filter[genre]" into a list of access
keys. Simple keys return a single-element list.
Examples
iex> PhoenixSelect.Helpers.URI.param_path("filter[genre]")
["filter", "genre"]
iex> PhoenixSelect.Helpers.URI.param_path("genre")
["genre"]