Skip to content

Commit

Permalink
fix(list): fix list editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Oct 27, 2023
1 parent f8adba0 commit 3df395b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/components/features/list-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const ListEditor: FunctionComponent<ListEditorProps> = (props) => {

const onItemChange = (itemValue: any, itemIndex: number) => {
const newListValue = [...currentValue];
if (typeof itemValue === 'object') {
itemValue = {...currentValue[itemIndex], ...itemValue}
}
newListValue[itemIndex] = itemValue;
replaceList(newListValue);
};
Expand Down
107 changes: 79 additions & 28 deletions src/components/features/list/list.tsx
Original file line number Diff line number Diff line change
@@ -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<ListFeature> & { 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 (
<RangeListEditor
onChange={(value) => onChange(endpoint as Endpoint, property ? { [property]: value } : value)}
value={property ? (deviceState[property] as number[]) : []}
minimal={minimal}
/>
);
interface State {
value: any[];
}

type Props = BaseFeatureProps<ListFeature> & { parentFeatures: (CompositeFeature | GenericExposedFeature)[] } & WithTranslation<'list'>;

class List extends Component<Props, State> {
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 (
<RangeListEditor
onChange={this.onChange}
value={this.state.value}
minimal={minimal}
/>
);
} else {
const result = [
<ListEditor
key='1'
feature={itemType}
parentFeatures={[...parentFeatures, feature]}
onChange={this.onChange}
value={this.state.value}
/>
];

if (this.isListRoot()) {
result.push(
<div key='2'>
<Button
className={cx('btn btn-primary float-end', { 'btn-sm': minimal })}
onClick={this.onApply}
>
{t('common:apply')}
</Button>
</div>
);
}

return result;
}
} else if (access & FeatureAccessMode.ACCESS_STATE) {
return <BaseViewer {...this.props} />;
} else {
return (
<ListEditor
feature={itemType}
parentFeatures={[...parentFeatures, feature]}
onChange={(value) => onChange(endpoint as Endpoint, property ? { [property]: value } : value)}
value={property ? ((deviceState[property] ?? []) as any[]) : []}
/>
);
return <NoAccessError {...this.props} />;
}
} else if (access & FeatureAccessMode.ACCESS_STATE) {
return <BaseViewer {...props} />;
} else {
return <NoAccessError {...props} />;
}
};

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

0 comments on commit 3df395b

Please sign in to comment.