@treely/strapi-slices 6.1.1 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/slices/TextCarousel/TextCarousel.d.ts +1 -0
- package/dist/slices/TextCarousel/TextCarousel.stories.d.ts +1 -0
- package/dist/strapi-slices.cjs.development.js +19 -1
- package/dist/strapi-slices.cjs.development.js.map +1 -1
- package/dist/strapi-slices.cjs.production.min.js +1 -1
- package/dist/strapi-slices.cjs.production.min.js.map +1 -1
- package/dist/strapi-slices.esm.js +19 -1
- package/dist/strapi-slices.esm.js.map +1 -1
- package/dist/utils/shuffleElements.d.ts +2 -0
- package/package.json +1 -1
- package/src/slices/TextCarousel/TextCarousel.stories.tsx +9 -0
- package/src/slices/TextCarousel/TextCarousel.tsx +9 -1
- package/src/utils/shuffleElements.test.ts +30 -0
- package/src/utils/shuffleElements.ts +10 -0
package/package.json
CHANGED
|
@@ -47,6 +47,15 @@ Minimal.args = {
|
|
|
47
47
|
},
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
+
export const WithShuffledCards = Template.bind({});
|
|
51
|
+
WithShuffledCards.args = {
|
|
52
|
+
slice: {
|
|
53
|
+
title: 'Title',
|
|
54
|
+
slides,
|
|
55
|
+
isShuffled: true,
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
50
59
|
export const WithTagline = Template.bind({});
|
|
51
60
|
WithTagline.args = {
|
|
52
61
|
slice: {
|
|
@@ -26,10 +26,12 @@ import { ArrowLeft, ArrowRight } from '@phosphor-icons/react';
|
|
|
26
26
|
import { IntlContext } from '../../components/ContextProvider';
|
|
27
27
|
import strapiLinkUrl from '../../utils/strapiLinkUrl';
|
|
28
28
|
import { useRouter } from 'next/router';
|
|
29
|
+
import shuffleElements from '../../utils/shuffleElements';
|
|
29
30
|
|
|
30
31
|
interface TextCarouselSlice extends StrapiDefaultHeader {
|
|
31
32
|
slides: StrapiTextCardWithIcon[];
|
|
32
33
|
button?: StrapiLink;
|
|
34
|
+
isShuffled?: boolean;
|
|
33
35
|
}
|
|
34
36
|
export interface TextCarouselProps {
|
|
35
37
|
slice: TextCarouselSlice;
|
|
@@ -78,6 +80,12 @@ export const TextCarousel: React.FC<TextCarouselProps> = ({
|
|
|
78
80
|
}, [itemWidth, sliderIndex, sliderItemsWidth, windowWidth]);
|
|
79
81
|
|
|
80
82
|
const canMoveLeft = useMemo(() => sliderIndex !== 0, [sliderIndex]);
|
|
83
|
+
const shuffleSlides = useMemo(
|
|
84
|
+
() => shuffleElements(slice.slides),
|
|
85
|
+
[slice.slides]
|
|
86
|
+
);
|
|
87
|
+
const { slides, isShuffled = false } = slice;
|
|
88
|
+
const displaySlides = isShuffled ? shuffleSlides : slides;
|
|
81
89
|
|
|
82
90
|
return (
|
|
83
91
|
<DefaultSectionContainer backgroundColor={primary50} title={slice.title}>
|
|
@@ -104,7 +112,7 @@ export const TextCarousel: React.FC<TextCarouselProps> = ({
|
|
|
104
112
|
ease: 'easeInOut',
|
|
105
113
|
}}
|
|
106
114
|
>
|
|
107
|
-
{
|
|
115
|
+
{displaySlides.map(({ id, title, text, icon, image, button }) => (
|
|
108
116
|
<CardContainer key={id} ref={itemRef}>
|
|
109
117
|
<TextCardWithIcon
|
|
110
118
|
title={title}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import shuffleElements from './shuffleElements';
|
|
2
|
+
|
|
3
|
+
const originalSlides = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }];
|
|
4
|
+
|
|
5
|
+
describe('shuffleElement', () => {
|
|
6
|
+
it('returns a new array with the same elements but in a different order', () => {
|
|
7
|
+
const shuffled = shuffleElements(originalSlides);
|
|
8
|
+
|
|
9
|
+
// Check that the shuffled array is not the same reference as the original
|
|
10
|
+
expect(shuffled).not.toBe(originalSlides);
|
|
11
|
+
|
|
12
|
+
// Check that the shuffled array has the same elements as the original
|
|
13
|
+
expect(shuffled).toEqual(expect.arrayContaining(originalSlides));
|
|
14
|
+
expect(originalSlides).toEqual(expect.arrayContaining(shuffled));
|
|
15
|
+
|
|
16
|
+
// Verify that at least one element is in a different position to ensure it's shuffled
|
|
17
|
+
const isShuffled = originalSlides.some(
|
|
18
|
+
(slide, index) => slide.id !== shuffled[index].id
|
|
19
|
+
);
|
|
20
|
+
expect(isShuffled).toBe(true);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('does not mutate the original array', () => {
|
|
24
|
+
const originalSlidesCopy = [...originalSlides];
|
|
25
|
+
shuffleElements(originalSlides);
|
|
26
|
+
|
|
27
|
+
// Verify that the original array remains unchanged
|
|
28
|
+
expect(originalSlides).toEqual(originalSlidesCopy);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const shuffleElements = (slides: any[]) => {
|
|
2
|
+
const slidesCopy = [...slides]; // Create a copy to avoid mutating the original array
|
|
3
|
+
for (let i = slidesCopy.length - 1; i > 0; i--) {
|
|
4
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
5
|
+
[slidesCopy[i], slidesCopy[j]] = [slidesCopy[j], slidesCopy[i]];
|
|
6
|
+
}
|
|
7
|
+
return slidesCopy;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export default shuffleElements;
|