import "./style.scss"; import React, { useState, useEffect } from "react"; import search from "./search.png"; import close from "./close.png"; import go from "./go.png"; import arrow from "./arrow.png"; import ShopsWithFilter from "../ShopsWithFilter/ShopsWithFilter"; const STATES = { init: 0, moreFac: 1, facList: 2, }; export const DefaultPopupStates = STATES; const initMarginBottom = 47; const DefaultPopup = ({ state, setState, onSearch, facilities, onClickFac, mall, onClick, setEnd, hasTab, blurMap = () => {}, }) => { const marginBottomStateMap = { 0: 47, 1: hasTab ? window.innerHeight - 52 - 179 - 98 : window.innerHeight - 52 - 179, }; const baseMarginBottom = marginBottomStateMap[state]; const [facList, setFacList] = useState(null); const [focused, setFocused] = useState(null); const [start, setStart] = useState(null); const [marginBottom, setMarginBottom] = useState(null); const [doTransition, setdoTransition] = useState(false); useEffect(() => { setdoTransition(true); setMarginBottom(baseMarginBottom); setTimeout(() => { setdoTransition(false); }, 500); }, [state]); const handleTouchStart = (e) => { if (start) return; setStart({ identifier: e.changedTouches[0].identifier, y: e.changedTouches[0].clientY, }); }; const handleTouchMove = (e) => { if (!start) return; const touch = Array.from(e.changedTouches).find( ({ identifier }) => identifier === start.identifier ); if (!touch) return; const delta = touch.clientY - start.y; setMarginBottom(baseMarginBottom - delta); }; const handleTouchEnd = (e) => { if (!start) return; const touch = Array.from(e.changedTouches).find( ({ identifier }) => identifier === start.identifier ); if (!touch) return; setStart(null); const delta = touch.clientY - start.y; const newState = state === STATES.init && delta < -100 ? STATES.moreFac : state === STATES.moreFac && delta > 100 ? STATES.init : state; if (state === newState) { setdoTransition(true); setMarginBottom(baseMarginBottom); setTimeout(() => { setdoTransition(false); }, 500); } else setState(newState); }; return ( <> {state === STATES.init || state === STATES.moreFac ? (
setState(state === STATES.moreFac ? STATES.init : STATES.moreFac) } >
搜索店铺
{Object.entries(facilities).map(([name, list]) => (
{ list.sort((a, b) => a.floorOrder - b.floorOrder); setFacList(list); setState(STATES.facList); }} > {name}
))}
) : ( <> { e.preventDefault(); e.stopPropagation(); setState(STATES.init); setFacList(null); blurMap(); }} >
{facList && facList.map((fac, i) => { const showRow1 = i === 0 || fac.floorName !== facList[i - 1].floorName; const isActive = focused === fac.id; return (
{showRow1 && (
{fac.floorName}
)}
{ setFocused(fac.id); onClickFac(fac.id); }} > {fac.name} {fac.floorName} {isActive && (
{ e.stopPropagation(); setState(STATES.init); onClickFac(fac.id); }} > GO
)}
); })}
)} {state !== STATES.facList && (
{ setState(STATES.moreFac); }} > { setState(STATES.init); onClick(e); }} wingHeight="calc(100vh - 310px)" >
)} ); }; export default DefaultPopup;