From 783eccb019824946d758c31f2f945ce2fc80fc2a Mon Sep 17 00:00:00 2001 From: Matjaž Črnko Date: Fri, 19 Jan 2024 19:21:02 +0100 Subject: wip: Modify SelectField so that it uses Autocomplete component instead of Select with single select --- modern/src/common/components/SelectField.jsx | 58 ++++++++++++++++------ modern/src/settings/ComputedAttributePage.jsx | 2 +- modern/src/settings/DevicePage.jsx | 4 +- modern/src/settings/GeofencePage.jsx | 2 +- modern/src/settings/GroupPage.jsx | 2 +- modern/src/settings/PreferencesPage.jsx | 4 +- modern/src/settings/ServerPage.jsx | 5 +- modern/src/settings/UserPage.jsx | 5 +- modern/src/settings/components/BaseCommandView.jsx | 3 +- 9 files changed, 58 insertions(+), 27 deletions(-) (limited to 'modern/src') diff --git a/modern/src/common/components/SelectField.jsx b/modern/src/common/components/SelectField.jsx index 35f817a0..dc31a156 100644 --- a/modern/src/common/components/SelectField.jsx +++ b/modern/src/common/components/SelectField.jsx @@ -1,5 +1,5 @@ import { - FormControl, InputLabel, MenuItem, Select, + FormControl, InputLabel, MenuItem, Select, Autocomplete, TextField } from '@mui/material'; import React, { useState } from 'react'; import { useEffectAsync } from '../../reactHelper'; @@ -14,6 +14,7 @@ const SelectField = ({ onChange, endpoint, data, + keyField = 'id', keyGetter = (item) => item.id, titleGetter = (item) => item.name, }) => { @@ -33,20 +34,47 @@ const SelectField = ({ if (items) { return ( - {label} - + {multiple ? ( + <> + {label} + + + ) : ( + <> + { + 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 : '' + } + } + renderOption={(props, option) => ( + {titleGetter(option)} + )} + isOptionEqualToValue={(option, value) => keyGetter(option) == value} + value={value} + onChange={(e, nV) => {onChange({target:{value:nV ? keyGetter(nV) : emptyValue}})}} + renderInput={(params) => } + /> + + )} ); } diff --git a/modern/src/settings/ComputedAttributePage.jsx b/modern/src/settings/ComputedAttributePage.jsx index 3bae8231..f7ffe36e 100644 --- a/modern/src/settings/ComputedAttributePage.jsx +++ b/modern/src/settings/ComputedAttributePage.jsx @@ -156,7 +156,7 @@ const ComputedAttributePage = () => { setDeviceId(Number(e.target.value))} endpoint="/api/devices" label={t('sharedDevice')} diff --git a/modern/src/settings/DevicePage.jsx b/modern/src/settings/DevicePage.jsx index d8a30cbf..66a71cdb 100644 --- a/modern/src/settings/DevicePage.jsx +++ b/modern/src/settings/DevicePage.jsx @@ -103,7 +103,7 @@ const DevicePage = () => { setItem({ ...item, groupId: Number(event.target.value) })} endpoint="/api/groups" label={t('groupParent')} @@ -134,7 +134,7 @@ const DevicePage = () => { label={t('deviceCategory')} /> setItem({ ...item, calendarId: Number(event.target.value) })} endpoint="/api/calendars" label={t('sharedCalendar')} diff --git a/modern/src/settings/GeofencePage.jsx b/modern/src/settings/GeofencePage.jsx index b6c516a1..174bdfb1 100644 --- a/modern/src/settings/GeofencePage.jsx +++ b/modern/src/settings/GeofencePage.jsx @@ -76,7 +76,7 @@ const GeofencePage = () => { label={t('sharedDescription')} /> setItem({ ...item, calendarId: Number(event.target.value) })} endpoint="/api/calendars" label={t('sharedCalendar')} diff --git a/modern/src/settings/GroupPage.jsx b/modern/src/settings/GroupPage.jsx index c4ca867d..b54e3b7f 100644 --- a/modern/src/settings/GroupPage.jsx +++ b/modern/src/settings/GroupPage.jsx @@ -81,7 +81,7 @@ const GroupPage = () => { setItem({ ...item, groupId: Number(event.target.value) })} endpoint="/api/groups" label={t('groupParent')} diff --git a/modern/src/settings/PreferencesPage.jsx b/modern/src/settings/PreferencesPage.jsx index bd96b140..53d205f5 100644 --- a/modern/src/settings/PreferencesPage.jsx +++ b/modern/src/settings/PreferencesPage.jsx @@ -272,8 +272,8 @@ const PreferencesPage = () => { label={t('devicePrimaryInfo')} /> setAttributes({ ...attributes, deviceSecondary: e.target.value })} data={deviceFields} titleGetter={(it) => t(it.name)} diff --git a/modern/src/settings/ServerPage.jsx b/modern/src/settings/ServerPage.jsx index 07ce2dad..4dc87887 100644 --- a/modern/src/settings/ServerPage.jsx +++ b/modern/src/settings/ServerPage.jsx @@ -193,10 +193,11 @@ const ServerPage = () => { setItem({ ...item, attributes: { ...item.attributes, timezone: e.target.value } })} endpoint="/api/server/timezones" + keyField={false} keyGetter={(it) => it} titleGetter={(it) => it} label={t('sharedTimezone')} diff --git a/modern/src/settings/UserPage.jsx b/modern/src/settings/UserPage.jsx index d9a608ef..7411ee64 100644 --- a/modern/src/settings/UserPage.jsx +++ b/modern/src/settings/UserPage.jsx @@ -267,10 +267,11 @@ const UserPage = () => { setItem({ ...item, attributes: { ...item.attributes, timezone: e.target.value } })} endpoint="/api/server/timezones" + keyField={false} keyGetter={(it) => it} titleGetter={(it) => it} label={t('sharedTimezone')} diff --git a/modern/src/settings/components/BaseCommandView.jsx b/modern/src/settings/components/BaseCommandView.jsx index acf39090..070ecfed 100644 --- a/modern/src/settings/components/BaseCommandView.jsx +++ b/modern/src/settings/components/BaseCommandView.jsx @@ -28,9 +28,10 @@ const BaseCommandView = ({ deviceId, item, setItem }) => { return ( <> setItem({ ...item, type: e.target.value, attributes: {} })} endpoint={deviceId ? `/api/commands/types?${new URLSearchParams({ deviceId }).toString()}` : '/api/commands/types'} + keyField='type' keyGetter={(it) => it.type} titleGetter={(it) => t(prefixString('command', it.type))} label={t('sharedType')} -- cgit v1.2.3