1.4.0 • Published 4 years ago

react-simple-list2 v1.4.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

react-simple-list2

A collection of list related components I use in my projects

npm.io npm.io npm.io

Install

npm i -D react-simple-list2

Usage

This libraries exports five components; ListViewer, ListFilter, ListFilterItem, ListPagination and ObjectViewer

ListViewer

Renders a list of items in a tabular form. It also supports templating, where you can use a template function to render each item. To see full usages of this component, view its stories here.

The props for this component are shown below:

NameTypeDescription
listArray<object>The list to render
emptyListMessagestringA message to display if the list is empty. Defaults to "No items to display"
emptyAttrMessagestringA message to display if a property of an item is empty. Defaults to "-----"
sortingColumnstringThe column under sort. This allows the sort column to be controlled by the host component
sortingOrderstringThe sorting order of the list. Values are an empty string, ASC or DESC. This allows the sort order to be controlled by the host component
attrsArray<string, ((item: any, index: number) => any) | string>The properties to render for each item
action(item: any, index: number) => anyIf specified, an additional column is added for each item displaying whatever this function returns
template(item: any, index: number) => anyIf specified, the ListViewer will use it to render a custom JSX for each item. It is passed the item and its index
onColumnSort(label: string, order: string) => voidIf specified, the column headers will have sorting buttons enabled. If any header is clicked, the ListViewer will call this function with the label clicked and the sorting order. The sorting order can be one of ASC, DESC or an empty string for unsorted
skipIf(item: any, index: number) => booleanIf specified, this controls whether an item is rendered or not. When this function returns true, the item at that index is skipped.
keyFn(item: any, index: number) => booleanIf specified, this assigns unique keys to each rendered item. This function should return a string key which is assigned to the <tr> elements.

ListFilter

As the name implies, this component provides an easy and consistent way of adding filters to a list. The ListFilterItem actually renders the filter components while the ListFilter manages the state of its ListFilterItems. To see full usages of this component, view its stories here.

The props for the ListFilter are shown below:

NameTypeDescription
valueanyThe state object containing the filter values
trimNullValuesbooleanIf true, keys with null values will be removed from the filter object
onChange(values: any) => voidA function that is called when the value of any the filter list item changes. It is passed an object containing the values of each filter item

The props for the ListFilterItem are shown below:

NameTypeDescription
namestringThe name to use as the key in the object passed to the ListFilter onChange function
labelstringThe label to show on top of this filter item
children(config: { value: any; setValue: (val: any) => void }) => anyThe child of the a ListFilterItem must be a function which is passed a value and a setValue function. This returns the JSX to be rendered

ListPagination

The pagination allows you to traverse pages of a paginated list quickly. To see full usages of this component, view its stories here.

The props for this component are shown below:

NameTypeDescription
totalItemsnumberThe total number of items
currentPagenumberThe current page
itemsPerPagenumberThe number of items rendered in a page (I.e page size)
displayCountnumberThe number of numbered page items to display. Defaults to 2
onNavigate(page: number) => voidCalled when any of the navigation buttons is clicked. It is passed the page number corresponding to that button

ObjectViewer

This components renders the specified properties of an object in a tabular form. To see full usages of this component, view its stories here.

The props for this component are shown below:

NameTypeDescription
objectanyThe object whose properties are to be rendered
attrsArray<string, ((item: any, index: number) => any) | string>A list of properties to render for the object
emptyAttrMessagestringA message to display if a property of an item is empty. Defaults to "-----"
widthRationumberThe ratio of widths for the label and value column of the table. Defaults to 0.3 (I.e. the label column width is 30% of the width

Storybook

The project uses Storybook to visually test the components

To run these stories:

  1. Clone the repository
  2. Run npm install to install dependencies
  3. Run npm run storybook to start the storybook server.

For more information on Storybook, click here

Maintainers

Support

If you'd like to support this project, you can do so by becoming a patreon on Patreon

It would be really helpful if you can star the project on Github

Changelog

  • v1.2.0
    • Changed ListFilter to a controlled component.
    • Added trimNullValues prop to ListFilter component.
    • Removed defaultValue prop from ListFilterItem as parent ListFilter is now a controlled component
1.4.0

4 years ago

1.3.0

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago