页面状态与交互
处理设备操作时,界面可以先给出反馈,等设备确认后再更新已确认的状态。例如,拖动时显示当前亮度,松手后再向设备提交目标值。
根据操作结果更新界面
以设备开关为例,点击后先把开关标记为正在提交,避免同一操作被重复触发。设备确认后再更新开关的已确认状态。失败时保留上次确认的状态,并提示用户重试。
Ray 页面用 React 状态记录已确认的值、提交状态和错误提示。原生小程序在事件处理函数中调用 setData 更新模板绑定的字段。页面如果启用了首屏即时渲染,依赖设备或权限的操作应等本次数据就绪后再开放。见首屏即时渲染。
展开内容后再读取位置
例如,用户点击设备列表中的「更多」后,页面才渲染详情节点。如果只是把详情滚入视野,可以使用 scroll-view 的 scroll-into-view 指向容器内的详情节点。只有需要计算额外偏移、浮层位置或实际尺寸时,才需要测量节点。
- Ray 可以在控制展开的状态提交后,通过相应的
useEffect 发起查询。
- 原生小程序 可以在更新展开状态的
setData 回调中查询。首次渲染的节点可以在 onReady 中查询。
使用 SelectorQuery 异步取得尺寸,并在结果返回后执行后续操作。如果返回空值,或用户在等待期间已经收起详情、离开页面,就跳过这次操作。需要测量展开动画的最终尺寸时,等动画结束后再查询。见事件与节点查询。
拖动时预览,松手后提交
亮度控件通过 changing 在拖动过程中更新界面,通过 change 在拖动结束后提交一次设备指令。Ray 对应的事件是 onChanging 和 onChange,两者都可以从 event.detail.value 读取当前值。
下面以亮度控件为例。设备状态就绪后传入初始亮度,saveBrightness 由业务实现,在设备确认后返回实际值,失败时抛出错误。
Ray
import { useRef, useState } from 'react';
import { Slider, Text, View } from '@ray-js/ray';
type BrightnessControlProps = {
initialBrightness: number;
saveBrightness: (value: number) => Promise<number>;
};
export function BrightnessControl({
initialBrightness,
saveBrightness,
}: BrightnessControlProps) {
const confirmed = useRef(initialBrightness);
const pending = useRef(false);
const [preview, setPreview] = useState(initialBrightness);
const [saving, setSaving] = useState(false);
const [error, setError] = useState('');
async function commit(value: number) {
if (pending.current) return;
pending.current = true;
setPreview(value);
setSaving(true);
setError('');
try {
confirmed.current = await saveBrightness(value);
} catch {
setError('亮度设置失败,请重试');
} finally {
setPreview(confirmed.current);
setSaving(false);
pending.current = false;
}
}
return (
<View>
<Text>亮度 {preview}%{saving ? '(设置中)' : ''}</Text>
<Slider
min={0}
max={100}
value={preview}
disabled={saving}
onChanging={event => {
if (!pending.current) setPreview(event.detail.value);
}}
onChange={event => void commit(event.detail.value)}
/>
{error && <Text>{error}</Text>}
</View>
);
}
原生小程序
滑块在 index.tyml 中绑定拖动和松手事件。
<text>亮度 {{preview}}%</text>
<slider value="{{preview}}" disabled="{{saving}}" bindchanging="previewBrightness" bindchange="commitBrightness" />
<text ty:if="{{error}}">{{error}}</text>
在 index.js 的页面方法中更新预览和确认值。取得设备状态后初始化 confirmed 和 preview,并提供与 Ray 示例相同的 saveBrightness。
Page({
data: { confirmed: 0, preview: 0, saving: false, error: '' },
previewBrightness(event) {
if (!this.data.saving) this.setData({ preview: event.detail.value });
},
async commitBrightness(event) {
if (this.data.saving) return;
const value = event.detail.value;
this.setData({ saving: true, preview: value, error: '' });
try {
const confirmed = await saveBrightness(value);
this.setData({ confirmed, preview: confirmed });
} catch {
this.setData({ preview: this.data.confirmed, error: '亮度设置失败,请重试' });
} finally {
this.setData({ saving: false });
}
},
});
设备指令失败时,两种写法都会把预览值恢复到上次确认的亮度。
添加按压反馈
列表项或卡片只需要按下变色时,可以用 view 的 hover-class。更复杂的反馈可以用 SJS 处理组件支持的触摸事件,再通过 setStyle 更新样式。例如按下时降低透明度,松手或取消触摸时恢复。设备指令和请求仍由页面的业务处理函数执行。见选择器与样式作用域和RJS 与视图交互。