React-Select breaking CoreUi functionalities

Viewed 1165

I'm using @coreui/react with React-select the problem is that returning a Select element in scoped slots breaks core-ui functionalities like searching & sorting

However it works fine if when returning a <label> or a <p> with text Ex: <label>{item.status}</label>

Question

Why is Select component breaking the functionality ?

Any workaround / efforts are highly appreciated

Note

I have tried workarounds like <p hidden >{item.status}</p> and then rendering the Select component but it does not work

import React from "react";
import Select from "react-select";
import { CDataTable } from "@coreui/react";

...

  <CDataTable
    bordered
    clickableRows
    fields={fields}
    hover
    items={[...employeeData]}
    itemsPerPage={10}
    itemsPerPageSelect
    loading={tableLoader}
    onRowClick={(e) => rowSelectHandler(e)}
    pagination
    size="sm"
    sorter={{ resetable: true }}
    striped
    tableFilter={{
      placeholder: "Filter",
      label: "Search:",
    }}
    scopedSlots={{
      status: (item, index) => (
        <td style={{ width: "13%" }}>
          <Select
            id={item.index}
            placeholder="Select Status"
            isSearchable={false}
            className="basic-single"
            onChange={(e) => selectChangeHandler(e.value, index)}
            classNamePrefix="select"
            defaultValue={{
              label: item.status,
              value: item.status,
              color: getBadge(item.status),
            }}
            // name="color"
            // inputValue={item.status}
            options={[
              {
                value: "ACTIVE",
                label: "ACTIVE",
                color: "#2eb85c",
              },
              {
                value: "DEACTIVE",
                label: "DEACTIVE",
                color: "#e55353",
              },
            ]}
            styles={colourStyles}
          />
        </td>
      ),
    }}
  />
...

Edit

Accepting answers with antd-select also if it works with coreui-datatable

1 Answers

I managed to put together a codesandbox and also was able to find the reason it does not work, and fix it.

The Problem:

https://codesandbox.io/s/twilight-butterfly-itppu?file=/src/App.js

I guess this Select component has some internal state initialised with defaultValue. Then the table when sorting changes the index(it sorts the data array) and you are using the index as id, so react reuses the same element but is unable to update the value because you are only providing defaultValue.

The Solution(s):

basically you should use value instead of defaultValue in the Select.

https://codesandbox.io/s/goofy-browser-b02xb?file=/src/App.js

OR

add a key based on some unique property of the item (not the index in the array).

https://codesandbox.io/s/zen-sky-56vly?file=/src/App.js

Related