From 1178b2ab349e055b91a472d40c9b581db951bf22 Mon Sep 17 00:00:00 2001 From: Fahad67 Date: Sat, 3 Sep 2022 18:39:40 +0500 Subject: [PATCH] add dynamic image access key --- README.md | 3 +- package.json | 2 +- src/App.css | 5 ++-- src/App.js | 23 +++++++------- src/lib/components/Slider.js | 58 +++++++++++++++++++----------------- 5 files changed, 49 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 2e6a41e..04090cf 100644 --- a/README.md +++ b/README.md @@ -58,11 +58,12 @@ function App() {
diff --git a/package.json b/package.json index 4eec031..233cf0c 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "Slider", "Multi Images" ], - "version": "2.0.0", + "version": "2.1.0", "private": false, "main": "dist/index.js", "module": "dist/index.js", diff --git a/src/App.css b/src/App.css index df9aee5..7a8e17e 100644 --- a/src/App.css +++ b/src/App.css @@ -1,6 +1,7 @@ .App { - width: 50%; - height: 5vh; + width: 98%; + margin: auto; + text-align: center; } diff --git a/src/App.js b/src/App.js index d43157c..d089ee5 100644 --- a/src/App.js +++ b/src/App.js @@ -3,29 +3,29 @@ import { Slider } from './lib'; const data = [ { - image: + imageUrl: 'https://images.pexels.com/photos/10161004/pexels-photo-10161004.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260', - description: `Duis sed bibendum libero. Vestibulum ut enim vitae leo finibus condimentum ut sit amet quam. Maecenas eget malesuada sem, id ultrices diam. `, + description: `image 1`, }, { - image: + imageUrl: 'https://images.pexels.com/photos/10166802/pexels-photo-10166802.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260', - description: `Duis sed bibendum libero. Vestibulum ut enim vitae leo finibus condimentum ut sit amet quam. Maecenas eget malesuada sem, id ultrices diam. `, + description: `image 2`, }, { - image: + imageUrl: 'https://worlduniversityofdesign.ac.in/assets/images/bgs/school-of-visual-arts-banner.jpg', - description: `Duis sed bibendum libero. Vestibulum ut enim vitae leo finibus condimentum ut sit amet quam. Maecenas eget malesuada sem, id ultrices diam. `, + description: `image 3`, }, { - image: + imageUrl: 'https://images.pexels.com/photos/10166802/pexels-photo-10166802.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260', - description: `Duis sed bibendum libero. Vestibulum ut enim vitae leo finibus condimentum ut sit amet quam. Maecenas eget malesuada sem, id ultrices diam. `, + description: `image 4`, }, { - image: + imageUrl: 'https://worlduniversityofdesign.ac.in/assets/images/bgs/school-of-visual-arts-banner.jpg', - description: `Duis sed bibendum libero. Vestibulum ut enim vitae leo finibus condimentum ut sit amet quam. Maecenas eget malesuada sem, id ultrices diam. `, + description: `image 5`, }, ]; @@ -34,11 +34,12 @@ function App() {
diff --git a/src/lib/components/Slider.js b/src/lib/components/Slider.js index 3bca567..f679fc3 100644 --- a/src/lib/components/Slider.js +++ b/src/lib/components/Slider.js @@ -8,6 +8,7 @@ import { ArrowsHandler } from './Arrows_Handler'; import ImageViewer from 'react-simple-image-viewer'; const Slider = ({ + imageKeyToAccess, sliderResource, backgroundColor, dotColor, @@ -23,11 +24,15 @@ const Slider = ({ const [activeIndex, setActiveIndex] = useState(0); useEffect(() => { - if (sliderResource && Object.keys(sliderResource).length > 0) { - const images = sliderResource?.map(image => image?.image); + if ( + sliderResource && + Object.keys(sliderResource).length > 0 && + imageKeyToAccess + ) { + const images = sliderResource?.map(image => image[imageKeyToAccess]); setImageList(images); } - }, [sliderResource]); + }, [sliderResource, imageKeyToAccess]); const openImageViewer = useCallback(index => { setCurrentImage(index); @@ -65,33 +70,32 @@ const Slider = ({ onClick={() => openImageViewer(activeIndex)} component='div' sx={ - sliderResource - ? imageBox( - sliderResource[activeIndex]?.image, - imageTransition || '', - imageHeight || '50vh' - ) - : imageBox( - data[activeIndex].image, - imageTransition || '', - imageHeight || '50vh' - ) + sliderResource && + imageKeyToAccess && + imageBox( + sliderResource[activeIndex][imageKeyToAccess], + imageTransition || '', + imageHeight || '50vh' + ) } /> - - - + + + + + + {isViewerOpen && (