diff --git a/src/panels/ControlFourV3/Common/Switch/Switch.less b/src/panels/ControlFourV3/Common/Switch/Switch.less index 385c1101..6a554106 100644 --- a/src/panels/ControlFourV3/Common/Switch/Switch.less +++ b/src/panels/ControlFourV3/Common/Switch/Switch.less @@ -61,4 +61,16 @@ } } -} + .save{ + border: 1px solid black; + width: 90%; + height: 30px; + display: flex; + align-items: center; + border-radius: 15px; + justify-content: center; + background-color: #fff; + margin-left: 17px; + font-size: 16px; + } +} \ No newline at end of file diff --git a/src/panels/ControlFourV3/Common/Switch/index.tsx b/src/panels/ControlFourV3/Common/Switch/index.tsx index f02db054..7d24c76d 100644 --- a/src/panels/ControlFourV3/Common/Switch/index.tsx +++ b/src/panels/ControlFourV3/Common/Switch/index.tsx @@ -1,42 +1,49 @@ -import React, { useRef } from 'react'; +import React, { useRef, useState } from 'react'; import { Form } from 'antd-mobile'; import { Input } from '@custom/Input'; import { Icon } from '@custom/Icon'; import { useTitle } from '@hooks/useTitle'; -import { t } from '@locales'; + const defaultValue = { - local: [ - [`${t('开关')}1`, 'switch_1_name'], - [`${t('开关')}2`, 'switch_2_name'], - [`${t('开关')}3`, 'switch_3_name'], - ], - infinite: [ - // ['无限开关1', 'infinite_switch_1'], - // ['无限开关2', 'infinite_switch_2'], - // ['无限开关3', 'infinite_switch_3'], - // ['无限开关4', 'infinite_switch_4'], - // ['无限开关5', 'infinite_switch_5'], - // ['无限开关6', 'infinite_switch_6'] - ] + local: [['开关1', 'switch_1_name'], ['开关2', 'switch_2_name'], ['开关3', 'switch_3_name']], + infinite: [], }; function Switch(props) { - useTitle(t('开关名称')); + useTitle('开关名称'); const { doControlDeviceData, deviceData } = { ...props }; + const [values, setValues] = useState(defaultValue.local.map(([v, k]) => deviceData[k] || v)); // 初始化为 deviceData 或默认值 + const items = ['item0', 'item1', 'item2']; const itemRefs = items.reduce((acc, value) => { acc[value] = React.createRef(); return acc; }, {}); + // 保存所有输入框的当前值 + const handleSave = () => { + defaultValue.local.forEach(([, key], index) => { + const valueToSave = values[index]; // 获取对应的值 + console.log('Saving value:', valueToSave); + doControlDeviceData(key, valueToSave); // 调用 doControlDeviceData + }); + window.history.back(); + }; + + const handleBlur = index => (e) => { + const currentValue = e.currentTarget.value; + const newValues = [...values]; // 创建值的副本 + newValues[index] = currentValue; // 更新该索引的值 + setValues(newValues); // 更新状态 + }; + return (
- {t('本地开关')} + 本地开关
- {defaultValue.local.map(([value, key], index) => - itemRefs[`item${index}`].current.focus()} /> @@ -44,32 +51,17 @@ function Switch(props) { > { - const v = e.currentTarget.value; - doControlDeviceData(key, v); - }} + onBlur={handleBlur(index)} /> - - )} + )}
- {/*
- 无限开关 - {defaultValue.infinite.map(([value, key], index) =>
-
{value}
- { - const v = e.currentTarget.value; - doControlDeviceData(key, v); - }} />
)} -
*/} -
+
+ 保存 +
+ ); }