|
1 |
| -import React, { FunctionComponent } from 'react'; |
| 1 | +import React, { Component } from 'react'; |
2 | 2 | import { CompositeFeature, Endpoint, FeatureAccessMode, GenericExposedFeature, ListFeature } from '../../../types';
|
3 | 3 | import RangeListEditor from '../../range-list-editor/range-list-editor';
|
4 | 4 | import { BaseFeatureProps, BaseViewer, NoAccessError } from '../base';
|
5 | 5 | import ListEditor from '../list-editor';
|
| 6 | +import Button from '../../button'; |
| 7 | +import cx from 'classnames'; |
| 8 | +import { WithTranslation, withTranslation } from 'react-i18next'; |
6 | 9 |
|
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} />; |
21 | 83 | } 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} />; |
30 | 85 | }
|
31 |
| - } else if (access & FeatureAccessMode.ACCESS_STATE) { |
32 |
| - return <BaseViewer {...props} />; |
33 |
| - } else { |
34 |
| - return <NoAccessError {...props} />; |
35 | 86 | }
|
36 | 87 | };
|
37 | 88 |
|
38 |
| -export default List; |
| 89 | +export default withTranslation(['list', 'common'])(React.memo(List)); |
0 commit comments