From 3df395b774816b7605cf34699c4e9cdf288f1b56 Mon Sep 17 00:00:00 2001 From: koenkk Date: Fri, 27 Oct 2023 21:37:17 +0200 Subject: [PATCH] fix(list): fix list editor --- src/components/features/list-editor.tsx | 3 + src/components/features/list/list.tsx | 107 +++++++++++++++++------- 2 files changed, 82 insertions(+), 28 deletions(-) diff --git a/src/components/features/list-editor.tsx b/src/components/features/list-editor.tsx index 42c78fdae..88d895bca 100644 --- a/src/components/features/list-editor.tsx +++ b/src/components/features/list-editor.tsx @@ -26,6 +26,9 @@ const ListEditor: FunctionComponent = (props) => { const onItemChange = (itemValue: any, itemIndex: number) => { const newListValue = [...currentValue]; + if (typeof itemValue === 'object') { + itemValue = {...currentValue[itemIndex], ...itemValue} + } newListValue[itemIndex] = itemValue; replaceList(newListValue); }; diff --git a/src/components/features/list/list.tsx b/src/components/features/list/list.tsx index f404a723f..6f52474ac 100644 --- a/src/components/features/list/list.tsx +++ b/src/components/features/list/list.tsx @@ -1,38 +1,89 @@ -import React, { FunctionComponent } from 'react'; +import React, { Component } from 'react'; import { CompositeFeature, Endpoint, FeatureAccessMode, GenericExposedFeature, ListFeature } from '../../../types'; import RangeListEditor from '../../range-list-editor/range-list-editor'; import { BaseFeatureProps, BaseViewer, NoAccessError } from '../base'; import ListEditor from '../list-editor'; +import Button from '../../button'; +import cx from 'classnames'; +import { WithTranslation, withTranslation } from 'react-i18next'; -const List: FunctionComponent< - BaseFeatureProps & { parentFeatures: (CompositeFeature | GenericExposedFeature)[] } -> = (props) => { - const { feature, deviceState, onChange, minimal, parentFeatures } = props; - const { access = FeatureAccessMode.ACCESS_WRITE, endpoint, property, item_type: itemType } = feature; - if (access & FeatureAccessMode.ACCESS_WRITE) { - if (itemType == 'number') { - return ( - onChange(endpoint as Endpoint, property ? { [property]: value } : value)} - value={property ? (deviceState[property] as number[]) : []} - minimal={minimal} - /> - ); +interface State { + value: any[]; +} + +type Props = BaseFeatureProps & { parentFeatures: (CompositeFeature | GenericExposedFeature)[] } & WithTranslation<'list'>; + +class List extends Component { + constructor(props: Props) { + super(props); + const {deviceState, feature} = this.props; + const {property} = feature; + const value = (property ? ((deviceState[property] ?? []) as any[]) : []);; + this.state = {value} + } + + onChange = (value: any[]): void => { + const {endpoint, property} = this.props.feature; + this.setState({value}); + if (!this.isListRoot()) { + this.props.onChange(endpoint as Endpoint, property ? { [property]: value } : value); + } + } + + onApply = () => { + const {value} = this.state; + const {endpoint, property} = this.props.feature; + this.props.onChange(endpoint as Endpoint, property ? { [property]: value } : value); + } + + isListRoot = (): boolean => { + return this.props.parentFeatures?.length === 1; + }; + + render(): JSX.Element | JSX.Element[] { + const { feature, minimal, parentFeatures, t } = this.props; + const { access = FeatureAccessMode.ACCESS_WRITE, item_type: itemType } = feature; + if (access & FeatureAccessMode.ACCESS_WRITE) { + if (itemType == 'number') { + return ( + + ); + } else { + const result = [ + + ]; + + if (this.isListRoot()) { + result.push( +
+ +
+ ); + } + + return result; + } + } else if (access & FeatureAccessMode.ACCESS_STATE) { + return ; } else { - return ( - onChange(endpoint as Endpoint, property ? { [property]: value } : value)} - value={property ? ((deviceState[property] ?? []) as any[]) : []} - /> - ); + return ; } - } else if (access & FeatureAccessMode.ACCESS_STATE) { - return ; - } else { - return ; } }; -export default List; +export default withTranslation(['list', 'common'])(React.memo(List));