@@ -5,39 +5,46 @@ import ApiClient from '../../../utils/api-client'
55import { FilterPropertyProps , SelectRecord } from '../base-property-props'
66
77type CombinedProps = FilterPropertyProps
8+ type FilterState = {
9+ options : Array < SelectRecord >
10+ }
811
9- class Filter extends React . PureComponent < CombinedProps > {
12+ class Filter extends React . PureComponent < CombinedProps , FilterState > {
1013 private api : ApiClient
1114
12- private options : Array < SelectRecord >
13-
1415 constructor ( props : CombinedProps ) {
1516 super ( props )
1617 this . api = new ApiClient ( )
17- this . options = [ ]
1818 this . loadOptions = this . loadOptions . bind ( this )
1919 this . handleChange = this . handleChange . bind ( this )
20+ this . state = {
21+ options : [ ] ,
22+ }
2023 }
2124
2225 handleChange ( selected : SelectRecord ) : void {
2326 const { onChange, property } = this . props
2427 onChange ( property . path , selected ? selected . value : '' )
2528 }
2629
27- async loadOptions ( inputValue : string ) : Promise < Array < { value : string ; label : string } > > {
30+ async loadOptions ( inputValue : string ) : Promise < Array < { value : string | number ; label : string } > > {
2831 const { property } = this . props
2932 const records = await this . api . searchRecords ( {
3033 resourceId : property . reference as string ,
3134 query : inputValue ,
3235 } )
33- this . options = records . map ( ( r ) => ( { value : r . id , label : r . title } ) )
34- return this . options
36+ const options = records . map ( ( r ) => ( { value : r . id , label : r . title } ) )
37+ this . setState ( {
38+ options,
39+ } )
40+ return options
3541 }
3642
3743 render ( ) : ReactNode {
3844 const { property, filter } = this . props
45+ const { options } = this . state
3946 const value = typeof filter [ property . path ] === 'undefined' ? '' : filter [ property . path ]
40- const selected = ( this . options || [ ] ) . find ( ( o ) => o . value === value )
47+ const selected = ( options || [ ] ) . find ( ( o ) => String ( o . value ) === String ( value ) )
4148 return (
4249 < FormGroup >
4350 < Label > { property . label } </ Label >
0 commit comments