-
Notifications
You must be signed in to change notification settings - Fork 870
[EuiAccordion] Migrate from class to function component #9558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,14 +6,15 @@ | |||||||||||||||||||||||||||||||||
| * Side Public License, v 1. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import React, { Component, HTMLAttributes, ReactNode } from 'react'; | ||||||||||||||||||||||||||||||||||
| import React, { | ||||||||||||||||||||||||||||||||||
| FunctionComponent, | ||||||||||||||||||||||||||||||||||
| HTMLAttributes, | ||||||||||||||||||||||||||||||||||
| ReactNode, | ||||||||||||||||||||||||||||||||||
| useState, | ||||||||||||||||||||||||||||||||||
| } from 'react'; | ||||||||||||||||||||||||||||||||||
| import classNames from 'classnames'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||
| htmlIdGenerator, | ||||||||||||||||||||||||||||||||||
| withEuiTheme, | ||||||||||||||||||||||||||||||||||
| WithEuiThemeProps, | ||||||||||||||||||||||||||||||||||
| } from '../../services'; | ||||||||||||||||||||||||||||||||||
| import { useEuiTheme, useGeneratedHtmlId } from '../../services'; | ||||||||||||||||||||||||||||||||||
| import { CommonProps } from '../common'; | ||||||||||||||||||||||||||||||||||
| import { EuiLoadingSpinner } from '../loading'; | ||||||||||||||||||||||||||||||||||
| import type { EuiButtonIconProps } from '../button'; | ||||||||||||||||||||||||||||||||||
|
|
@@ -114,133 +115,98 @@ export type EuiAccordionProps = CommonProps & | |||||||||||||||||||||||||||||||||
| isDisabled?: boolean; | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| type EuiAccordionState = { | ||||||||||||||||||||||||||||||||||
| isOpen: boolean; | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export class EuiAccordionClass extends Component< | ||||||||||||||||||||||||||||||||||
| WithEuiThemeProps & EuiAccordionProps, | ||||||||||||||||||||||||||||||||||
| EuiAccordionState | ||||||||||||||||||||||||||||||||||
| > { | ||||||||||||||||||||||||||||||||||
| static defaultProps = { | ||||||||||||||||||||||||||||||||||
| initialIsOpen: false, | ||||||||||||||||||||||||||||||||||
| borders: 'none' as const, | ||||||||||||||||||||||||||||||||||
| paddingSize: 'none' as const, | ||||||||||||||||||||||||||||||||||
| arrowDisplay: 'left' as const, | ||||||||||||||||||||||||||||||||||
| isLoading: false, | ||||||||||||||||||||||||||||||||||
| isDisabled: false, | ||||||||||||||||||||||||||||||||||
| isLoadingMessage: false, | ||||||||||||||||||||||||||||||||||
| element: 'div' as const, | ||||||||||||||||||||||||||||||||||
| buttonElement: 'button' as const, | ||||||||||||||||||||||||||||||||||
| role: 'group' as const, | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| state = { | ||||||||||||||||||||||||||||||||||
| isOpen: this.props.forceState | ||||||||||||||||||||||||||||||||||
| ? this.props.forceState === 'open' | ||||||||||||||||||||||||||||||||||
| : this.props.initialIsOpen!, | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| get isOpen() { | ||||||||||||||||||||||||||||||||||
| return this.props.forceState | ||||||||||||||||||||||||||||||||||
| ? this.props.forceState === 'open' | ||||||||||||||||||||||||||||||||||
| : this.state.isOpen; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| onToggle = () => { | ||||||||||||||||||||||||||||||||||
| const { forceState } = this.props; | ||||||||||||||||||||||||||||||||||
| export const EuiAccordionClass: FunctionComponent<EuiAccordionProps> = ({ | ||||||||||||||||||||||||||||||||||
| children, | ||||||||||||||||||||||||||||||||||
| className, | ||||||||||||||||||||||||||||||||||
| id, | ||||||||||||||||||||||||||||||||||
| role = 'group', | ||||||||||||||||||||||||||||||||||
| element: Element = 'div', | ||||||||||||||||||||||||||||||||||
| buttonElement = 'button', | ||||||||||||||||||||||||||||||||||
| buttonProps, | ||||||||||||||||||||||||||||||||||
| buttonClassName, | ||||||||||||||||||||||||||||||||||
| buttonContentClassName, | ||||||||||||||||||||||||||||||||||
| buttonContent, | ||||||||||||||||||||||||||||||||||
| arrowDisplay = 'left', | ||||||||||||||||||||||||||||||||||
| arrowProps, | ||||||||||||||||||||||||||||||||||
| extraAction, | ||||||||||||||||||||||||||||||||||
| paddingSize = 'none', | ||||||||||||||||||||||||||||||||||
| borders = 'none', | ||||||||||||||||||||||||||||||||||
| initialIsOpen = false, | ||||||||||||||||||||||||||||||||||
| forceState, | ||||||||||||||||||||||||||||||||||
| isLoading = false, | ||||||||||||||||||||||||||||||||||
| isLoadingMessage = false, | ||||||||||||||||||||||||||||||||||
| isDisabled = false, | ||||||||||||||||||||||||||||||||||
| onToggle, | ||||||||||||||||||||||||||||||||||
| ...rest | ||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||
| const [isOpenState, setIsOpenState] = useState( | ||||||||||||||||||||||||||||||||||
| forceState ? forceState === 'open' : initialIsOpen | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const isOpen = forceState ? forceState === 'open' : isOpenState; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const onAccordionToggle = () => { | ||||||||||||||||||||||||||||||||||
| if (forceState) { | ||||||||||||||||||||||||||||||||||
| const nextState = !this.isOpen; | ||||||||||||||||||||||||||||||||||
| this.props.onToggle?.(nextState); | ||||||||||||||||||||||||||||||||||
| onToggle?.(!isOpen); | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| this.setState( | ||||||||||||||||||||||||||||||||||
| (prevState) => ({ | ||||||||||||||||||||||||||||||||||
| isOpen: !prevState.isOpen, | ||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||
| () => { | ||||||||||||||||||||||||||||||||||
| this.props.onToggle?.(this.state.isOpen); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| setIsOpenState((prevIsOpen) => { | ||||||||||||||||||||||||||||||||||
| const nextState = !prevIsOpen; | ||||||||||||||||||||||||||||||||||
| onToggle?.(nextState); | ||||||||||||||||||||||||||||||||||
| return nextState; | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+152
to
+156
|
||||||||||||||||||||||||||||||||||
| setIsOpenState((prevIsOpen) => { | |
| const nextState = !prevIsOpen; | |
| onToggle?.(nextState); | |
| return nextState; | |
| }); | |
| const nextState = !isOpenState; | |
| setIsOpenState(nextState); | |
| onToggle?.(nextState); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's please address this.
Copilot
AI
Apr 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useEuiTheme() returns a theme object (with euiTheme, colorMode, etc.), but the local variable is named euiTheme, which reads like it contains only the euiTheme property. Renaming this variable to theme (or destructuring { euiTheme } and adjusting the style call accordingly) would avoid confusion and reduce the chance of misusing the hook return value later.
| const euiTheme = useEuiTheme(); | |
| const classes = classNames( | |
| 'euiAccordion', | |
| { 'euiAccordion-isOpen': isOpen }, | |
| className | |
| ); | |
| const styles = euiAccordionStyles(euiTheme); | |
| const theme = useEuiTheme(); | |
| const classes = classNames( | |
| 'euiAccordion', | |
| { 'euiAccordion-isOpen': isOpen }, | |
| className | |
| ); | |
| const styles = euiAccordionStyles(theme); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually use euiThemeContext:
const euiThemeContext = useEuiTheme();
const styles = euiAccordionStyles(euiThemeContext);There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was not part of the task but we could use useEuiMemoizedStyles
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
We should export it directly as EuiAccordion, not EuiAccordionClass because that's no longer true.