修改闪烁问题

This commit is contained in:
zhx 2026-05-14 11:19:28 +08:00
parent 995bb6903b
commit f5a6c6685c
2 changed files with 56 additions and 10 deletions

View File

@ -24,8 +24,10 @@ export function useRoomPinsPage() {
const [activeAction, setActiveAction] = useState(""); const [activeAction, setActiveAction] = useState("");
const [form, setForm] = useState(emptyForm); const [form, setForm] = useState(emptyForm);
const [loadingAction, setLoadingAction] = useState(""); const [loadingAction, setLoadingAction] = useState("");
const [roomSearch, setRoomSearch] = useState(""); const [roomInput, setRoomInput] = useState("");
const [roomKeyword, setRoomKeyword] = useState("");
const [roomOptionsState, setRoomOptionsState] = useState({ error: "", loading: false, options: [] }); const [roomOptionsState, setRoomOptionsState] = useState({ error: "", loading: false, options: [] });
const [selectedRoom, setSelectedRoom] = useState(null);
const filters = useMemo( const filters = useMemo(
() => ({ () => ({
keyword: query, keyword: query,
@ -58,7 +60,7 @@ export function useRoomPinsPage() {
setRoomOptionsState((current) => ({ ...current, error: "", loading: true })); setRoomOptionsState((current) => ({ ...current, error: "", loading: true }));
try { try {
const data = await listRooms({ const data = await listRooms({
keyword: roomSearch.trim(), keyword: roomKeyword.trim(),
page: 1, page: 1,
page_size: 20, page_size: 20,
status: "active", status: "active",
@ -77,7 +79,17 @@ export function useRoomPinsPage() {
cancelled = true; cancelled = true;
window.clearTimeout(timer); window.clearTimeout(timer);
}; };
}, [activeAction, roomSearch]); }, [activeAction, roomKeyword]);
const roomOptions = useMemo(() => {
if (!selectedRoom) {
return roomOptionsState.options;
}
if (roomOptionsState.options.some((room) => room.roomId === selectedRoom.roomId)) {
return roomOptionsState.options;
}
return [selectedRoom, ...roomOptionsState.options];
}, [roomOptionsState.options, selectedRoom]);
const changeQuery = (value) => { const changeQuery = (value) => {
setQuery(value); setQuery(value);
@ -103,7 +115,9 @@ export function useRoomPinsPage() {
const openCreate = () => { const openCreate = () => {
setForm(emptyForm()); setForm(emptyForm());
setRoomSearch(""); setRoomInput("");
setRoomKeyword("");
setSelectedRoom(null);
setRoomOptionsState({ error: "", loading: false, options: [] }); setRoomOptionsState({ error: "", loading: false, options: [] });
setActiveAction("create"); setActiveAction("create");
}; };
@ -151,7 +165,23 @@ export function useRoomPinsPage() {
} }
}; };
const selectedRoom = roomOptionsState.options.find((room) => room.roomId === form.roomId) || null; const changeRoomInput = (value) => {
setRoomInput(value);
setRoomKeyword(value);
if (selectedRoom && roomOptionLabel(selectedRoom) !== value) {
setSelectedRoom(null);
setForm((current) => ({ ...current, roomId: "" }));
}
};
const selectRoom = (room) => {
setSelectedRoom(room || null);
setRoomInput(room ? roomOptionLabel(room) : "");
if (!room) {
setRoomKeyword("");
}
setForm((current) => ({ ...current, roomId: room?.roomId || "" }));
};
return { return {
abilities, abilities,
@ -174,15 +204,23 @@ export function useRoomPinsPage() {
regionOptions, regionOptions,
reload, reload,
resetFilters, resetFilters,
roomOptions: roomOptionsState.options, changeRoomInput,
roomInput,
roomOptions,
roomOptionsError: roomOptionsState.error, roomOptionsError: roomOptionsState.error,
roomOptionsLoading: roomOptionsState.loading, roomOptionsLoading: roomOptionsState.loading,
roomSearch, selectRoom,
selectedRoom, selectedRoom,
setForm, setForm,
setPage, setPage,
setRoomSearch,
status, status,
submitCreate, submitCreate,
}; };
} }
function roomOptionLabel(room) {
if (!room) {
return "";
}
return [room.title, room.roomShortId, room.roomId].filter(Boolean).join(" / ");
}

View File

@ -163,6 +163,7 @@ export function RoomPinsPage() {
isOptionEqualToValue={(option, value) => option.roomId === value.roomId} isOptionEqualToValue={(option, value) => option.roomId === value.roomId}
loading={page.roomOptionsLoading} loading={page.roomOptionsLoading}
noOptionsText="当前无数据" noOptionsText="当前无数据"
inputValue={page.roomInput}
options={page.roomOptions} options={page.roomOptions}
renderInput={(params) => ( renderInput={(params) => (
<TextField <TextField
@ -180,8 +181,15 @@ export function RoomPinsPage() {
)} )}
size="small" size="small"
value={page.selectedRoom} value={page.selectedRoom}
onChange={(_, option) => page.setForm({ ...page.form, roomId: option?.roomId || "" })} onChange={(_, option) => page.selectRoom(option)}
onInputChange={(_, value) => page.setRoomSearch(value)} onInputChange={(_, value, reason) => {
if (reason === "input") {
page.changeRoomInput(value);
}
if (reason === "clear") {
page.selectRoom(null);
}
}}
/> />
<TextField <TextField
label="权重" label="权重"