11/* eslint react/require-default-props: 0 */
22/* eslint no-return-assign: 0 */
33/* eslint react/no-unused-prop-types: 0 */
4+ /* eslint class-methods-use-this: 0 */
45import React , { Component } from 'react' ;
56import PropTypes from 'prop-types' ;
67import { LIKE , EQ } from '../comparison' ;
@@ -44,7 +45,8 @@ class SelectFilter extends Component {
4445 constructor ( props ) {
4546 super ( props ) ;
4647 this . filter = this . filter . bind ( this ) ;
47- const isSelected = getOptionValue ( props . options , this . getDefaultValue ( ) ) !== undefined ;
48+ this . options = this . getOptions ( props ) ;
49+ const isSelected = getOptionValue ( this . options , this . getDefaultValue ( ) ) !== undefined ;
4850 this . state = { isSelected } ;
4951 }
5052
@@ -69,19 +71,30 @@ class SelectFilter extends Component {
6971
7072 componentDidUpdate ( prevProps ) {
7173 let needFilter = false ;
72- if ( this . props . defaultValue !== prevProps . defaultValue ) {
74+ const {
75+ column,
76+ onFilter,
77+ defaultValue
78+ } = this . props ;
79+ const nextOptions = this . getOptions ( this . props ) ;
80+ if ( defaultValue !== prevProps . defaultValue ) {
7381 needFilter = true ;
74- } else if ( ! optionsEquals ( this . props . options , prevProps . options ) ) {
82+ } else if ( ! optionsEquals ( nextOptions , this . options ) ) {
83+ this . options = nextOptions ;
7584 needFilter = true ;
7685 }
7786 if ( needFilter ) {
7887 const value = this . selectInput . value ;
7988 if ( value ) {
80- this . props . onFilter ( this . props . column , FILTER_TYPE . SELECT ) ( value ) ;
89+ onFilter ( column , FILTER_TYPE . SELECT ) ( value ) ;
8190 }
8291 }
8392 }
8493
94+ getOptions ( props ) {
95+ return typeof props . options === 'function' ? props . options ( props . column ) : props . options ;
96+ }
97+
8598 getDefaultValue ( ) {
8699 const { filterState, defaultValue } = this . props ;
87100 if ( filterState && typeof filterState . filterVal !== 'undefined' ) {
@@ -90,25 +103,6 @@ class SelectFilter extends Component {
90103 return defaultValue ;
91104 }
92105
93- getOptions ( ) {
94- const optionTags = [ ] ;
95- const { options, placeholder, column, withoutEmptyOption } = this . props ;
96- if ( ! withoutEmptyOption ) {
97- optionTags . push ( (
98- < option key = "-1" value = "" > { placeholder || `Select ${ column . text } ...` } </ option >
99- ) ) ;
100- }
101- if ( Array . isArray ( options ) ) {
102- options . forEach ( ( { value, label } ) =>
103- optionTags . push ( < option key = { value } value = { value } > { label } </ option > ) ) ;
104- } else {
105- Object . keys ( options ) . forEach ( key =>
106- optionTags . push ( < option key = { key } value = { key } > { options [ key ] } </ option > )
107- ) ;
108- }
109- return optionTags ;
110- }
111-
112106 cleanFiltered ( ) {
113107 const value = ( this . props . defaultValue !== undefined ) ? this . props . defaultValue : '' ;
114108 this . setState ( ( ) => ( { isSelected : value !== '' } ) ) ;
@@ -128,6 +122,26 @@ class SelectFilter extends Component {
128122 this . props . onFilter ( this . props . column , FILTER_TYPE . SELECT ) ( value ) ;
129123 }
130124
125+ renderOptions ( ) {
126+ const optionTags = [ ] ;
127+ const { options } = this ;
128+ const { placeholder, column, withoutEmptyOption } = this . props ;
129+ if ( ! withoutEmptyOption ) {
130+ optionTags . push ( (
131+ < option key = "-1" value = "" > { placeholder || `Select ${ column . text } ...` } </ option >
132+ ) ) ;
133+ }
134+ if ( Array . isArray ( options ) ) {
135+ options . forEach ( ( { value, label } ) =>
136+ optionTags . push ( < option key = { value } value = { value } > { label } </ option > ) ) ;
137+ } else {
138+ Object . keys ( options ) . forEach ( key =>
139+ optionTags . push ( < option key = { key } value = { key } > { options [ key ] } </ option > )
140+ ) ;
141+ }
142+ return optionTags ;
143+ }
144+
131145 render ( ) {
132146 const {
133147 style,
@@ -163,7 +177,7 @@ class SelectFilter extends Component {
163177 onClick = { e => e . stopPropagation ( ) }
164178 defaultValue = { this . getDefaultValue ( ) || '' }
165179 >
166- { this . getOptions ( ) }
180+ { this . renderOptions ( ) }
167181 </ select >
168182 </ label >
169183 ) ;
0 commit comments