@@ -5,39 +5,46 @@ import ApiClient from '../../../utils/api-client'
5
5
import { FilterPropertyProps , SelectRecord } from '../base-property-props'
6
6
7
7
type CombinedProps = FilterPropertyProps
8
+ type FilterState = {
9
+ options : Array < SelectRecord >
10
+ }
8
11
9
- class Filter extends React . PureComponent < CombinedProps > {
12
+ class Filter extends React . PureComponent < CombinedProps , FilterState > {
10
13
private api : ApiClient
11
14
12
- private options : Array < SelectRecord >
13
-
14
15
constructor ( props : CombinedProps ) {
15
16
super ( props )
16
17
this . api = new ApiClient ( )
17
- this . options = [ ]
18
18
this . loadOptions = this . loadOptions . bind ( this )
19
19
this . handleChange = this . handleChange . bind ( this )
20
+ this . state = {
21
+ options : [ ] ,
22
+ }
20
23
}
21
24
22
25
handleChange ( selected : SelectRecord ) : void {
23
26
const { onChange, property } = this . props
24
27
onChange ( property . path , selected ? selected . value : '' )
25
28
}
26
29
27
- async loadOptions ( inputValue : string ) : Promise < Array < { value : string ; label : string } > > {
30
+ async loadOptions ( inputValue : string ) : Promise < Array < { value : string | number ; label : string } > > {
28
31
const { property } = this . props
29
32
const records = await this . api . searchRecords ( {
30
33
resourceId : property . reference as string ,
31
34
query : inputValue ,
32
35
} )
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
35
41
}
36
42
37
43
render ( ) : ReactNode {
38
44
const { property, filter } = this . props
45
+ const { options } = this . state
39
46
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 ) )
41
48
return (
42
49
< FormGroup >
43
50
< Label > { property . label } </ Label >
0 commit comments