Facebook
From Talal, 1 Month ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 140
  1. import React, {useState, useRef} from 'react';
  2. import {
  3.   StyleSheet,
  4.   View,
  5.   FlatList,
  6.   Image,
  7.   Alert,
  8.   Pressable,
  9.   Button,
  10.   Modal,
  11.   TextInput,
  12.   Keyboard,
  13.   useWindowDimensions,
  14. } from 'react-native';
  15.  
  16. const App = () => {
  17.   const imageUri =
  18.     'https://images.unsplash.com/photo-1453728013993-6d66e9c9123a?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA==&auto=format&fit=crop&w=870&q=80';
  19.   const images = Array.from({length: 13}, (_, i) => ({
  20.     uri: imageUri,
  21.     key: i.toString(),
  22.   }));
  23.   const [modalVisible, setModalVisible] = useState(false);
  24.   const [indexInput, setIndexInput] = useState('');
  25.   const flatListRef = useRef(null);
  26.   const {width} = useWindowDimensions();
  27.  
  28.   const handleImageSelection = index => {
  29.     Alert.alert(`You have selected image ${index + 1}`); // Display 1-indexed for user clarity
  30.   };
  31.  
  32.   const handleScrollToIndex = () => {
  33.     const index = parseInt(indexInput, 10) - 1; // Convert to 0-indexed
  34.     if (!isNaN(index) && index >= 0 && index < images.length) {
  35.       flatListRef.current?.scrollToIndex({animated: true, index});
  36.     } else {
  37.       Alert.alert&#40;
  38.         'Invalid Index',
  39.         `Please enter a number between 1 and ${images.length}`,
  40.       &#41;;
  41.     }
  42.     setModalVisible(false);
  43.     setIndexInput('');
  44.     Keyboard.dismiss(); // Hide keyboard after submission
  45.   };
  46.   const getItemLayout = (data, index) => ({
  47.     length: 100,
  48.     offset: 100 * index,
  49.     index,
  50.   }); // assuming each item is 100px high
  51.  
  52.   const onScrollToIndexFailed = info => {
  53.     const wait = new Promise(resolve => setTimeout(resolve, 500));
  54.     wait.then(() => {
  55.       flatListRef.current?.scrollToIndex({index: info.index, animated: true});
  56.     });
  57.   };
  58.   return (
  59.     <View  {width}]}>
  60.       <Button title="Scroll to Image"  => setModalVisible(true)} />
  61.  
  62.       <Modal visible={modalVisible} animati transparent>
  63.         <View >
  64.           <View >
  65.             <TextInput
  66.                
  67.                
  68.               value={indexInput}
  69.               keyboardType="numeric"
  70.               placeholder={`Image number (1-${images.length})`}
  71.             />
  72.             <Button  title="Go" />
  73.             <Button  => setModalVisible(false)} title="Cancel" />
  74.           </View>
  75.         </View>
  76.       </Modal>
  77.  
  78.       <FlatList
  79.         ref={flatListRef}
  80.         data={images}
  81.         getItemLayout={getItemLayout}
  82.          
  83.         renderItem={({item, index}) => (
  84.           <Pressable  => handleImageSelection(index)}>
  85.             <Image
  86.               source={item}
  87.                
  88.                 => Alert.alert&#40;'Error', 'Failed to load image'&#41;}
  89.             />
  90.           </Pressable>
  91.         )}
  92.       />
  93.     </View>
  94.   );
  95. };
  96. const styles = StyleSheet.create({
  97.   container: {
  98.     flex: 1,
  99.     padding: 16,
  100.   },
  101.   image: {
  102.     width: 100,
  103.     height: 100,
  104.   },
  105. });
  106.  
  107. export default App;
  108.