11import 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' ;
52import isEqual from 'lodash/isEqual' ;
63import './react-select.scss' ;
74
8- /**
9- * New implementation of select of PF3
10- */
11-
125import NewSelect from '@data-driven-forms/common/src/select' ;
136import { DropdownButton } from 'patternfly-react' ;
147import fnToString from '@data-driven-forms/common/src/utils/fn-to-string' ;
@@ -18,174 +11,6 @@ import DropdownIndicator from './dropdown-indicator';
1811import ClearIndicator from './clear-indicator' ;
1912import './react-select.scss' ;
2013
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-
18914const getDropdownText = ( value , placeholder , options ) => {
19015 if ( Array . isArray ( value ) ) {
19116 if ( value . length === 0 ) {
@@ -252,7 +77,7 @@ const SelectTitle = ({ title, classNamePrefix, isClearable, value, onClear, isFe
25277 </ Fragment >
25378) ;
25479
255- export class P3Select extends Component { constructor ( props ) {
80+ class Select extends Component { constructor ( props ) {
25681 super ( props ) ;
25782 this . state = {
25883 isFetching : false ,
@@ -310,7 +135,7 @@ export class P3Select extends Component { constructor(props){
310135 return true ;
311136 }
312137
313- if ( isEqual ( this . state . options , nextState . options ) ) {
138+ if ( ! isEqual ( this . state . options , nextState . options ) ) {
314139 return true ;
315140 }
316141
@@ -408,6 +233,8 @@ export class P3Select extends Component { constructor(props){
408233 }
409234}
410235
411- P3Select . defaultProps = {
236+ Select . defaultProps = {
412237 placeholder : 'Search...' ,
413238} ;
239+
240+ export default Select ;
0 commit comments