Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: resolve conflict #157

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/panels/ControlFourV3/Common/Switch/Switch.less
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
76 changes: 34 additions & 42 deletions src/panels/ControlFourV3/Common/Switch/index.tsx
Original file line number Diff line number Diff line change
@@ -1,75 +1,67 @@
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 (
<div className="switch-list-panel">
<div className="switch-group">
<span className="title">{t('本地开关')}</span>
<span className="title">本地开关</span>
<Form layout='horizontal' className="switch-form">
{defaultValue.local.map(([value, key], index) =>
<Form.Item
label={`${t('开关')}${index + 1}`}
{defaultValue.local.map(([value, key], index) => <Form.Item
label={`开关${index + 1}`}
key={index}
extra={
<Icon name="editor-other" onClick={e => itemRefs[`item${index}`].current.focus()} />
}
>
<Input
ref={itemRefs[`item${index}`]}
placeholder={t('请输入开关名称')}
placeholder='请输入开关名称'
defaultValue={deviceData[key] || value}
onBlur={(e) => {
const v = e.currentTarget.value;
doControlDeviceData(key, v);
}}
onBlur={handleBlur(index)}
/>
</Form.Item>
)}
</Form.Item>)}
</Form>
</div>
{/* <div className="switch-group">
<span className="title" style={{ marginBottom: 18, display: 'block' }}>无限开关</span>
{defaultValue.infinite.map(([value, key], index) => <div className="custom-group" key={`infinite_'${index}`}>
<div className="custom-label" >{value}</div>
<Input
className="custom-input wireless"
placeholder='请输入'
defaultValue={value}
clearable
onBlur={(e) => {
const v = e.currentTarget.value;
doControlDeviceData(key, v);
}} /></div>)}
</div> */}
</div >
<div onClick={handleSave} className='save'>
保存
</div>
</div>
);
}

Expand Down
Loading