Skip to content

Commit

Permalink
Merge pull request #461 from ismail9k/validate-itemsToShow
Browse files Browse the repository at this point in the history
Validate itemsToShow configuration
  • Loading branch information
ismail9k authored Dec 23, 2024
2 parents a9ddf34 + 45e86c0 commit 89179cf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Prop | Default | Description |
| ---------------------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `enabled` | true | Controlled weather the carousel is enabled or disabled. <Badge text="0.8.0"/> |
| `itemsToShow` | 1 | Count of items to showed per view (can be a fraction). |
| `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. |
| `itemsToScroll` | 1 | Number of slides to be scrolled |
| `wrapAround` | false | Enable infinite scrolling mode. |
| `snapAlign` | 'center' | Controls the carousel position alignment, can be 'start', 'end', 'center-[odd\|even]' |
Expand Down
7 changes: 7 additions & 0 deletions src/components/Carousel/Carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ export const Carousel = defineComponent({
min: minSlideIndex.value,
})
}

// Validate itemsToShow
config.itemsToShow = getNumberInRange({
val: config.itemsToShow,
max: slidesCount.value,
min: 1,
})
}

const ignoreAnimations = computed<false | string[]>(() => {
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/carousel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ describe('Carousel.ts', () => {
await triggerKeyEvent('ArrowLeft')
expect(wrapper.props('modelValue')).toBe(1)
})

it('Should default itemsToShow to 1 if less than 1', async () => {
await wrapper.setProps({ itemsToShow: 0 })
const slides = wrapper.findAll('.carousel__slide--visible')
expect(slides.length).toBe(1)
})

it('Should default itemsToShow to slidesCount if greater than slidesCount', async () => {
await wrapper.setProps({ itemsToShow: 10 })
const slides = wrapper.findAll('.carousel__slide--visible')
expect(slides.length).toBe(5)
})
})

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

0 comments on commit 89179cf

Please sign in to comment.