| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- // Libraries
- import classNames from 'classnames';
- import React, { PureComponent } from 'react';
- // Ignoring because I couldn't get @types/react-select work wih Torkel's fork
- // @ts-ignore
- import { default as ReactSelect } from '@torkelo/react-select';
- // @ts-ignore
- import { default as ReactAsyncSelect } from '@torkelo/react-select/lib/Async';
- // @ts-ignore
- import { components } from '@torkelo/react-select';
- // Components
- import { SelectOption, SingleValue } from './SelectOption';
- import SelectOptionGroup from './SelectOptionGroup';
- import IndicatorsContainer from './IndicatorsContainer';
- import NoOptionsMessage from './NoOptionsMessage';
- import resetSelectStyles from './resetSelectStyles';
- import { CustomScrollbar } from '../CustomScrollbar/CustomScrollbar';
- import { PopperContent } from '../Tooltip/PopperController';
- import { Tooltip } from '../Tooltip/Tooltip';
- export interface SelectOptionItem<T> {
- label?: string;
- value?: T;
- imgUrl?: string;
- description?: string;
- [key: string]: any;
- }
- export interface CommonProps<T> {
- defaultValue?: any;
- getOptionLabel?: (item: SelectOptionItem<T>) => string;
- getOptionValue?: (item: SelectOptionItem<T>) => string;
- onChange: (item: SelectOptionItem<T>) => {} | void;
- placeholder?: string;
- width?: number;
- value?: SelectOptionItem<T>;
- className?: string;
- isDisabled?: boolean;
- isSearchable?: boolean;
- isClearable?: boolean;
- autoFocus?: boolean;
- openMenuOnFocus?: boolean;
- onBlur?: () => void;
- maxMenuHeight?: number;
- isLoading?: boolean;
- noOptionsMessage?: () => string;
- isMulti?: boolean;
- backspaceRemovesValue?: boolean;
- isOpen?: boolean;
- components?: any;
- tooltipContent?: PopperContent<any>;
- onOpenMenu?: () => void;
- onCloseMenu?: () => void;
- }
- export interface SelectProps<T> extends CommonProps<T> {
- options: Array<SelectOptionItem<T>>;
- }
- interface AsyncProps<T> extends CommonProps<T> {
- defaultOptions: boolean;
- loadOptions: (query: string) => Promise<Array<SelectOptionItem<T>>>;
- loadingMessage?: () => string;
- }
- const wrapInTooltip = (
- component: React.ReactElement,
- tooltipContent: PopperContent<any> | undefined,
- isMenuOpen: boolean | undefined
- ) => {
- const showTooltip = isMenuOpen ? false : undefined;
- if (tooltipContent) {
- return (
- <Tooltip show={showTooltip} content={tooltipContent} placement="bottom">
- <div>
- {/* div needed for tooltip */}
- {component}
- </div>
- </Tooltip>
- );
- } else {
- return <div>{component}</div>;
- }
- };
- export const MenuList = (props: any) => {
- return (
- <components.MenuList {...props}>
- <CustomScrollbar autoHide={false} autoHeightMax="inherit">
- {props.children}
- </CustomScrollbar>
- </components.MenuList>
- );
- };
- export class Select<T> extends PureComponent<SelectProps<T>> {
- static defaultProps: Partial<SelectProps<any>> = {
- className: '',
- isDisabled: false,
- isSearchable: true,
- isClearable: false,
- isMulti: false,
- openMenuOnFocus: false,
- autoFocus: false,
- isLoading: false,
- backspaceRemovesValue: true,
- maxMenuHeight: 300,
- components: {
- Option: SelectOption,
- SingleValue,
- IndicatorsContainer,
- MenuList,
- Group: SelectOptionGroup,
- },
- };
- onOpenMenu = () => {
- const { onOpenMenu } = this.props;
- if (onOpenMenu) {
- onOpenMenu();
- }
- };
- onCloseMenu = () => {
- const { onCloseMenu } = this.props;
- if (onCloseMenu) {
- onCloseMenu();
- }
- };
- render() {
- const {
- defaultValue,
- getOptionLabel,
- getOptionValue,
- onChange,
- options,
- placeholder,
- width,
- value,
- className,
- isDisabled,
- isLoading,
- isSearchable,
- isClearable,
- backspaceRemovesValue,
- isMulti,
- autoFocus,
- openMenuOnFocus,
- onBlur,
- maxMenuHeight,
- noOptionsMessage,
- isOpen,
- components,
- tooltipContent,
- } = this.props;
- let widthClass = '';
- if (width) {
- widthClass = 'width-' + width;
- }
- const selectClassNames = classNames('gf-form-input', 'gf-form-input--form-dropdown', widthClass, className);
- const selectComponents = { ...Select.defaultProps.components, ...components };
- return wrapInTooltip(
- <ReactSelect
- classNamePrefix="gf-form-select-box"
- className={selectClassNames}
- components={selectComponents}
- defaultValue={defaultValue}
- value={value}
- getOptionLabel={getOptionLabel}
- getOptionValue={getOptionValue}
- menuShouldScrollIntoView={false}
- isSearchable={isSearchable}
- onChange={onChange}
- options={options}
- placeholder={placeholder || 'Choose'}
- styles={resetSelectStyles()}
- isDisabled={isDisabled}
- isLoading={isLoading}
- isClearable={isClearable}
- autoFocus={autoFocus}
- onBlur={onBlur}
- openMenuOnFocus={openMenuOnFocus}
- maxMenuHeight={maxMenuHeight}
- noOptionsMessage={noOptionsMessage}
- isMulti={isMulti}
- backspaceRemovesValue={backspaceRemovesValue}
- menuIsOpen={isOpen}
- onMenuOpen={this.onOpenMenu}
- onMenuClose={this.onCloseMenu}
- />,
- tooltipContent,
- isOpen
- );
- }
- }
- export class AsyncSelect<T> extends PureComponent<AsyncProps<T>> {
- static defaultProps: Partial<AsyncProps<any>> = {
- className: '',
- components: {},
- loadingMessage: () => 'Loading...',
- isDisabled: false,
- isClearable: false,
- isMulti: false,
- isSearchable: true,
- backspaceRemovesValue: true,
- autoFocus: false,
- openMenuOnFocus: false,
- maxMenuHeight: 300,
- };
- render() {
- const {
- defaultValue,
- getOptionLabel,
- getOptionValue,
- onChange,
- placeholder,
- width,
- value,
- className,
- loadOptions,
- defaultOptions,
- isLoading,
- loadingMessage,
- noOptionsMessage,
- isDisabled,
- isSearchable,
- isClearable,
- backspaceRemovesValue,
- autoFocus,
- onBlur,
- openMenuOnFocus,
- maxMenuHeight,
- isMulti,
- tooltipContent,
- } = this.props;
- let widthClass = '';
- if (width) {
- widthClass = 'width-' + width;
- }
- const selectClassNames = classNames('gf-form-input', 'gf-form-input--form-dropdown', widthClass, className);
- return wrapInTooltip(
- <ReactAsyncSelect
- classNamePrefix="gf-form-select-box"
- className={selectClassNames}
- components={{
- Option: SelectOption,
- SingleValue,
- IndicatorsContainer,
- NoOptionsMessage,
- }}
- defaultValue={defaultValue}
- value={value}
- getOptionLabel={getOptionLabel}
- getOptionValue={getOptionValue}
- menuShouldScrollIntoView={false}
- onChange={onChange}
- loadOptions={loadOptions}
- isLoading={isLoading}
- defaultOptions={defaultOptions}
- placeholder={placeholder || 'Choose'}
- styles={resetSelectStyles()}
- loadingMessage={loadingMessage}
- noOptionsMessage={noOptionsMessage}
- isDisabled={isDisabled}
- isSearchable={isSearchable}
- isClearable={isClearable}
- autoFocus={autoFocus}
- onBlur={onBlur}
- openMenuOnFocus={openMenuOnFocus}
- maxMenuHeight={maxMenuHeight}
- isMulti={isMulti}
- backspaceRemovesValue={backspaceRemovesValue}
- />,
- tooltipContent,
- false
- );
- }
- }
- export default Select;
|