Add pageChange event
This commit is contained in:
20
README.md
20
README.md
@@ -44,6 +44,26 @@ Import component and styles in App component
|
|||||||
| `autoplayDirection` | `string` | `'next'` | Auto play change direction (`next` or `prev`) |
|
| `autoplayDirection` | `string` | `'next'` | Auto play change direction (`next` or `prev`) |
|
||||||
| `dots` | `boolean` | `true` | Current page indicator dots |
|
| `dots` | `boolean` | `true` | Current page indicator dots |
|
||||||
|
|
||||||
|
|
||||||
|
# Event
|
||||||
|
|
||||||
|
## `pageChange`
|
||||||
|
Is dispatched on page change
|
||||||
|
|
||||||
|
| Payload field | Type | Description |
|
||||||
|
|--------------------|-------------|---------------------------------------|
|
||||||
|
| `event.detail` | `number` | Current page index |
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Carousel
|
||||||
|
on:pageChange={
|
||||||
|
event => console.log(`Current page index: ${event.detail}`)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<!-- -->
|
||||||
|
</Carousel>
|
||||||
|
```
|
||||||
|
|
||||||
## Slots
|
## Slots
|
||||||
|
|
||||||
### `prev` and `next`
|
### `prev` and `next`
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script>
|
<script>
|
||||||
import { onDestroy, onMount, tick } from 'svelte'
|
import { onDestroy, onMount, tick, createEventDispatcher } from 'svelte'
|
||||||
import { createStore } from '../../store'
|
import { createStore } from '../../store'
|
||||||
import Dots from '../Dots/Dots.svelte'
|
import Dots from '../Dots/Dots.svelte'
|
||||||
import Arrow from '../Arrow/Arrow.svelte'
|
import Arrow from '../Arrow/Arrow.svelte'
|
||||||
@@ -11,6 +11,8 @@
|
|||||||
} from '../../utils/event'
|
} from '../../utils/event'
|
||||||
import { getAdjacentIndexes } from '../../utils/page'
|
import { getAdjacentIndexes } from '../../utils/page'
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher()
|
||||||
|
|
||||||
const directionFnDescription = {
|
const directionFnDescription = {
|
||||||
[NEXT]: showNextPage,
|
[NEXT]: showNextPage,
|
||||||
[PREV]: showPrevPage
|
[PREV]: showPrevPage
|
||||||
@@ -60,6 +62,8 @@
|
|||||||
let store = createStore()
|
let store = createStore()
|
||||||
let currentPageIndex = 0
|
let currentPageIndex = 0
|
||||||
$: originalCurrentPageIndex = currentPageIndex - Number(infinite);
|
$: originalCurrentPageIndex = currentPageIndex - Number(infinite);
|
||||||
|
$: dispatch('pageChange', originalCurrentPageIndex)
|
||||||
|
|
||||||
let pagesCount = 0
|
let pagesCount = 0
|
||||||
$: originalPagesCount = Math.max(pagesCount - (infinite ? 2 : 0), 1) // without clones
|
$: originalPagesCount = Math.max(pagesCount - (infinite ? 2 : 0), 1) // without clones
|
||||||
let pageWidth = 0
|
let pageWidth = 0
|
||||||
|
|||||||
@@ -72,6 +72,9 @@
|
|||||||
{autoplayDuration}
|
{autoplayDuration}
|
||||||
{autoplayDirection}
|
{autoplayDirection}
|
||||||
{dots}
|
{dots}
|
||||||
|
on:pageChange={
|
||||||
|
event => console.log(`Current page index: ${event.detail}`)
|
||||||
|
}
|
||||||
>
|
>
|
||||||
{#each colors as { color, text } (color)}
|
{#each colors as { color, text } (color)}
|
||||||
<div
|
<div
|
||||||
|
|||||||
@@ -217,6 +217,29 @@ Import component and styles in App component
|
|||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
# Event
|
||||||
|
|
||||||
|
## `pageChange`
|
||||||
|
Is dispatched on page change
|
||||||
|
|
||||||
|
<div class="table-wrapper">
|
||||||
|
|
||||||
|
| Payload field | Type | Description |
|
||||||
|
|--------------------|-------------|---------------------------------------|
|
||||||
|
| `event.detail` | `number` | Current page index |
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Carousel
|
||||||
|
on:pageChange={
|
||||||
|
event => console.log(`Current page index: ${event.detail}`)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<!-- -->
|
||||||
|
</Carousel>
|
||||||
|
```
|
||||||
|
|
||||||
# Slots
|
# Slots
|
||||||
|
|
||||||
## `prev` and `next`
|
## `prev` and `next`
|
||||||
|
|||||||
Reference in New Issue
Block a user