diff options
author | Matjaž Črnko <m.crnko@txt.si> | 2024-01-22 15:22:33 +0100 |
---|---|---|
committer | Matjaž Črnko <m.crnko@txt.si> | 2024-01-22 15:22:33 +0100 |
commit | 2bd044ecf9fa456e168057acc513bd3b4582d0c6 (patch) | |
tree | 870d36062ae8c2d3898294a36c9e0e1dcb68d9f2 /modern/src/common | |
parent | fd58e2bb16e41a6097135b77565a4f6e39f61d85 (diff) | |
download | trackermap-web-2bd044ecf9fa456e168057acc513bd3b4582d0c6.tar.gz trackermap-web-2bd044ecf9fa456e168057acc513bd3b4582d0c6.tar.bz2 trackermap-web-2bd044ecf9fa456e168057acc513bd3b4582d0c6.zip |
fix: SelectField better label code
Diffstat (limited to 'modern/src/common')
-rw-r--r-- | modern/src/common/components/SelectField.jsx | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/modern/src/common/components/SelectField.jsx b/modern/src/common/components/SelectField.jsx index dc31a156..bc0571d5 100644 --- a/modern/src/common/components/SelectField.jsx +++ b/modern/src/common/components/SelectField.jsx @@ -20,6 +20,13 @@ const SelectField = ({ }) => { const [items, setItems] = useState(data); + const getOptionLabel = (option) => { + if (typeof option !== 'object') { + option = items.find(obj => keyField ? obj[keyField] === option : obj === option); + } + return option ? titleGetter(option) : ''; + } + useEffectAsync(async () => { if (endpoint) { const response = await fetch(endpoint); @@ -53,18 +60,7 @@ const SelectField = ({ <Autocomplete size="small" options={items} - getOptionLabel={(option) => { - if (typeof option != 'object') { - if (keyField) { - option = items.find(obj => obj[keyField] === option) - } else { - option = items.find(obj => obj === option) - } - } - const title = option ? titleGetter(option) : '' - return title ? title : '' - } - } + getOptionLabel={getOptionLabel} renderOption={(props, option) => ( <MenuItem {...props} key={keyGetter(option)} value={keyGetter(option)}>{titleGetter(option)}</MenuItem> )} |