fix: faulty StringField.suggestions
Massimo Melina committed
Mar 1, 2024 at 15:13 UTC
7f1af16d250f5981890cf47f25cee2d6b3b30a80
1 file changed
+15
-6
mui-grid-form/StringField.ts
+15
-6
@@ -37,6 +37,7 @@ export function StringField({ value, onChange, min, max, required, setApi, typin
37
fullWidth: true,
38
InputLabelProps: state || props.placeholder ? { shrink: true } : undefined,
39
...props,
40
+ ...params,
41
sx: props.label ? props.sx : Object.assign({ '& .MuiInputBase-input': { pt: 1.5 } }, props.sx),
42
value: state,
43
onChange(ev) {
@@ -53,7 +54,7 @@ export function StringField({ value, onChange, min, max, required, setApi, typin
54
onKeyDown(ev) {
55
props.onKeyDown?.(ev)
56
autoFillDetected.current = ev.code === undefined
56
- if (ev.key === 'Enter')
57
+ if (ev.key === 'Enter' && (ev.target as HTMLElement).ariaExpanded !== 'true') // don't act if suggestion list is expanded
58
go(ev)
59
},
60
onFocus(ev) {
@@ -65,17 +66,25 @@ export function StringField({ value, onChange, min, max, required, setApi, typin
66
go(ev)
67
},
68
InputProps: {
68
- startAdornment: start && h(InputAdornment, { position: 'start' }, start),
69
- endAdornment: end && h(InputAdornment, { position: 'end' }, end),
69
...props.InputProps,
70
+ ...params?.InputProps,
71
+ startAdornment: start && h(InputAdornment, { position: 'start' }, start, props?.InputProps?.startAdornment, params?.InputProps?.startAdornment),
72
+ endAdornment: end && h(InputAdornment, { position: 'end' }, end, props?.InputProps?.endAdornment, params?.InputProps?.endAdornment),
73
},
72
- ...params,
74
})
75
return !suggestions ? render(null)
75
- : h(Autocomplete, { freeSolo: true, options: suggestions, renderInput: render })
76
+ : h(Autocomplete, {
77
+ value,
78
+ freeSolo: true,
79
+ options: suggestions,
80
+ renderInput: render,
81
+ onChange(ev, v) {
82
+ go(ev, v as string)
83
+ }
84
+ })
85
86
function go(event: any, newVal: string=state) {
78
- newVal = newVal.trim()
87
+ newVal = newVal?.trim()
88
if (newVal === lastChange.current) return // don't compare to 'value' as that represents only accepted changes, while we are interested also in changes through discarded values
89
lastChange.current = newVal
90
onChange(newVal, {