@@ -2,6 +2,7 @@ import React, { useCallback } from 'react';
2
2
import { debounce , TextField } from '@mui/material' ;
3
3
import Autocomplete from '@mui/material/Autocomplete' ;
4
4
import { ParameterSelectProps } from './ParameterSelect' ;
5
+ import { RenderSubValue } from '../../../report/ReportRecordProcessing' ;
5
6
6
7
const NodePropertyParameterSelectComponent = ( props : ParameterSelectProps ) => {
7
8
const suggestionsUpdateTimeout =
@@ -11,10 +12,22 @@ const NodePropertyParameterSelectComponent = (props: ParameterSelectProps) => {
11
12
? props . settings . defaultValue
12
13
: '' ;
13
14
15
+ const getInitialValue = ( value , multi ) => {
16
+ if ( value && Array . isArray ( value ) ) {
17
+ return multi ? value : null ;
18
+ } else if ( value ) {
19
+ return multi ? [ value ] : value ;
20
+ }
21
+ return multi ? [ ] : value ;
22
+ } ;
23
+ const { multiSelector } = props ;
14
24
const allParameters = props . allParameters ? props . allParameters : { } ;
15
25
const [ extraRecords , setExtraRecords ] = React . useState ( [ ] ) ;
16
- // const [inputText, setInputText] = React.useState(props.parameterValue);
17
- const [ inputDisplayText , setInputDisplayText ] = React . useState ( props . parameterDisplayValue ) ;
26
+ const [ inputDisplayText , setInputDisplayText ] = React . useState (
27
+ props . parameterDisplayValue && multiSelector ? '' : props . parameterDisplayValue
28
+ ) ;
29
+ const [ inputValue , setInputValue ] = React . useState ( getInitialValue ( props . parameterDisplayValue , multiSelector ) ) ;
30
+
18
31
const debouncedQueryCallback = useCallback ( debounce ( props . queryCallback , suggestionsUpdateTimeout ) , [ ] ) ;
19
32
const label = props . settings && props . settings . entityType ? props . settings . entityType : '' ;
20
33
const propertyType = props . settings && props . settings . propertyType ? props . settings . propertyType : '' ;
@@ -29,50 +42,83 @@ const NodePropertyParameterSelectComponent = (props: ParameterSelectProps) => {
29
42
30
43
const realValueRowIndex = props . compatibilityMode ? 0 : 1 - displayValueRowIndex ;
31
44
45
+ const handleCrossClick = ( isMulti , value ) => {
46
+ if ( isMulti ) {
47
+ if ( value . length == 0 && clearParameterOnFieldClear ) {
48
+ setInputValue ( [ ] ) ;
49
+ props . setParameterValue ( undefined ) ;
50
+ props . setParameterDisplayValue ( undefined ) ;
51
+ return ;
52
+ }
53
+ if ( value . length == 0 ) {
54
+ setInputValue ( [ ] ) ;
55
+ props . setParameterValue ( [ ] ) ;
56
+ props . setParameterDisplayValue ( [ ] ) ;
57
+
58
+ }
59
+ } else {
60
+ if ( value && clearParameterOnFieldClear ) {
61
+ setInputValue ( null ) ;
62
+ props . setParameterValue ( undefined ) ;
63
+ props . setParameterDisplayValue ( undefined ) ;
64
+ return ;
65
+ }
66
+ if ( value == null ) {
67
+ setInputValue ( null ) ;
68
+ props . setParameterValue ( defaultValue ) ;
69
+ props . setParameterDisplayValue ( defaultValue ) ;
70
+
71
+ }
72
+ }
73
+ } ;
74
+ const propagateSelection = ( event , newDisplay ) => {
75
+ const isMulti = Array . isArray ( newDisplay ) ;
76
+ handleCrossClick ( isMulti , newDisplay ) ;
77
+ let newValue ;
78
+ // Multiple and new entry
79
+ if ( isMulti && inputValue . length < newDisplay . length ) {
80
+ newValue = Array . isArray ( props . parameterValue ) ? [ ...props . parameterValue ] : [ props . parameterValue ] ;
81
+ const newDisplayValue = [ ...newDisplay ] . slice ( - 1 ) [ 0 ] ;
82
+
83
+ let val = extraRecords . filter ( ( r ) => r . _fields [ displayValueRowIndex ] . toString ( ) == newDisplayValue ) [ 0 ] . _fields [
84
+ realValueRowIndex
85
+ ] ;
86
+
87
+ newValue . push ( val ?. low ?? val ) ;
88
+ } else if ( ! isMulti ) {
89
+ newValue = extraRecords . filter ( ( r ) => r . _fields [ displayValueRowIndex ] . toString ( ) == newDisplay ) [ 0 ] . _fields [
90
+ realValueRowIndex
91
+ ] ;
92
+
93
+ newValue = newValue ?. low || newValue ;
94
+ } else {
95
+ let ele = props . parameterDisplayValue . filter ( ( x ) => ! newDisplay . includes ( x ) ) [ 0 ] ;
96
+ newValue = [ ...props . parameterValue ] ;
97
+ newValue . splice ( props . parameterDisplayValue . indexOf ( ele ) , 1 ) ;
98
+ }
99
+
100
+ setInputDisplayText ( isMulti ? '' : newDisplay ) ;
101
+ setInputValue ( newDisplay ) ;
102
+
103
+ props . setParameterValue ( newValue ) ;
104
+ props . setParameterDisplayValue ( newDisplay ) ;
105
+ } ;
32
106
return (
33
107
< Autocomplete
34
108
id = 'autocomplete'
35
- options = { extraRecords
36
- . map ( ( r ) =>
37
- ( r . _fields && r . _fields [ displayValueRowIndex ] !== null ? r . _fields [ displayValueRowIndex ] : '(no data)' )
38
- )
39
- . sort ( ) }
40
- getOptionLabel = { ( option ) => ( option ? option . toString ( ) : '' ) }
41
- style = { { maxWidth : 'calc(100% - 30px)' , marginLeft : '15px' , marginTop : '6.5px' } }
42
- inputValue = { inputDisplayText !== null ? `${ inputDisplayText } ` : '' }
109
+ multiple = { multiSelector }
110
+ options = { extraRecords . map ( ( r ) => r ?. _fields ?. [ displayValueRowIndex ] || '(no data)' ) . sort ( ) }
111
+ style = { { maxWidth : 'calc(100% - 30px)' , marginLeft : '15px' , marginTop : '5px' } }
112
+ inputValue = { inputDisplayText }
43
113
onInputChange = { ( event , value ) => {
44
- setInputDisplayText ( value !== null ? ` ${ value } ` : '' ) ;
114
+ setInputDisplayText ( value ) ;
45
115
debouncedQueryCallback ( props . query , { input : `${ value } ` , ...allParameters } , setExtraRecords ) ;
46
116
} }
47
117
isOptionEqualToValue = { ( option , value ) => {
48
118
return ( option && option . toString ( ) ) === ( value && value . toString ( ) ) ;
49
119
} }
50
- value = { props . parameterDisplayValue !== null ? `${ props . parameterDisplayValue } ` : '' }
51
- onChange = { ( event , newDisplayValue ) => {
52
- if ( newDisplayValue == null && clearParameterOnFieldClear ) {
53
- props . setParameterValue ( undefined ) ;
54
- props . setParameterDisplayValue ( undefined ) ;
55
- return ;
56
- }
57
- if ( newDisplayValue == null ) {
58
- props . setParameterValue ( defaultValue ) ;
59
- props . setParameterDisplayValue ( defaultValue ) ;
60
- return ;
61
- }
62
-
63
- let newValue = extraRecords . filter ( ( r ) => r . _fields [ displayValueRowIndex ] . toString ( ) == newDisplayValue ) [ 0 ]
64
- . _fields [ realValueRowIndex ] ;
65
- setInputDisplayText ( newDisplayValue ) ;
66
- if ( newValue && newValue . low ) {
67
- newValue = newValue . low ;
68
- }
69
- if ( newDisplayValue && newDisplayValue . low ) {
70
- newDisplayValue = newDisplayValue . low ;
71
- }
72
-
73
- props . setParameterValue ( newValue ) ;
74
- props . setParameterDisplayValue ( newDisplayValue ) ;
75
- } }
120
+ value = { inputValue }
121
+ onChange = { propagateSelection }
76
122
renderInput = { ( params ) => (
77
123
< TextField
78
124
{ ...params }
@@ -82,6 +128,7 @@ const NodePropertyParameterSelectComponent = (props: ParameterSelectProps) => {
82
128
variant = 'outlined'
83
129
/>
84
130
) }
131
+ getOptionLabel = { ( option ) => RenderSubValue ( option ) }
85
132
/>
86
133
) ;
87
134
} ;
0 commit comments