Skip to content

Commit a9c91af

Browse files
committed
refactor: change Context to CCarouselContext
1 parent 8467c8a commit a9c91af

File tree

5 files changed

+28
-29
lines changed

5 files changed

+28
-29
lines changed

src/components/carousel/CCarousel.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import React, { forwardRef, HTMLAttributes, useState, useEffect, useRef } from 'react'
1+
import React, {
2+
createContext,
3+
forwardRef,
4+
HTMLAttributes,
5+
useState,
6+
useEffect,
7+
useRef,
8+
} from 'react'
29
import PropTypes from 'prop-types'
310
import classNames from 'classnames'
411

@@ -68,7 +75,7 @@ interface DataType {
6875
timeout?: null | ReturnType<typeof setTimeout>
6976
}
7077

71-
export interface ContextType {
78+
export interface ContextProps {
7279
itemsNumber: number
7380
state: [number | null, number, string?]
7481
animating: boolean
@@ -78,16 +85,7 @@ export interface ContextType {
7885
setState: (a: [number | null, number, string?]) => void
7986
}
8087

81-
//
82-
83-
export const Context = React.createContext<ContextType>({
84-
itemsNumber: 0,
85-
state: [null, 0],
86-
animating: false,
87-
setItemsNumber: (_) => {},
88-
setAnimating: (_) => {},
89-
setState: (_) => {},
90-
})
88+
export const CCarouselContext = createContext({} as ContextProps)
9189

9290
export const CCarousel = forwardRef<HTMLDivElement, CCarouselProps>(
9391
(
@@ -110,7 +108,7 @@ export const CCarousel = forwardRef<HTMLDivElement, CCarouselProps>(
110108
const [itemsNumber, setItemsNumber] = useState<number>(0)
111109
const [animating, setAnimating] = useState<boolean>(false)
112110

113-
let data = useRef<DataType>({}).current
111+
const data = useRef<DataType>({}).current
114112

115113
const cycle = () => {
116114
pause()
@@ -147,7 +145,7 @@ export const CCarousel = forwardRef<HTMLDivElement, CCarouselProps>(
147145

148146
return (
149147
<div className={_className} onMouseEnter={pause} onMouseLeave={cycle} {...rest} ref={ref}>
150-
<Context.Provider
148+
<CCarouselContext.Provider
151149
value={{
152150
state,
153151
setState,
@@ -166,7 +164,7 @@ export const CCarousel = forwardRef<HTMLDivElement, CCarouselProps>(
166164
<CCarouselControl direction="next" />
167165
</>
168166
)}
169-
</Context.Provider>
167+
</CCarouselContext.Provider>
170168
</div>
171169
)
172170
},

src/components/carousel/CCarouselControl.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { forwardRef, HTMLAttributes, useContext } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
4-
import { Context } from './CCarousel'
4+
import { CCarouselContext } from './CCarousel'
55

66
type Direction = 'prev' | 'next'
77

@@ -20,7 +20,7 @@ export interface CCarouselControlProps extends HTMLAttributes<HTMLButtonElement>
2020

2121
export const CCarouselControl = forwardRef<HTMLButtonElement, CCarouselControlProps>(
2222
({ className, children, direction, ...rest }, ref) => {
23-
const { state, setState, itemsNumber, animating } = useContext(Context)
23+
const { state, setState, itemsNumber, animating } = useContext(CCarouselContext)
2424

2525
const onClick = (): void => {
2626
if (animating) {

src/components/carousel/CCarouselIndicators.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { forwardRef, HTMLAttributes, useContext } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
4-
import { Context } from './CCarousel'
4+
import { CCarouselContext } from './CCarousel'
55

66
export interface CCarouselIndicatorsProps extends HTMLAttributes<HTMLOListElement> {
77
/**
@@ -18,7 +18,7 @@ export interface CCarouselIndicatorsProps extends HTMLAttributes<HTMLOListElemen
1818

1919
export const CCarouselIndicators = forwardRef<HTMLOListElement, CCarouselIndicatorsProps>(
2020
({ className, indicatorsClass = 'carousel-indicators' }, ref) => {
21-
const { itemsNumber, state, setState, animating } = useContext(Context)
21+
const { itemsNumber, state, setState, animating } = useContext(CCarouselContext)
2222

2323
const listClasses = classNames(indicatorsClass, className)
2424

@@ -30,7 +30,7 @@ export const CCarouselIndicators = forwardRef<HTMLOListElement, CCarouselIndicat
3030
!animating && key !== state[1] && setState([state[1], key])
3131
}}
3232
className={state[1] === key ? 'active' : ''}
33-
data-coreui-target=''
33+
data-coreui-target=""
3434
/>
3535
)
3636
})

src/components/carousel/CCarouselInner.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { Children, forwardRef, HTMLAttributes, useContext, useEffect } fr
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44

5-
import { Context } from './CCarousel'
5+
import { CCarouselContext } from './CCarousel'
66

77
export interface CCarouselInnerProps extends HTMLAttributes<HTMLDivElement> {
88
/**
@@ -13,7 +13,7 @@ export interface CCarouselInnerProps extends HTMLAttributes<HTMLDivElement> {
1313

1414
export const CCarouselInner = forwardRef<HTMLDivElement, CCarouselInnerProps>(
1515
({ children, className, ...rest }, ref) => {
16-
const { setItemsNumber } = useContext(Context)
16+
const { setItemsNumber } = useContext(CCarouselContext)
1717
const _className = classNames('carousel-inner', className)
1818

1919
useEffect(() => {

src/components/carousel/CCarouselItem.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44
import { CSSTransition } from 'react-transition-group'
55

6-
import { Context } from './CCarousel'
6+
import { CCarouselContext } from './CCarousel'
77
export interface CCarouselItemProps extends HTMLAttributes<HTMLDivElement> {
88
/**
99
* A string of all className you want applied to the base component. [docs]
1010
*/
1111
className?: string
12-
idx: number
12+
/**
13+
* @ignore
14+
*/
15+
idx?: number
1316
}
1417

18+
// eslint-disable-next-line @typescript-eslint/ban-types
1519
const getDirection = (state: object) => {
1620
if (state[2]) {
1721
return state[2] === 'next' ? 'right' : 'left'
@@ -22,9 +26,7 @@ const getDirection = (state: object) => {
2226

2327
export const CCarouselItem = forwardRef<HTMLDivElement, CCarouselItemProps>(
2428
({ children, className, idx, ...rest }, ref) => {
25-
const { animate, state, animating, setAnimating } = useContext(
26-
Context,
27-
)
29+
const { animate, state, animating, setAnimating } = useContext(CCarouselContext)
2830

2931
const [isIn, setIsIn] = useState<boolean>(false)
3032

@@ -44,12 +46,10 @@ export const CCarouselItem = forwardRef<HTMLDivElement, CCarouselItemProps>(
4446
setAnimating(false)
4547
}
4648

47-
4849
useEffect(() => {
4950
setIsIn(state[1] === idx)
5051
}, [state])
5152

52-
5353
if (!animate || state[0] === null) {
5454
const itemClasses = classNames('carousel-item', isIn && 'active', className)
5555
return (
@@ -104,6 +104,7 @@ export const CCarouselItem = forwardRef<HTMLDivElement, CCarouselItemProps>(
104104
CCarouselItem.propTypes = {
105105
children: PropTypes.node,
106106
className: PropTypes.string,
107+
idx: PropTypes.number,
107108
}
108109

109110
CCarouselItem.displayName = 'CCarouselItem'

0 commit comments

Comments
 (0)