// @flow import React from "react"; import ListItem from "@material-ui/core/ListItem"; import ListItemIcon from "@material-ui/core/ListItemIcon"; import { renderRawIcon } from "config/icon"; import type { ControlUI } from "config/flowtypes"; import { Toggle, DropDown, Link, Section, Text, Progress, Slider } from "./UiItem"; import MqttContext from "mqtt/context"; import type { MqttContextValue } from "mqtt/context"; export type UiItemListProps = { controls: Array }; export default class UiItemList extends React.PureComponent { constructor(props: UiItemListProps) { super(props); } render() { return this.props.controls.map((control, key) => { if (control.type == null) { throw new Error( "A control is missing the \"type\" parameter" ); } if (control.type === "section") { return ( {this.renderListItem(control, key)} ); } return ( {this.renderListItem(control, key)} ); }); } renderListItem(control: ControlUI, key: number) { return (mqtt: MqttContextValue) => { const node = this.renderControl(control, key.toString(), mqtt); if (control.icon == null || control.type === "link") { return node; } else { const listIconNode = ( {renderRawIcon(control.icon(mqtt.state), "mdi-24px")} ); return [listIconNode, node]; } }; } renderControl(control: ControlUI, key: string, mqtt: MqttContextValue) { const props = { state: Object.assign({}, mqtt.state), onChangeState: mqtt.changeState, key: `${key}-licontrol` }; switch (control.type) { case "toggle": { return ; } case "dropDown": { return ; } case "section": { return
; } case "link": { return ; } case "slider": { return ; } case "text": { return ; } case "progress": { return ; } default: { throw new Error( `Unknown UI type "${control.type}" for "${control.text}" component` ); } } } }