CarouselBoxコンポーネントのrenderItem関数を分離し、MenuコンポーネントでのcurrentStationの管理をnearPositionStationに変更

This commit is contained in:
harukin-expo-dev-env 2025-04-13 16:54:02 +00:00
parent 748350178d
commit 35bb460b54
2 changed files with 53 additions and 61 deletions

View File

@ -46,25 +46,7 @@ export const CarouselBox = ({
setDotButton(data === "true"); setDotButton(data === "true");
}); });
}, []); }, []);
return ( const RenderItem = ({ item, index }) => {
<View style={{ flex: 1, paddingTop: 10 }}>
<Carousel
ref={carouselRef}
data={allStationData}
height={(((width / 100) * 80) / 20) * 9 + 10}
pagingEnabled={true}
snapEnabled={true}
loop={false}
width={width}
style={{ width: width, alignContent: "center" }}
mode="parallax"
modeConfig={{
parallaxScrollingScale: 1,
parallaxScrollingOffset: 100,
parallaxAdjacentItemScale: 0.8,
}}
onSnapToItem={setSelectedCurrentStation}
renderItem={({ item, index }) => {
return ( return (
<View <View
style={{ style={{
@ -86,7 +68,26 @@ export const CarouselBox = ({
<View style={{ flex: 1 }} /> <View style={{ flex: 1 }} />
</View> </View>
); );
};
return (
<View style={{ flex: 1, paddingTop: 10 }}>
<Carousel
ref={carouselRef}
data={allStationData}
height={(((width / 100) * 80) / 20) * 9 + 10}
pagingEnabled={true}
snapEnabled={true}
loop={false}
width={width}
style={{ width: width, alignContent: "center" }}
mode="parallax"
modeConfig={{
parallaxScrollingScale: 1,
parallaxScrollingOffset: 100,
parallaxAdjacentItemScale: 0.8,
}} }}
onSnapToItem={setSelectedCurrentStation}
renderItem={RenderItem}
/> />
<View <View
style={{ style={{
@ -99,25 +100,21 @@ export const CarouselBox = ({
{originalStationList && {originalStationList &&
allStationData.map((d, index) => { allStationData.map((d, index) => {
const active = index == selectedCurrentStation; const active = index == selectedCurrentStation;
const numberIndex = d[0].StationNumber; const numberKey = d[0].StationNumber + index;
if (dotButton) { return dotButton ? (
return (
<StationNumber <StationNumber
onPress={() => setSelectedCurrentStation(index)} onPress={() => setSelectedCurrentStation(index)}
currentStation={d} currentStation={d}
active={active} active={active}
key={numberIndex + index} key={numberKey}
/> />
); ) : (
} else {
return (
<SimpleDot <SimpleDot
onPress={() => setSelectedCurrentStation(index)} onPress={() => setSelectedCurrentStation(index)}
active={active} active={active}
key={numberIndex + index} key={numberKey}
/> />
); );
}
})} })}
</View> </View>
</View> </View>

33
menu.js
View File

@ -84,7 +84,8 @@ export default function Menu({ getCurrentTrain, scrollRef }) {
let returnDataBase = lineList let returnDataBase = lineList
.map((d) => findStationEachLine(originalStationList[d])) .map((d) => findStationEachLine(originalStationList[d]))
.filter((d) => { .filter((d) => {
return d.length > 0}) return d.length > 0;
})
.reduce((pre, current) => { .reduce((pre, current) => {
pre.push(...current); pre.push(...current);
return pre; return pre;
@ -92,14 +93,14 @@ export default function Menu({ getCurrentTrain, scrollRef }) {
if (returnDataBase.length) { if (returnDataBase.length) {
let currentStation = currentStation == undefined ? [] : currentStation; let currentStation = currentStation == undefined ? [] : currentStation;
if (currentStation.toString() != returnDataBase.toString()) { if (currentStation.toString() != returnDataBase.toString()) {
setCurrentStation(returnDataBase); setNearPositionStation(returnDataBase);
} }
} else { } else {
setCurrentStation(undefined); setNearPositionStation(undefined);
} }
}; };
const [currentStation, setCurrentStation] = useState(undefined); //第三要素 const [nearPositionStation, setNearPositionStation] = useState(undefined); //第三要素
const carouselRef = useRef(); const carouselRef = useRef();
const [selectedCurrentStation, setSelectedCurrentStation] = useState(0); const [selectedCurrentStation, setSelectedCurrentStation] = useState(0);
@ -107,16 +108,12 @@ export default function Menu({ getCurrentTrain, scrollRef }) {
const [allStationData, setAllStationData] = useState([]); const [allStationData, setAllStationData] = useState([]);
useEffect(() => { useEffect(() => {
if (stationListMode == "position") { if (stationListMode == "position") {
console.log(currentStation); console.log(nearPositionStation);
setAllStationData( setAllStationData([nearPositionStation].filter((d) => d != undefined));
[currentStation].filter((d) => d != undefined)
);
} else { } else {
setAllStationData( setAllStationData(favoriteStation.filter((d) => d != undefined));
favoriteStation.filter((d) => d != undefined)
);
} }
}, [currentStation, favoriteStation, stationListMode]); }, [nearPositionStation, favoriteStation, stationListMode]);
useEffect(() => { useEffect(() => {
if (allStationData.length == 0) { if (allStationData.length == 0) {
setSelectedCurrentStation(0); setSelectedCurrentStation(0);
@ -126,7 +123,7 @@ export default function Menu({ getCurrentTrain, scrollRef }) {
const count = selectedCurrentStation - 1; const count = selectedCurrentStation - 1;
setSelectedCurrentStation(count); setSelectedCurrentStation(count);
} }
}, [selectedCurrentStation, currentStation, allStationData]); }, [selectedCurrentStation, nearPositionStation, allStationData]);
useEffect(() => { useEffect(() => {
if (!carouselRef.current) return; if (!carouselRef.current) return;
carouselRef?.current.scrollTo({ carouselRef?.current.scrollTo({
@ -136,12 +133,10 @@ export default function Menu({ getCurrentTrain, scrollRef }) {
}, [selectedCurrentStation]); }, [selectedCurrentStation]);
useEffect(() => { useEffect(() => {
if (originalStationList == undefined) return;
if (allStationData.length == 0) return; if (allStationData.length == 0) return;
if (allStationData[selectedCurrentStation] == undefined) return; if (allStationData[selectedCurrentStation] == undefined) return;
const { lat, lng } = const { lat, lng } = allStationData[selectedCurrentStation][0];
originalStationList &&
allStationData[selectedCurrentStation] &&
allStationData[selectedCurrentStation][0];
const mapRegion = { const mapRegion = {
latitude: lat, latitude: lat,
longitude: lng, longitude: lng,
@ -149,7 +144,7 @@ export default function Menu({ getCurrentTrain, scrollRef }) {
longitudeDelta: 0.05, longitudeDelta: 0.05,
}; };
mapsRef.current.animateToRegion(mapRegion, 1000); mapsRef.current.animateToRegion(mapRegion, 1000);
}, [selectedCurrentStation, currentStation, allStationData, mapsRef]); }, [selectedCurrentStation, nearPositionStation, allStationData, mapsRef]);
return ( return (
<View <View
style={{ style={{
@ -204,7 +199,7 @@ export default function Menu({ getCurrentTrain, scrollRef }) {
{...{ {...{
originalStationList, originalStationList,
allStationData, allStationData,
currentStation, currentStation: nearPositionStation,
setSelectedCurrentStation, setSelectedCurrentStation,
carouselRef, carouselRef,
selectedCurrentStation, selectedCurrentStation,