Skip to content

Commit 89179cf

Browse files
authored
Merge pull request #461 from ismail9k/validate-itemsToShow
Validate itemsToShow configuration
2 parents a9ddf34 + 45e86c0 commit 89179cf

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

docs/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
| Prop | Default | Description |
66
| ---------------------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
77
| `enabled` | true | Controlled weather the carousel is enabled or disabled. <Badge text="0.8.0"/> |
8-
| `itemsToShow` | 1 | Count of items to showed per view (can be a fraction). |
8+
| `itemsToShow` | 1 | Count of items to showed per view (can be a fraction). Must be between 1 and the total number of slides. If set to a value less than 1, it defaults to 1. If set to a value greater than the total number of slides, it defaults to the total number of slides. |
99
| `itemsToScroll` | 1 | Number of slides to be scrolled |
1010
| `wrapAround` | false | Enable infinite scrolling mode. |
1111
| `snapAlign` | 'center' | Controls the carousel position alignment, can be 'start', 'end', 'center-[odd\|even]' |

src/components/Carousel/Carousel.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ export const Carousel = defineComponent({
188188
min: minSlideIndex.value,
189189
})
190190
}
191+
192+
// Validate itemsToShow
193+
config.itemsToShow = getNumberInRange({
194+
val: config.itemsToShow,
195+
max: slidesCount.value,
196+
min: 1,
197+
})
191198
}
192199

193200
const ignoreAnimations = computed<false | string[]>(() => {

tests/integration/carousel.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ describe('Carousel.ts', () => {
104104
await triggerKeyEvent('ArrowLeft')
105105
expect(wrapper.props('modelValue')).toBe(1)
106106
})
107+
108+
it('Should default itemsToShow to 1 if less than 1', async () => {
109+
await wrapper.setProps({ itemsToShow: 0 })
110+
const slides = wrapper.findAll('.carousel__slide--visible')
111+
expect(slides.length).toBe(1)
112+
})
113+
114+
it('Should default itemsToShow to slidesCount if greater than slidesCount', async () => {
115+
await wrapper.setProps({ itemsToShow: 10 })
116+
const slides = wrapper.findAll('.carousel__slide--visible')
117+
expect(slides.length).toBe(5)
118+
})
107119
})
108120

109121
describe('Slotted Carousel.ts', () => {

0 commit comments

Comments
 (0)