How do I style react-table filter input field?

cbll picture cbll · Jun 27, 2017 · Viewed 12.9k times · Source

I'm using react-table and have a filter method for each column which works. However, I cannot seem to figure out how I actually style the filter input field - it's white now and blends in with the background.

Here's a codepen where the "Last Name" column is filterable, with an input field: https://codepen.io/anon/pen/QgOdOp?editors=0010

I want to add some semantic styling to this input field, such as this:

                    <div className="ui icon input">
                        // Input values would go here
                        <i className="search icon" />
                    </div>

But I do not seem to know how to bind the input value to the column itself.

I've tried the following in the specific column, but it doesn't render anything:

{
    Header: 'Last Name',
    filterable: true,
    id: 'lastName',
    accessor: d => d.lastName
    Filter: Cellinfo => (
                <div className="ui icon input">
                     <select onChange={event => onFiltersChange(event.target.value)} value={filter ? filter.value : ''}></select> 
                    <i className="search icon" />
                </div>
    )
  }

Answer

Brett DeWoody picture Brett DeWoody · Jun 27, 2017

This could be accomplished with plain CSS. Give the table an id or class, then target the inputs and style them as needed.

<ReactTable 
  showFilters={true} 
  data={makeData()} 
  columns={columns} 
  className='react-table' 
/>

.react-table input {
  background-color: black;
  color: white;
}

The better option would be to use react-tables built-in Filter column option to define a custom UI for the filter. This is documented in the Custom Filtering example. This allows you to pass a style object.

const columns = [
  {
    Header: 'Name',
    columns: [
      {
        Header: 'First Name',
        accessor: 'firstName',
      },
      {
        Header: 'Last Name',
        filterable: true,
        id: 'lastName',
        accessor: d => d.lastName,
        Filter: ({filter, onChange}) => (
          <input
            onChange={event => onChange(event.target.value)}
            value={filter ? filter.value : ''}
            style={{
              width: '100%',
              backgroundColor: 'gray',
              color: 'white',
            }}
          />
        ),
      },
    ],
  },
  {
    Header: 'Info',
    columns: [
      {
        Header: 'Age',
        accessor: 'age',
      },
    ],
  },
];

Using this you could define a background image to achieve the icon you want. Or you could pass in a custom component which sets an icon element behind the input. Something like this:

Filter: ({filter, onChange}) => {
  return (
        <div style={{position: 'relative'}}>
        <input
          onChange={event => onChange(event.target.value)}
          value={filter ? filter.value : ''}
          style={{
            width: '100%',
            backgroundColor: 'transparent',
            color: '#222222',
          }}
        />
        <i style={{position: 'absolute', right: '10px', lineHeight: '30px'}}>
          Icon
        </i>
      </div>
  )
);