1
1
import React , { Component , createRef , Fragment } from 'react' ;
2
- import PropTypes from 'prop-types' ;
3
- import ReactSelect , { components } from 'react-select' ;
4
- import customStyles from './select-styles' ;
5
2
import isEqual from 'lodash/isEqual' ;
6
3
import './react-select.scss' ;
7
4
8
- /**
9
- * New implementation of select of PF3
10
- */
11
-
12
5
import NewSelect from '@data-driven-forms/common/src/select' ;
13
6
import { DropdownButton } from 'patternfly-react' ;
14
7
import fnToString from '@data-driven-forms/common/src/utils/fn-to-string' ;
@@ -18,174 +11,6 @@ import DropdownIndicator from './dropdown-indicator';
18
11
import ClearIndicator from './clear-indicator' ;
19
12
import './react-select.scss' ;
20
13
21
- const ValueContainer = ( { children, ...props } ) => {
22
- if ( props . isMulti ) {
23
- return (
24
- < components . ValueContainer { ...props } >
25
- < div className = "pf3-select_multi-values-wrapper" >
26
- { children [ 0 ] }
27
- </ div >
28
- { children . slice ( 1 ) }
29
- </ components . ValueContainer >
30
- ) ;
31
- }
32
-
33
- return (
34
- < components . ValueContainer { ...props } >
35
- { children }
36
- </ components . ValueContainer >
37
- ) ;
38
- } ;
39
-
40
- class Select extends Component {
41
- constructor ( props ) {
42
- super ( props ) ;
43
- this . state = {
44
- isLoading : false ,
45
- options : props . options || [ ] ,
46
- } ;
47
- }
48
-
49
- updateOptions = ( ) => {
50
- const { loadOptions } = this . props ;
51
-
52
- this . setState ( { isLoading : true } ) ;
53
-
54
- return loadOptions ( )
55
- . then ( ( data ) => {
56
- if ( ! data . map ( ( { value } ) => value ) . includes ( this . props . input . value ) ) {
57
- this . props . input . onChange ( undefined ) ;
58
- }
59
-
60
- return this . setState ( {
61
- options : data ,
62
- isLoading : false ,
63
- } ) ;
64
- } ) ;
65
- }
66
-
67
- componentDidMount ( ) {
68
- const { loadOptions } = this . props ;
69
-
70
- if ( loadOptions ) {
71
- return this . updateOptions ( ) ;
72
- }
73
- }
74
-
75
- componentDidUpdate ( prevProps ) {
76
- if ( ! isEqual ( this . props . options , prevProps . options ) ) {
77
- if ( ! this . props . options . map ( ( { value } ) => value ) . includes ( this . props . input . value ) ) {
78
- this . props . input . onChange ( undefined ) ;
79
- }
80
-
81
- this . setState ( { options : this . props . options } ) ;
82
- }
83
-
84
- if ( this . props . loadOptions && fnToString ( this . props . loadOptions ) !== fnToString ( prevProps . loadOptions ) ) {
85
- return this . updateOptions ( ) ;
86
- }
87
- }
88
-
89
- render ( ) {
90
- const {
91
- invalid,
92
- input,
93
- placeholder,
94
- isSearchable,
95
- isDisabled,
96
- isReadOnly,
97
- loadingMessage,
98
- simpleValue,
99
- options : _options , // catch options from props, if they are undefined (bcs they would overwrite the state)
100
- pluckSingleValue,
101
- ...rest
102
- } = this . props ;
103
-
104
- const { options, isLoading } = this . state ;
105
- const { value, ...inputRest } = input ;
106
-
107
- if ( isLoading ) {
108
- return < ReactSelect
109
- className = { `final-form-select ${ invalid ? 'has-error' : '' } ` }
110
- styles = { customStyles }
111
- placeholder = { loadingMessage }
112
- isDisabled = { true }
113
- /> ;
114
- }
115
-
116
- const selectValue = pluckSingleValue ? rest . multi ? value : Array . isArray ( value ) && value [ 0 ] ? value [ 0 ] : value : value ;
117
-
118
- return < ReactSelect
119
- className = { `final-form-select ${ invalid ? 'has-error' : '' } ` }
120
- styles = { customStyles }
121
- { ...inputRest }
122
- options = { options }
123
- placeholder = { placeholder || 'Please choose' }
124
- isMulti = { rest . multi }
125
- isSearchable = { ! ! isSearchable }
126
- isClearable = { false }
127
- hideSelectedOptions = { false }
128
- closeMenuOnSelect = { ! rest . multi }
129
- noOptionsMessage = { ( ) => 'No option found' }
130
- isDisabled = { isDisabled || isReadOnly }
131
- components = { {
132
- ValueContainer,
133
- } }
134
- onChange = { option => simpleValue
135
- ? input . onChange ( rest . multi
136
- ? option . map ( item => item . value )
137
- : option ? option . value : undefined )
138
- : input . onChange ( option ) }
139
- value = { simpleValue ? options . filter ( ( { value } ) => rest . multi ? input . value . includes ( value ) : isEqual ( value , selectValue ) ) : selectValue }
140
- { ...rest }
141
- /> ;
142
- }
143
- }
144
-
145
- Select . propTypes = {
146
- simpleValue : PropTypes . bool ,
147
- loadOptions : PropTypes . func ,
148
- options : PropTypes . arrayOf ( PropTypes . shape ( {
149
- label : PropTypes . string ,
150
- value : PropTypes . oneOfType ( [
151
- PropTypes . string ,
152
- PropTypes . number ,
153
- PropTypes . array ,
154
- ] ) ,
155
- } ) ) ,
156
- invalid : PropTypes . oneOfType ( [
157
- PropTypes . string ,
158
- PropTypes . bool ,
159
- ] ) ,
160
- input : PropTypes . shape ( {
161
- onChange : PropTypes . func . isRequired ,
162
- value : PropTypes . oneOfType ( [
163
- PropTypes . string ,
164
- PropTypes . array ,
165
- PropTypes . any ,
166
- ] ) ,
167
- } ) ,
168
- initialValue : PropTypes . any ,
169
- placeholder : PropTypes . string ,
170
- rest : PropTypes . any ,
171
- isSearchable : PropTypes . bool ,
172
- isDisabled : PropTypes . bool ,
173
- isReadOnly : PropTypes . bool ,
174
- loadingMessage : PropTypes . string ,
175
- pluckSingleValue : PropTypes . bool ,
176
- } ;
177
-
178
- Select . defaultProps = {
179
- input : {
180
- value : [ ] ,
181
- } ,
182
- loadingMessage : 'Loading...' ,
183
- simpleValue : true ,
184
- pluckSingleValue : true ,
185
- } ;
186
-
187
- export default Select ;
188
-
189
14
const getDropdownText = ( value , placeholder , options ) => {
190
15
if ( Array . isArray ( value ) ) {
191
16
if ( value . length === 0 ) {
@@ -252,7 +77,7 @@ const SelectTitle = ({ title, classNamePrefix, isClearable, value, onClear, isFe
252
77
</ Fragment >
253
78
) ;
254
79
255
- export class P3Select extends Component { constructor ( props ) {
80
+ class Select extends Component { constructor ( props ) {
256
81
super ( props ) ;
257
82
this . state = {
258
83
isFetching : false ,
@@ -310,7 +135,7 @@ export class P3Select extends Component { constructor(props){
310
135
return true ;
311
136
}
312
137
313
- if ( isEqual ( this . state . options , nextState . options ) ) {
138
+ if ( ! isEqual ( this . state . options , nextState . options ) ) {
314
139
return true ;
315
140
}
316
141
@@ -408,6 +233,8 @@ export class P3Select extends Component { constructor(props){
408
233
}
409
234
}
410
235
411
- P3Select . defaultProps = {
236
+ Select . defaultProps = {
412
237
placeholder : 'Search...' ,
413
238
} ;
239
+
240
+ export default Select ;
0 commit comments