react-visibility-toggles
Visibility toggles
Last updated 3 years ago by bebraw .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install react-visibility-toggles 
SYNC missed versions from official npm registry.

build status bitHound Score codecov

react-visibility-toggles - Visibility toggles for React

react-visibility-toggles provides a React component for rendering visibility toggles for table columns. It works well with Reactabular.

Example

/*
import VisibilityToggles from 'react-visibility-toggles';
*/

const columns = [
  {
    header: {
      label: 'Name'
    },
    visible: true
  },
  {
    header: {
      label: 'Age'
    },
    visible: false
  }
];

<VisibilityToggles
  columns={columns}
  onToggleColumn={({ column, columnIndex }) => console.log(column, columnIndex)}
  isVisible={({ column }) => column.visible}
  props={{}}
/>

Integrating with Reactabular

The following example shows how to integrate react-visibility-toggles with Reactabular:

/*
import React from 'react';
import * as Table from 'reactabular-table';
import { cloneDeep } from 'lodash';
import VisibilityToggles from 'react-visibility-toggles';
*/

class ToggleColumnsTable extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      columns: [
        {
          property: 'name',
          header: {
            label: 'Name'
          },
          visible: true
        },
        {
          property: 'age',
          header: {
            label: 'Age'
          },
          visible: false
        },
        {
          property: 'color',
          header: {
            label: 'Color'
          },
          cell: {
            transforms: [color => ({ style: { color } })]
          },
          visible: true
        }
      ],
      rows: [
        {
          id: 100,
          name: 'Adam',
          age: 12,
          color: 'red'
        },
        {
          id: 101,
          name: 'Brian',
          age: 44,
          color: 'green'
        },
        {
          id: 102,
          name: 'Mike',
          age: 25,
          color: 'blue'
        }
      ]
    };

    this.onToggleColumn = this.onToggleColumn.bind(this);
  }
  render() {
    const { columns, rows } = this.state;

    return (
      <div>
        <VisibilityToggles
          columns={columns}
          onToggleColumn={this.onToggleColumn}
        />

        <Table.Provider
          columns={columns.filter(column => column.visible)}
        >
          <Table.Header />

          <Table.Body rows={rows} rowKey="id" />
        </Table.Provider>
      </div>
    );
  }
  onToggleColumn({ columnIndex }) {
    const columns = cloneDeep(this.state.columns);

    columns[columnIndex].visible = !columns[columnIndex].visible;

    this.setState({ columns });
  }
}

<ToggleColumnsTable />

License

MIT. See LICENSE for details.

Current Tags

  • 1.2.0                                ...           latest (3 years ago)

5 Versions

  • 1.2.0                                ...           3 years ago
  • 1.1.1                                ...           4 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 0
Last Month 3
Dependencies (1)

Copyright 2014 - 2017 © taobao.org |