Skip to content

Commit adc116d

Browse files
committed
refactor(CAccordion): move the context outside the component
1 parent d8fd24a commit adc116d

File tree

6 files changed

+29
-25
lines changed

6 files changed

+29
-25
lines changed

packages/coreui-react/src/components/accordion/CAccordion.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import React, { createContext, forwardRef, HTMLAttributes, useState } from 'react'
1+
import React, { forwardRef, HTMLAttributes, useState } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44

5+
import { CAccordionContext } from './CAccordionContext'
6+
57
export interface CAccordionProps extends HTMLAttributes<HTMLDivElement> {
68
/**
79
* The active item key.
@@ -21,14 +23,6 @@ export interface CAccordionProps extends HTMLAttributes<HTMLDivElement> {
2123
flush?: boolean
2224
}
2325

24-
export interface CAccordionContextProps {
25-
_activeItemKey?: number | string
26-
alwaysOpen?: boolean
27-
setActiveKey: React.Dispatch<React.SetStateAction<number | string | undefined>>
28-
}
29-
30-
export const CAccordionContext = createContext({} as CAccordionContextProps)
31-
3226
export const CAccordion = forwardRef<HTMLDivElement, CAccordionProps>(
3327
({ children, activeItemKey, alwaysOpen = false, className, flush, ...rest }, ref) => {
3428
const [_activeItemKey, setActiveKey] = useState(activeItemKey)
@@ -48,8 +42,8 @@ export const CAccordion = forwardRef<HTMLDivElement, CAccordionProps>(
4842
)
4943

5044
CAccordion.propTypes = {
51-
alwaysOpen: PropTypes.bool,
5245
activeItemKey: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
46+
alwaysOpen: PropTypes.bool,
5347
children: PropTypes.node,
5448
className: PropTypes.string,
5549
flush: PropTypes.bool,

packages/coreui-react/src/components/accordion/CAccordionBody.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import React, { forwardRef, HTMLAttributes, useContext } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44

5-
import { CAccordionItemContext } from './CAccordionItem'
6-
75
import { CCollapse } from './../collapse/CCollapse'
6+
import { CAccordionItemContext } from './CAccordionItemContext'
87

98
export interface CAccordionBodyProps extends HTMLAttributes<HTMLDivElement> {
109
/**

packages/coreui-react/src/components/accordion/CAccordionButton.tsx

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

5-
import { CAccordionItemContext } from './CAccordionItem'
5+
import { CAccordionItemContext } from './CAccordionItemContext'
66

77
export interface CAccordionButtonProps extends HTMLAttributes<HTMLButtonElement> {
88
/**
@@ -28,7 +28,7 @@ export const CAccordionButton = forwardRef<HTMLButtonElement, CAccordionButtonPr
2828
{children}
2929
</button>
3030
)
31-
},
31+
}
3232
)
3333

3434
CAccordionButton.propTypes = {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createContext } from 'react'
2+
3+
export interface CAccordionContextProps {
4+
_activeItemKey?: number | string
5+
alwaysOpen?: boolean
6+
setActiveKey: React.Dispatch<React.SetStateAction<number | string | undefined>>
7+
}
8+
9+
export const CAccordionContext = createContext({} as CAccordionContextProps)

packages/coreui-react/src/components/accordion/CAccordionItem.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, {
2-
createContext,
32
forwardRef,
43
HTMLAttributes,
54
useContext,
@@ -11,15 +10,8 @@ import React, {
1110
import PropTypes from 'prop-types'
1211
import classNames from 'classnames'
1312

14-
import { CAccordionContext } from './CAccordion'
15-
16-
export interface CAccordionItemContextProps {
17-
id: string
18-
setVisible: (a: boolean) => void
19-
visible?: boolean
20-
}
21-
22-
export const CAccordionItemContext = createContext({} as CAccordionItemContextProps)
13+
import { CAccordionContext } from './CAccordionContext'
14+
import { CAccordionItemContext } from './CAccordionItemContext'
2315

2416
export interface CAccordionItemProps extends HTMLAttributes<HTMLDivElement> {
2517
/**
@@ -38,7 +30,8 @@ export interface CAccordionItemProps extends HTMLAttributes<HTMLDivElement> {
3830

3931
export const CAccordionItem = forwardRef<HTMLDivElement, CAccordionItemProps>(
4032
({ children, className, id, itemKey, ...rest }, ref) => {
41-
const _id = id ?? useId()
33+
const uniqueId = useId()
34+
const _id = id ?? uniqueId
4235
const _itemKey = useRef(itemKey ?? _id)
4336

4437
const { _activeItemKey, alwaysOpen, setActiveKey } = useContext(CAccordionContext)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createContext } from 'react'
2+
3+
export interface CAccordionItemContextProps {
4+
id: string
5+
setVisible: (a: boolean) => void
6+
visible?: boolean
7+
}
8+
9+
export const CAccordionItemContext = createContext({} as CAccordionItemContextProps)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy