Skip to content

Commit 3df395b

Browse files
committed
fix(list): fix list editor
1 parent f8adba0 commit 3df395b

File tree

2 files changed

+82
-28
lines changed

2 files changed

+82
-28
lines changed

src/components/features/list-editor.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const ListEditor: FunctionComponent<ListEditorProps> = (props) => {
2626

2727
const onItemChange = (itemValue: any, itemIndex: number) => {
2828
const newListValue = [...currentValue];
29+
if (typeof itemValue === 'object') {
30+
itemValue = {...currentValue[itemIndex], ...itemValue}
31+
}
2932
newListValue[itemIndex] = itemValue;
3033
replaceList(newListValue);
3134
};

src/components/features/list/list.tsx

+79-28
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,89 @@
1-
import React, { FunctionComponent } from 'react';
1+
import React, { Component } from 'react';
22
import { CompositeFeature, Endpoint, FeatureAccessMode, GenericExposedFeature, ListFeature } from '../../../types';
33
import RangeListEditor from '../../range-list-editor/range-list-editor';
44
import { BaseFeatureProps, BaseViewer, NoAccessError } from '../base';
55
import ListEditor from '../list-editor';
6+
import Button from '../../button';
7+
import cx from 'classnames';
8+
import { WithTranslation, withTranslation } from 'react-i18next';
69

7-
const List: FunctionComponent<
8-
BaseFeatureProps<ListFeature> & { parentFeatures: (CompositeFeature | GenericExposedFeature)[] }
9-
> = (props) => {
10-
const { feature, deviceState, onChange, minimal, parentFeatures } = props;
11-
const { access = FeatureAccessMode.ACCESS_WRITE, endpoint, property, item_type: itemType } = feature;
12-
if (access & FeatureAccessMode.ACCESS_WRITE) {
13-
if (itemType == 'number') {
14-
return (
15-
<RangeListEditor
16-
onChange={(value) => onChange(endpoint as Endpoint, property ? { [property]: value } : value)}
17-
value={property ? (deviceState[property] as number[]) : []}
18-
minimal={minimal}
19-
/>
20-
);
10+
interface State {
11+
value: any[];
12+
}
13+
14+
type Props = BaseFeatureProps<ListFeature> & { parentFeatures: (CompositeFeature | GenericExposedFeature)[] } & WithTranslation<'list'>;
15+
16+
class List extends Component<Props, State> {
17+
constructor(props: Props) {
18+
super(props);
19+
const {deviceState, feature} = this.props;
20+
const {property} = feature;
21+
const value = (property ? ((deviceState[property] ?? []) as any[]) : []);;
22+
this.state = {value}
23+
}
24+
25+
onChange = (value: any[]): void => {
26+
const {endpoint, property} = this.props.feature;
27+
this.setState({value});
28+
if (!this.isListRoot()) {
29+
this.props.onChange(endpoint as Endpoint, property ? { [property]: value } : value);
30+
}
31+
}
32+
33+
onApply = () => {
34+
const {value} = this.state;
35+
const {endpoint, property} = this.props.feature;
36+
this.props.onChange(endpoint as Endpoint, property ? { [property]: value } : value);
37+
}
38+
39+
isListRoot = (): boolean => {
40+
return this.props.parentFeatures?.length === 1;
41+
};
42+
43+
render(): JSX.Element | JSX.Element[] {
44+
const { feature, minimal, parentFeatures, t } = this.props;
45+
const { access = FeatureAccessMode.ACCESS_WRITE, item_type: itemType } = feature;
46+
if (access & FeatureAccessMode.ACCESS_WRITE) {
47+
if (itemType == 'number') {
48+
return (
49+
<RangeListEditor
50+
onChange={this.onChange}
51+
value={this.state.value}
52+
minimal={minimal}
53+
/>
54+
);
55+
} else {
56+
const result = [
57+
<ListEditor
58+
key='1'
59+
feature={itemType}
60+
parentFeatures={[...parentFeatures, feature]}
61+
onChange={this.onChange}
62+
value={this.state.value}
63+
/>
64+
];
65+
66+
if (this.isListRoot()) {
67+
result.push(
68+
<div key='2'>
69+
<Button
70+
className={cx('btn btn-primary float-end', { 'btn-sm': minimal })}
71+
onClick={this.onApply}
72+
>
73+
{t('common:apply')}
74+
</Button>
75+
</div>
76+
);
77+
}
78+
79+
return result;
80+
}
81+
} else if (access & FeatureAccessMode.ACCESS_STATE) {
82+
return <BaseViewer {...this.props} />;
2183
} else {
22-
return (
23-
<ListEditor
24-
feature={itemType}
25-
parentFeatures={[...parentFeatures, feature]}
26-
onChange={(value) => onChange(endpoint as Endpoint, property ? { [property]: value } : value)}
27-
value={property ? ((deviceState[property] ?? []) as any[]) : []}
28-
/>
29-
);
84+
return <NoAccessError {...this.props} />;
3085
}
31-
} else if (access & FeatureAccessMode.ACCESS_STATE) {
32-
return <BaseViewer {...props} />;
33-
} else {
34-
return <NoAccessError {...props} />;
3586
}
3687
};
3788

38-
export default List;
89+
export default withTranslation(['list', 'common'])(React.memo(List));

0 commit comments

Comments
 (0)