0.3.7 • Published 5 years ago

orizzonte v0.3.7

Weekly downloads
6
License
MIT
Repository
github
Last release
5 years ago

Orizzonte

React components for a horizontal, filtered search UI

npm version Build Status Coverage Status David gzip size license

Usage

The organization of Orizzonte is simple: first you have the encapsulating Orizzonte component, then there are groups and every group holds one or more filters of its own. Groups can be added and removed from the Orizzonte bar, so users can include only what is relevant to them. Whenever field values change, Orizzonte will compute a new query object with all new values from all groups that are currently visible. Here is a basic example.

import Orizzonte, { Choices, Dropdown, FullText, Group, Select, Toggle } from 'orizzonte';

<Orizzonte
  query={{
    language: 'fr',
    waistSize: 32,
    shirtSize: ['m', 'xl']
  }}
  clearedQuerySnapshot={{
    language: 'en'
  }}
  onChange={(query) => {}}
  onGroupAdd={(groupIndex) => {}}
  onGroupRemove={(groupIndex) => {}}
  groupTopLabels
>
  <Group
    description="Choose your shirt and waist sizes"
    label="Sizes"
    included
  >
    <Dropdown
      fieldName="shirtSize"
      label="Shirt Size"
      selectedLabel={ (value, totalCount) => {
        if (value.length <= 2) {
          return `Size (${ value.map((v) => v.label).join(' & ') })`;
        }
        if (value.length === totalCount) {
          return 'Any shirt size';
        }
        return `Size (${ value.length } selected)`;
      }}
      options={[
        {
          value: 'Small sizes',
          children: [
            {label: 'Extra Small',value: 'xs',disabled: true,facetCount: 0},
            {label: 'Small',value: 's',facetCount: 129}
          ]
        },
        {label: 'Medium',value: 'm',facetCount: 213},
        …
      ]}
      multiple
      selectAll
    />
    <Choices
      fieldName="waistSize"
      label="Waist Size"
      selectedLabel={ (value) => (`${ value.selectedLabel || value.label } waist size`) }
      options={[
        {label: 'Extra Small (28)',selectedLabel: 'Extra Small',value: 28,facetCount: 345},
        {label: 'Small (30)',selectedLabel: 'Small',value: 30,facetCount: 12},
        {label: 'Medium (32)',selectedLabel: 'Medium',value: 32,facetCount: 228},
        …
      ]}
      multiple
    />
  </Group>
  <Group
    label="Locale"
    included
  >
    <Select
      fieldName="language"
      label="Language"
      selectedLabel="%s (Primary)"
      options={[
        {label: 'English',value: 'en'},
        {label: 'French',value: 'fr'},
        {label: 'German',value: 'de'},
        …
      ]}
    />
    <Toggle
      fieldName="toggle"
      option={{
        label: 'Toggle me on or off',
        value: 'on'
      }}
      selectedLabel="Toggle is on"
      toggleStateLabel={{
        on: 'Active',
        off: 'Inactive'
      }}
    />
    <Dropdown
      fieldName="country"
      label="Country"
      selectedLabel={selectedLabel}
      options={[
        {label: 'United Kingdom',value: 'uk'},
        {label: 'France',value: 'fr'},
        {label: 'Germany',value: 'de'},
        …
      ]}
      filter={{
        enabled: true,
        placeholder: 'Search options...'
      }}
    />
  </Group>
  <Group
    label="Keywords"
  >
    <FullText
      fieldName="keywords"
      label="Keywords"
      selectedLabel={(value) => (truncate(value.trim(), {
        length: 20
      }))}
      placeholder="Enter some keywords..."
      multiline
    />
    <FullText
      disabled
      fieldName="disabledTextField"
      label="Another field"
      placeholder="This one is disabled..."
    />
  </Group>
</Orizzonte>

Examples

Click here for a live demo

Storybook

Orizzonte comes with a Storybook that you can run yourself

  1. Make sure you have Storybook installed (globally):

    $ npm i -g @storybook/cli
  2. Run npm run storybook

  3. Go to http://localhost:9001 to see all examples

Click on 'Show info' to see additional implementation details such as supported prop types and their definitions.

Configuration

<Orizzonte /> component

PropTypeRequiredDescription
addBtnLabelstringnoCustom label for add-button
autoExpandOnGroupAddbooleannoMakes a newly added group auto expand
autoHideControlsbooleannoIf true, add, clear and save buttons will hide automatically
autoHideTimeoutnumbernoCustom timeout interval for auto-hiding controls
classNamestringnoCustom additional class name for the top-level element
clearAllLabelstringnoCustom label for the button to clear all of the query. onClear prop needs to be defined for the button to show.
clearedQuerySnapshotobjectnoSnapshot of initial query state. If set, this will be used to determine if the query diverged from blank-slate.
collapseGroupOnClickOutsidebooleannoWhether the group should collapse when the user clicks outside of it. Changes will not be applied to the query.
groupToggleIconnodenoToggle indicator icon to be shown in groups
groupTopLabelsbooleannoWhether the group label should be shown at the top if some of it's filters have selected values
hideAddOnAllGroupsIncludedbooleannoHide the add-button when there are no more groups to add
dispatchOnFilterChangebooleannoIf true, the query object will be updated right after any filter change
maxGroupsnumbernoMaximum number of groups to be added
onChangefunctionyesCallback function that triggers when the final query object is updated
onClearfunctionnoCallback function for clearing all of the query. A snapshot of the cleared query state is given as argument.
onGroupAddfunctionnoCallback function for when a new filter group is added
onGroupRemovefunctionnoCallback function for when a filter group is removed
onSavefunctionnoCallback function saving the current query object
orientationltr or rtlnoRender groups and controls from left-to-right (ltr, default) or right-to-left (rtl)
queryobjectnoThe current query object
saveLabelstringnoCustom label for the button to save the current query. onSave prop needs to be defined for the button to show.
showGroupControlsOnMouseoverbooleannoOnly show the group controls (remove, done) when hovering over the dropdown list
styleobjectnoCustom inline styles for the top-level element

<Group /> component

Groups contain one or more filters for which it make sense to be shown together. Each group has its own name and can be provided with a description.

PropTypeRequiredDescription
classNamestringnoCustom additional class name for top-level component element
mutuallyExclusiveFiltersboolean or arraynoWhen true, only one filter can be selected for this group. When you want only specific filters to be mutually exclusive, you can provide an array of (two or more) field names.
descriptionstringnoA description for this group of filters
doneBtnLabelstringnoCustom label for Done-button
hideClearbooleannoHides the clear button in the dropdown
hideDonebooleannoHides the done button in the dropdown
hideRemovebooleannoHides the button to remove this group
includedbooleannoIf the group should be present in the bar
labelstringyesLabel for this group
orientationleft or rightnoDefault orientation of the group dropdown list
removeBtnLabelstringnoCustom label for Remove-button.
showControlsOnMouseoverbooleannoOnly show group controls (remove, done) when hovering over the dropdown list element
styleobjectnoCustom inline styles for top-level component element

Filters

A filter is responsible for controlling the value of a particular field in the query object. Every filter should be included as part of a group. Orizzonte comes with the following filter types:

FilterDescription
ChoicesA series of inline checkboxes or radios
DropdownA more advanced dropdown select with support for filtering options and select all
FullTextA single or multi line full text field
SelectA simple single-select filter (uses browser <select /> element)
ToggleA toggle switch filter for toggling a single-value field on and off

<Choices /> filter

A series of inline checkboxes (multiple selections) or radios (single selection). Each Choices filter can have multiple options.

PropTypeRequiredDescription
fieldNamestringyesField name for this filter, to be used in composed query
informationstringnoHelp text for this filter, to be shown on mouseover
labelstringnoLabel for this filter
multiplebooleannoWhether to show checkboxes (true) or radios (false)
noPreferenceLabelstringnoLabel to show if you want to include a 'no preference' option. Only available for radio groups.
optionsarrayyesCollection of possible options for this group of choices. Each option must at least have a value.
Option properties

Each option is represented by an object that can have the following properties. All possible options are provided as an array of objects (collection).

PropTypeDescription
option.disabledboolField name for this filter, to be used in composed query
option.facetCountstring or numberHelp text for this filter, to be shown on mouseover
option.labelstringLabel for this option. If no label is defined, option.value will be used instead.
option.valuestring or numberSelected value for this option (required)

<Dropdown /> filter

A more advanced dropdown select with support for filtering options and select all. Facet counts for individual options are supported.

PropTypeRequiredDescription
fieldNamestringyesField name for this filter, to be used in composed query
informationstringnoHelp text for this filter, to be shown on mouseover
disabledbooleannoDisables the dropdown
filterobjectnoFilter dropdown options and highlight matches
filter.enabledbooleanyesIf filtering options should be enabled
filter.matchCasebooleannoIf the dropdown filter is case sensitive
filter.matchDiacriticsbooleannoIf the dropdown filter should strictly match diacritics
filter.matchPositionstart or endnoWhether to match at any position in the string or from the start
labelstringnoLabel for this filter
multiplebooleannoAllows selecting multiple options
notSetLabelstringnoLabel to shown when no options are selected
optionsarrayyesCollection of selectable options (property value is required, label and disabled are optional). To create a group of options, use value for the group label and add an array of grouped options as children.
remoteobjectnoRemote API to fetch dropdown options from
remote.dataobjectnoData object to use as request payload
remote.endpointstringyesRemote endpoint URI
remote.loadingTextstringnoText to show while loading data
remote.searchParamstringyesQuery parameter to apply the filter value to
remote.transformerfunctionnoFunction for transforming response data before consuming it
remote.requestMethodstringnoRequest method (GET by default)
selectAllbooleannoWhether to include a select all option on multiselects. This is not supported when remote source is configured.
selectAllCountbooleannoShow total option count (excl. disabled options) in select all label
selectAllLabelstringnoWhat label to show for the select all option
selectedLabelstring or functionnoTransforming function or placeholder for group label

<FullText /> filter

A single or multi line full text field

PropTypeRequiredDescription
autoFocusbooleannoIf the input should automatically receive focus when group is expanded
disabledbooleannoDisables the input field
dispatchTimeoutnumbernoCustom debounce timeout before dispatching the new value to the query object
fieldNamestringyesField name for this filter, to be used in composed query
informationstringnoHelp text for this filter, to be shown on mouseover
labelstringnoLabel for this filter section
maxHeightnumbernoMaximum textarea height (only applicable in multiline mode)
maxWidthnumbernoMaximum textarea width (only applicable in multiline mode)
multilinebooleannoWhether to render a textarea (true) or input field (false)
placeholderstringnoPlaceholder text for the input field
selectedLabelstring or functionnoTransforming function or placeholder for group label
validateInputfunctionnoFunction to validate input, should return true (valid) or false (invalid)

<Select /> filter

A simple single-select filter (uses browser <select /> element)

PropTypeRequiredDescription
disabledbooleannoDisables the input field
fieldNamestringyesField name for this filter, to be used in composed query
informationstringnoHelp text for this filter, to be shown on mouseover
labelstringnoLabel for this filter section
notSetLabelstringnoWhich label the first (empty) option should have in case the select can be empty
optionsarrayyesCollection of selectable options (property value is required, label and disabled are optional). To create a group of options, use value for the group label and add an array of grouped options as children.
placeholderstringnoPlaceholder text for the input field
selectedLabelstring or functionnoTransforming function or placeholder for group label

<Toggle /> filter

A toggle switch button (affects single value)

PropTypeRequiredDescription
disabledbooleannoDisables the toggle switch
fieldNamestringyesField name for this filter, to be used in composed query
informationstringnoHelp text for this filter, to be shown on mouseover
labelstringnoLabel for this filter
optionobjectyesOption that can be toggled (property value is required, label and disabled are optional). A toggle can only take one option. When switched on, the field will be included in the query with the option value

Theming

All customizable variables defaults are found in src/scss/variables.scss. To override any variable, don't import the compiled distribution CSS that comes with the package but instead, import the Sass source in your project's source and redeclare the variables you want before that statement. For example:

// Custom variable values
$color-a: blue;
$color-b: red;

// Import Sass source
@import '~orizzonte/src/scss/Orizzonte.scss';

Following this method, the styles should be compiled as part of your own project's build process while taking in consideration the customizations applied.

Custom filters

Orizzonte comes with a set of default filters out of the box. If these do not meet your requirements, you can choose to create a filter of your own. Internally, Orizzonte clones each filter component and adds a few props, in addition to the props you configured, that are used to read and update filter values. Any other filter interactivity should be managed within the filter component itself.

  • fieldName This prop is required to be defined and will be used as the field name (key) for this filter in the query object (if a value is set). It should only be of type PropTypes.string.

  • value The current filter value is provided through the value-prop and is derived directly from the current query object. Make sure to define which types you expect to receive for this prop.

  • onUpdate(newValue) The filter value can be updated by calling the onUpdate function and passing the new value as it's first and only argument. Note that the value in the query object will be exactly as passed through the onUpdate callback. If for any reason the component expects a value representation different from how it's stored in the query, you should use a transformer function to convert the internal format from and to the query format.

Tests

  • Run tests: npm test
  • Test a specific component: npm test -- <component>.spec.js
  • Run a coverage report: npm test -- --coverage
0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.1.26

6 years ago

0.1.25

6 years ago

0.1.24

6 years ago

0.1.23

6 years ago

0.1.22

6 years ago

0.1.21

6 years ago

0.1.20

6 years ago

0.1.19

6 years ago

0.1.18

6 years ago

0.1.17

6 years ago

0.1.16

6 years ago

0.1.15

6 years ago

0.1.14

6 years ago

0.1.13

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago