Select.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Libraries
  2. import classNames from 'classnames';
  3. import React, { PureComponent } from 'react';
  4. import { default as ReactSelect } from 'react-select';
  5. import { default as ReactAsyncSelect } from 'react-select/lib/Async';
  6. // Components
  7. import { Option, SingleValue } from './PickerOption';
  8. import IndicatorsContainer from './IndicatorsContainer';
  9. import NoOptionsMessage from './NoOptionsMessage';
  10. import ResetStyles from './ResetStyles';
  11. export interface SelectOptionItem {
  12. label?: string;
  13. value?: any;
  14. imgUrl?: string;
  15. description?: string;
  16. [key: string]: any;
  17. }
  18. interface CommonProps {
  19. defaultValue?: any;
  20. getOptionLabel?: (item: SelectOptionItem) => string;
  21. getOptionValue?: (item: SelectOptionItem) => string;
  22. onChange: (item: SelectOptionItem) => {} | void;
  23. placeholder?: string;
  24. width?: number;
  25. value?: any;
  26. className?: string;
  27. components: object;
  28. isDisabled?: boolean;
  29. isSearchable?: boolean;
  30. isClearable?: boolean;
  31. autoFocus?: boolean;
  32. openMenuOnFocus?: boolean;
  33. onBlur?: () => void;
  34. maxMenuHeight?: number;
  35. isLoading: boolean;
  36. noOptionsMessage?: () => string;
  37. isMulti?: boolean;
  38. backspaceRemovesValue: boolean;
  39. }
  40. interface SelectProps {
  41. options: SelectOptionItem[];
  42. }
  43. interface AsyncProps {
  44. defaultOptions: boolean;
  45. loadOptions: (query: string) => Promise<SelectOptionItem[]>;
  46. loadingMessage?: () => string;
  47. }
  48. export class Select extends PureComponent<CommonProps & SelectProps> {
  49. static defaultProps = {
  50. width: null,
  51. className: '',
  52. components: {},
  53. isDisabled: false,
  54. isSearchable: true,
  55. isClearable: false,
  56. isMulti: false,
  57. openMenuOnFocus: false,
  58. autoFocus: false,
  59. isLoading: false,
  60. backspaceRemovesValue: true,
  61. maxMenuHeight: 300,
  62. };
  63. render() {
  64. const {
  65. defaultValue,
  66. getOptionLabel,
  67. getOptionValue,
  68. onChange,
  69. options,
  70. placeholder,
  71. width,
  72. value,
  73. className,
  74. isDisabled,
  75. isLoading,
  76. isSearchable,
  77. isClearable,
  78. backspaceRemovesValue,
  79. isMulti,
  80. autoFocus,
  81. openMenuOnFocus,
  82. onBlur,
  83. maxMenuHeight,
  84. noOptionsMessage,
  85. } = this.props;
  86. let widthClass = '';
  87. if (width) {
  88. widthClass = 'width-' + width;
  89. }
  90. const selectClassNames = classNames('gf-form-input', 'gf-form-input--form-dropdown', widthClass, className);
  91. return (
  92. <ReactSelect
  93. classNamePrefix="gf-form-select-box"
  94. className={selectClassNames}
  95. components={{
  96. Option,
  97. SingleValue,
  98. IndicatorsContainer,
  99. }}
  100. defaultValue={defaultValue}
  101. value={value}
  102. getOptionLabel={getOptionLabel}
  103. getOptionValue={getOptionValue}
  104. menuShouldScrollIntoView={false}
  105. isSearchable={isSearchable}
  106. onChange={onChange}
  107. options={options}
  108. placeholder={placeholder || 'Choose'}
  109. styles={ResetStyles}
  110. isDisabled={isDisabled}
  111. isLoading={isLoading}
  112. isClearable={isClearable}
  113. autoFocus={autoFocus}
  114. onBlur={onBlur}
  115. openMenuOnFocus={openMenuOnFocus}
  116. maxMenuHeight={maxMenuHeight}
  117. noOptionsMessage={noOptionsMessage}
  118. isMulti={isMulti}
  119. backspaceRemovesValue={backspaceRemovesValue}
  120. />
  121. );
  122. }
  123. }
  124. export class AsyncSelect extends PureComponent<CommonProps & AsyncProps> {
  125. static defaultProps = {
  126. width: null,
  127. className: '',
  128. components: {},
  129. loadingMessage: () => 'Loading...',
  130. isDisabled: false,
  131. isClearable: false,
  132. isMulti: false,
  133. isSearchable: true,
  134. backspaceRemovesValue: true,
  135. autoFocus: false,
  136. openMenuOnFocus: false,
  137. maxMenuHeight: 300,
  138. };
  139. render() {
  140. const {
  141. defaultValue,
  142. getOptionLabel,
  143. getOptionValue,
  144. onChange,
  145. placeholder,
  146. width,
  147. value,
  148. className,
  149. loadOptions,
  150. defaultOptions,
  151. isLoading,
  152. loadingMessage,
  153. noOptionsMessage,
  154. isDisabled,
  155. isSearchable,
  156. isClearable,
  157. backspaceRemovesValue,
  158. autoFocus,
  159. onBlur,
  160. openMenuOnFocus,
  161. maxMenuHeight,
  162. isMulti,
  163. } = this.props;
  164. let widthClass = '';
  165. if (width) {
  166. widthClass = 'width-' + width;
  167. }
  168. const selectClassNames = classNames('gf-form-input', 'gf-form-input--form-dropdown', widthClass, className);
  169. return (
  170. <ReactAsyncSelect
  171. classNamePrefix="gf-form-select-box"
  172. className={selectClassNames}
  173. components={{
  174. Option,
  175. SingleValue,
  176. IndicatorsContainer,
  177. NoOptionsMessage,
  178. }}
  179. defaultValue={defaultValue}
  180. value={value}
  181. getOptionLabel={getOptionLabel}
  182. getOptionValue={getOptionValue}
  183. menuShouldScrollIntoView={false}
  184. onChange={onChange}
  185. loadOptions={loadOptions}
  186. isLoading={isLoading}
  187. defaultOptions={defaultOptions}
  188. placeholder={placeholder || 'Choose'}
  189. styles={ResetStyles}
  190. loadingMessage={loadingMessage}
  191. noOptionsMessage={noOptionsMessage}
  192. isDisabled={isDisabled}
  193. isSearchable={isSearchable}
  194. isClearable={isClearable}
  195. autoFocus={autoFocus}
  196. onBlur={onBlur}
  197. openMenuOnFocus={openMenuOnFocus}
  198. maxMenuHeight={maxMenuHeight}
  199. isMulti={isMulti}
  200. backspaceRemovesValue={backspaceRemovesValue}
  201. />
  202. );
  203. }
  204. }
  205. export default Select;