StringField.suggestions
Massimo Melina committed
Feb 23, 2023 at 09:25 UTC
d660623f4f99b8377c90c41578d26db7c999a482
1 file changed
+6
-3
mui-grid-form/StringField.ts
+6
-3
@@ -2,7 +2,7 @@
2
3
import { createElement as h, ReactNode, useEffect, useRef, useState } from 'react'
4
import { FieldProps } from '.'
5
-import { InputAdornment, TextField } from '@mui/material'
5
+import { Autocomplete, InputAdornment, TextField } from '@mui/material'
6
7
interface StringProps extends FieldProps<string> {
8
typing?: boolean
@@ -13,7 +13,7 @@ interface StringProps extends FieldProps<string> {
13
start?: ReactNode
14
end?: ReactNode
15
}
16
-export function StringField({ value, onChange, min, max, required, getApi, typing, start, end, onTyping, ...props }: StringProps) {
16
+export function StringField({ value, onChange, min, max, required, getApi, typing, start, end, onTyping, suggestions, ...props }: StringProps) {
17
const normalized = value ?? ''
18
getApi?.({
19
getError() {
@@ -30,7 +30,7 @@ export function StringField({ value, onChange, min, max, required, getApi, typin
30
setState(normalized)
31
lastChange.current = normalized
32
}, [normalized])
33
- return h(TextField, {
33
+ const render = (params: any) => h(TextField, {
34
fullWidth: true,
35
InputLabelProps: state || props.placeholder ? { shrink: true } : undefined,
36
...props,
@@ -58,7 +58,10 @@ export function StringField({ value, onChange, min, max, required, getApi, typin
58
endAdornment: end && h(InputAdornment, { position: 'end' }, end),
59
...props.InputProps,
60
},
61
+ ...params,
62
})
63
+ return !suggestions ? render(null)
64
+ : h(Autocomplete, { freeSolo: true, options: suggestions, renderInput: render })
65
66
function go(event: any, val: string=state) {
67
const newV = val.trim()