react-custom-scrollbars icon indicating copy to clipboard operation
react-custom-scrollbars copied to clipboard

React-custom-scrollbars with react-select

Open Raghava19 opened this issue 4 years ago • 1 comments

Hi, How to use, React custom scroll bar with React Select. https://github.com/malte-wessel/react-custom-scrollbars/pull/305

Now my code is like this:

import React from 'react'; import './App.css'; import Select from 'react-select'; import { Scrollbars } from 'react-custom-scrollbars';

const options = [ { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' }, { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' }, { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' }, { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' }, { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' } ];

class App extends React.Component { state = { selectedOption: null, }; handleChange = selectedOption => { this.setState({ selectedOption }); console.log(Option selected:, selectedOption); }; render() { const { selectedOption } = this.state;

return (
<div>
<Scrollbars style={{ width: 500, height: 300 }}>
   <Select
     value={selectedOption}
     onChange={this.handleChange}
     options={options}
   />
  </Scrollbars>
  </div>
);

} }

Raghava19 avatar Aug 21 '19 13:08 Raghava19

You need to replace the MenuList component in react-select with a custom one that uses react-custom-scrollbars.

// CustomMenuList.tsx
import React from "react";
import { Scrollbars } from "react-custom-scrollbars";

export const MenuList = (props: any) => {
  return (
    <div style={{ height: 200 }}>
      <Scrollbars renderThumbVertical={renderThumbVertical}>
        {props.children}
      </Scrollbars>
    </div>
  );
};

// scrollbar styles
function renderThumbVertical({ style, ...props }: { style: any }) {
  return (
    <div
      {...props}
      style={{
        ...style,
        backgroundColor: "red",
        width: "0.3rem",
        opacity: "1",
      }}
    />
  );
}

Then just add the above custom component to your Select and set captureMenuScroll to false:

import { MenuList } from "./CustomMenuList"
...
<Select 
  components={{ MenuList }}
  captureMenuScroll={false}
/>

cseas avatar Sep 11 '20 23:09 cseas