Git Product home page Git Product logo

Comments (2)

bd-arc avatar bd-arc commented on August 28, 2024

@ajonno Hi! You're going to need the prop onPageSelected for that. The following code should get you started ;)

const CAPTIONS = ['Caption 1', 'Caption 2', 'Caption 3'];

render () {
    const { caption } = this.state;

    <View>
        <Gallery onPageSelected={(index) => { this.setState({ caption: CAPTIONS[index] }); }} />
        <View>
            <Text>{ caption }</Text>
        </View>
    </View>
}

from react-native-image-gallery.

Exilz avatar Exilz commented on August 28, 2024

Hi,
here's how I do it

When the user taps the thumbnail, my router (react-navigation or NavigationExperimental depending on the project) opens a modal in which I'm rendering a view I'm calling ImageGallery.

In this view, I render react-native-gallery and any view I want on top of it, like the captions or the slide counter.

Here's a dumbed-down example

import React, { Component, PropTypes } from 'react';
import { View, Text } from 'react-native';
import Gallery from 'react-native-image-gallery';
import styles from 'app/src/styles/views/ImageGallery.style';

export default class ImageGallery extends Component {

    static propTypes () {
        return {
            images: PropTypes.PropTypes.arrayOf(
                PropTypes.shape({
                    url: PropTypes.string,
                    thumbnail: PropTypes.string,
                    imageId: PropTypes.string,
                    caption: PropTypes.string
                })
            ).isRequired,
            index: PropTypes.number
        };
    };

    constructor (props) {
        super(props);
        const { index, images } = props;

        this.onChangeImage = this.onChangeImage.bind(this);
        this.state = {
            status: 1,
            index: index || 0,
            imagesCount: images.length
        };
    }

    _getImagesUrls (images) {
        let urls = [];
        images.forEach((image) => {
            urls.push({
                source: { uri: image.url }
            });
        });
        return urls;
    }

    onChangeImage (index) {
        this.setState({ index });
    }

    get captions () {
        const { index } = this.state;
        const { images } = this.props;

        const captionField = images[index].caption ? (
            <Text style={styles.caption} numberOfLines={2}>{images[index].caption}</Text>
        ) : false;

        if (!captionField) {
            return false;
        }

        return (
            <View style={styles.captionContainer}>
                { captionField }
                <View style={styles.captionLine} />
            </View>
        );
    }

    get counter () {
        const { index, imagesCount } = this.state;

        return (
            <View style={styles.counterContainer}>
                <Text style={styles.counterCurrent}>{index + 1}</Text>
                <Text style={styles.counterTotal}>| {imagesCount}</Text>
            </View>
        );
    }

    get gallery () {
        const { images, index } = this.props;

        return (
            <Gallery
              style={styles.galleryContainer}
              initialPage={index || 0}
              onPageSelected={this.onChangeImage}
              images={this._getImagesUrls(images)} />
        );
    }

    render () {
        return (
            <View style={styles.container}>
                { this.gallery }
                { this.captions }
                { this.counter }
            </View>
        );
    }
}

from react-native-image-gallery.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.