-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
index.tsx
584 lines (457 loc) · 17.7 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import React, { Fragment } from 'react';
import { Helmet } from 'react-helmet';
import md from '../../markdown/renderer';
import ExampleWrapper from '../../ExampleWrapper';
import {
CustomSelectProps,
CustomClearIndicator,
CustomDropdownIndicator,
CustomLoadingIndicator,
CustomLoadingMessage,
CustomIndicatorsContainer,
CustomIndicatorSeparator,
CustomNoOptionsMessage,
CustomMultiValueContainer,
CustomMultiValueRemove,
CustomMultiValueLabel,
CustomControl,
CustomGroup,
CustomInput,
CustomOption,
CustomMenu,
CustomMenuList,
CustomPlaceholder,
CustomSelectContainer,
CustomSingleValue,
CustomGroupHeading,
CustomValueContainer,
CustomMenuPlacer,
} from '../../examples';
export default function Components() {
return (
<Fragment>
<Helmet>
<title>Components - React Select</title>
<meta
name="description"
content="The main feature of this library is providing consumers with the building blocks necessary to create their component."
/>
</Helmet>
{md`
# Components
The main feature of this library is providing consumers with the
building blocks necessary to create _their_ component.
The following components are customizable and switchable:
* ClearIndicator
* Control
* DropdownIndicator
* DownChevron
* CrossIcon
* Group
* GroupHeading
* IndicatorsContainer
* IndicatorSeparator
* Input
* LoadingIndicator
* Menu
* MenuList
* MenuPlacer
* MenuPortal
* LoadingMessage
* NoOptionsMessage
* MultiValue
* MultiValueContainer
* MultiValueLabel
* MultiValueRemove
* Option
* Placeholder
* SelectContainer
* SingleValue
* ValueContainer
## Replacing Components
React-Select allows you to augment layout and functionality by replacing
the default components with your own, using the \`components\`
property. These components are given all the current props and state
letting you achieve anything you dream up.
## Inner Ref
Some components also take an innerRef prop that react-select needs in
order to manage internal behaviour. Please assign this to the ref
property of the relevant react-element. For example:
~~~~
const CustomOption = ({ innerRef, innerProps }) => (
<div ref={innerRef} {...innerProps} />)
~~~~
### Inner Props
All functional properties that the component needs are provided in
\`innerProps\` which you must spread.
### Common Props
Every component receives \`commonProps\` which are spread onto
the component. These include:
* \`clearValue\`
* \`getStyles\`
* \`getValue\`
* \`hasValue\`
* \`isMulti\`
* \`isRtl\`
* \`options\`
* \`selectOption\`
* \`setValue\`
* \`selectProps\`
~~~jsx
import React from 'react';
import Select from 'react-select';
const CustomOption = ({ innerProps, isDisabled }) =>
!isDisabled ? (
<div {...innerProps}>{/* your component internals */}</div>
) : null;
class Component extends React.Component {
render() {
return <Select components={{ Option: CustomOption }} />;
}
}
~~~
## Defining components
When defining replacement components, it is important to do so __outside__ the scope of
rendering the Select. Defining a replacement component directly in the components prop can
cause issues.
On this topic, React
[documentation](https://reactjs.org/docs/higher-order-components.html#dont-use-hocs-inside-the-render-method)
has the following to say:
> The problem here isn’t just about performance — remounting a component causes the state
of that component and all of its children to be lost.
This statement applies as well when replacing components in react-select with inline definitions.
~~~jsx
// Bad: Inline declaration will cause remounting issues
const BadSelect = props => (
<Select {...props} components={{
Control: ({ children, ...rest }) => (
<components.Control {...rest}>
👎 {children}
</components.Control>
)}}
/>
)
// Good: Custom component declared outside of the Select scope
const Control = ({ children, ...props }) => (
<components.Control {...props}>
👍 {children}
</components.Control>
);
const GoodSelect = props => <Select {...props} components={{ Control }} />
~~~
There will likely be times that data or methods may need to be shared,
but this can be achieved with the \`selectProps\` prop passed to each component.
${(
<ExampleWrapper
label="Custom Component with selectProps Example"
urlPath="docs/examples/CustomSelectProps.tsx"
raw={require('!!raw-loader!../../examples/CustomSelectProps.tsx')}
>
<CustomSelectProps />
</ExampleWrapper>
)}
~~~
~~~
## Adjusting the Styling
The \`styles\` prop allows you to pass styles to a particular component, without
replacing the entire component. If you only want to change styling, you should
start by using the \`styles\` prop.
## Replaceable components
### ClearIndicator
The indicator presented to clear the values from the component. The default
component is a cross. The conditions under which the indicator will not be
rendered when:
* When \`isClearable\` is false, or when \`isMulti\` is false, and \`isClearable\` is undefined
* When the select is disabled
* When the select has no value
* When the select is loading
See [props docs](/props#clearindicator) for more details
${(
<ExampleWrapper
label="Custom ClearIndicator Example"
urlPath="docs/examples/CustomClearIndicator.tsx"
raw={require('!!raw-loader!../../examples/CustomClearIndicator.tsx')}
>
<CustomClearIndicator />
</ExampleWrapper>
)}
### Control
The second highest level wrapper around the components. It is responsible for the
positioning of the \`ValueContainer\` and \`IndicatorsContainer\`. It is followed
by the Menu.
See [props docs](/props#control) for more details
${(
<ExampleWrapper
label="Custom Control Example"
urlPath="docs/examples/CustomControl.tsx"
raw={require('!!raw-loader!../../examples/CustomControl.tsx')}
>
<CustomControl />
</ExampleWrapper>
)}
### Dropdown Indicator
The indicator for opening the select, designed to indicate to users that
this is a select. By default it is a chevron pointed down.
See [props docs](/props#dropdownindicator) for more details
${(
<ExampleWrapper
label="Custom Dropdown Indicator Example"
urlPath="docs/examples/CustomDropdownIndicator.tsx"
raw={require('!!raw-loader!../../examples/CustomDropdownIndicator.tsx')}
>
<CustomDropdownIndicator />
</ExampleWrapper>
)}
### Group
The wrapper around each group if the Select has groups in its data. The default
component is responsible both for mapping its options, as well as rendering
its data into the GroupHeading.
See [props docs](/props#group) for more details
${(
<ExampleWrapper
label="Custom Group Example"
urlPath="docs/examples/CustomGroup.tsx"
raw={require('!!raw-loader!../../examples/CustomGroup.tsx')}
>
<CustomGroup />
</ExampleWrapper>
)}
### GroupHeading
Component that renders the data of a group.
See [props docs](/props#groupheading) for more details
${(
<ExampleWrapper
label="Custom GroupHeading Example"
urlPath="docs/examples/CustomGroupHeading.tsx"
raw={require('!!raw-loader!../../examples/CustomGroupHeading.tsx')}
>
<CustomGroupHeading />
</ExampleWrapper>
)}
### IndicatorsContainer
Wraps the indicators. This is one of the two components directly under the
control. The indicators that \`react-select\` will check to render by are:
* Clear Indicator
* Loading Indicator
* Dropdown Indicator
See [props docs](/props#indicatorscontainer) for more details
${(
<ExampleWrapper
label="Custom IndicatorsContainer Example"
urlPath="docs/examples/CustomIndicatorsContainer.tsx"
raw={require('!!raw-loader!../../examples/CustomIndicatorsContainer.tsx')}
>
<CustomIndicatorsContainer />
</ExampleWrapper>
)}
### Indicator Separator
Component directly to the the inner side of the Dropdown Indicator. By default
it is a line to act as a visual separator.
See [props docs](/props#customindicatorseparator) for more details
${(
<ExampleWrapper
label="Custom IndicatorSeparator Example"
urlPath="docs/examples/CustomIndicatorSeparator.tsx"
raw={require('!!raw-loader!../../examples/CustomIndicatorSeparator.tsx')}
>
<CustomIndicatorSeparator />
</ExampleWrapper>
)}
### Input
Input to render when an input is required. If the select is not searchable,
a dummy input is rendered instead. If the select is disabled, a div of the
correct size and shape is rendered.
All provided inputs are given aria attributes to ensure the input is accessible
by default.
See [props docs](/props#input) for more details
${(
<ExampleWrapper
label="Custom Input Example"
urlPath="docs/examples/CustomInput.tsx"
raw={require('!!raw-loader!../../examples/CustomInput.tsx')}
>
<CustomInput />
</ExampleWrapper>
)}
### LoadingIndicator
Loading indicator to be displayed in the Indicators Container when \`isLoading\`
is true. By default it is three dots.
See [props docs](/props#loadingindicator) for more details
${(
<ExampleWrapper
label="Custom LoadingIndicator Example"
urlPath="docs/examples/CustomLoadingIndicator.tsx"
raw={require('!!raw-loader!../../examples/CustomLoadingIndicator.tsx')}
>
<CustomLoadingIndicator />
</ExampleWrapper>
)}
### Menu
The wrapper for the dropdown menu in the select. It is responsible for wrapping
the menu items. If you want to modify the options themselves, you should use
the \`Option\` component.
See [props docs](/props#menu) for more details
${(
<ExampleWrapper
label="Custom Menu Example"
urlPath="docs/examples/CustomMenu.tsx"
raw={require('!!raw-loader!../../examples/CustomMenu.tsx')}
>
<CustomMenu />
</ExampleWrapper>
)}
### MenuList
Inner wrapper for the menu. It directly wraps around the returned options.
See [props docs](/props#menulist) for more details
${(
<ExampleWrapper
label="Custom MenuList Example"
urlPath="docs/examples/CustomMenuList.tsx"
raw={require('!!raw-loader!../../examples/CustomMenuList.tsx')}
>
<CustomMenuList />
</ExampleWrapper>
)}
### MenuPlacer
The wrapper of the Menu. It allows to make custom logic to find the optimal placement of the Menu.
See [props docs](/props#menuPlacer) for more details
${(
<ExampleWrapper
label="Custom MenuPlacer Example"
urlPath="docs/examples/CustomMenuPlacer.tsx"
raw={require('!!raw-loader!../../examples/CustomMenuPlacer.tsx')}
>
<CustomMenuPlacer />
</ExampleWrapper>
)}
### LoadingMessage
Message to display in the menu when there are no options and \`isLoading\` is
true. By default it is 'Loading...'
See [props docs](/props#loadingmessage) for more details
${(
<ExampleWrapper
label="Custom LoadingMessage Example"
urlPath="docs/examples/CustomLoadingMessage.tsx"
raw={require('!!raw-loader!../../examples/CustomLoadingMessage.tsx')}
>
<CustomLoadingMessage />
</ExampleWrapper>
)}
### NoOptionsMessage
Message to be displayed in the menu if there are no options passed in.
See [props docs](/props#nooptionsmessage) for more details
${(
<ExampleWrapper
label="Custom NoOptionsMessage Example"
urlPath="docs/examples/CustomNoOptionsMessage.tsx"
raw={require('!!raw-loader!../../examples/CustomNoOptionsMessage.tsx')}
>
<CustomNoOptionsMessage />
</ExampleWrapper>
)}
### MultiValue
Component used to display a selected option in the input when \`isMulti\` is
true. Takes responsibility for rendering the \`MultiValueContainer\`,
\`MultiValueLabel\`, and \`MultiValueRemove\`.
### MultiValueContainer
Wraps the Label and Remove in a Multi Value
See [props docs](/props#multivaluecontainer) for more details
${(
<ExampleWrapper
label="Custom MultiValueContainer Example"
urlPath="docs/examples/CustomMultiValueContainer.tsx"
raw={require('!!raw-loader!../../examples/CustomMultiValueContainer.tsx')}
>
<CustomMultiValueContainer />
</ExampleWrapper>
)}
### MultiValueLabel
Receives the value of the option and is responsible for rendering it to the
input.
See [props docs](/props#multivaluelabel) for more details
${(
<ExampleWrapper
label="Custom MultiValueLabel Example"
urlPath="docs/examples/CustomMultiValueLabel.tsx"
raw={require('!!raw-loader!../../examples/CustomMultiValueLabel.tsx')}
>
<CustomMultiValueLabel />
</ExampleWrapper>
)}
### MultiValueRemove
Receives an onClick to remove the selected item. By default it is a cross.
See [props docs](/props#multivalueremove) for more details
${(
<ExampleWrapper
label="Custom MultiValueRemove Example"
urlPath="docs/examples/CustomMultiValueRemove.tsx"
raw={require('!!raw-loader!../../examples/CustomMultiValueRemove.tsx')}
>
<CustomMultiValueRemove />
</ExampleWrapper>
)}
### Option
Component responsible for displaying an option in the menu.
See [props docs](/props#option) for more details
${(
<ExampleWrapper
label="Custom Option Example"
urlPath="docs/examples/CustomOption.tsx"
raw={require('!!raw-loader!../../examples/CustomOption.tsx')}
>
<CustomOption />
</ExampleWrapper>
)}
### Placeholder
Component to be displayed in the input when nothing is selected. By default
it is the text 'Select...'
See [props docs](/props#placeholder) for more details
${(
<ExampleWrapper
label="Custom Placeholder Example"
urlPath="docs/examples/CustomPlaceholder.tsx"
raw={require('!!raw-loader!../../examples/CustomPlaceholder.tsx')}
>
<CustomPlaceholder />
</ExampleWrapper>
)}
### SelectContainer
The wrapper around the entire select component.
See [props docs](/props#selectcontainer) for more details
${(
<ExampleWrapper
label="Custom SelectContainer Example"
urlPath="docs/examples/CustomSelectContainer.tsx"
raw={require('!!raw-loader!../../examples/CustomSelectContainer.tsx')}
>
<CustomSelectContainer />
</ExampleWrapper>
)}
### SingleValue
The component that displays the selected value in the input for a single select.
See [props docs](/props#singlevalue) for more details
${(
<ExampleWrapper
label="Custom SingleValue Example"
urlPath="docs/examples/CustomSingleValue.tsx"
raw={require('!!raw-loader!../../examples/CustomSingleValue.tsx')}
>
<CustomSingleValue />
</ExampleWrapper>
)}
### ValueContainer
Container responsible for loading the placeholder value and the input.
See [props docs](/props#valuecontainer) for more details
${(
<ExampleWrapper
label="Custom ValueContainer Example"
urlPath="docs/examples/CustomValueContainer.tsx"
raw={require('!!raw-loader!../../examples/CustomValueContainer.tsx')}
>
<CustomValueContainer />
</ExampleWrapper>
)}
`}
</Fragment>
);
}