-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(web-react): Refactor Dropdown to controlled component #DS-637
- Created new component called DropdownModern with new structure - Added deprecation messages to Dropdown and DropdownModern - Added deprecation to `renderTrigger` in Dropdown component - Created new component called UncontrolledDropdown
- Loading branch information
1 parent
7147a6d
commit 3c0f08b
Showing
30 changed files
with
911 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/web-react/src/components/Dropdown/DropdownContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { MutableRefObject, createContext, useContext } from 'react'; | ||
import { Placements } from '../../constants'; | ||
import { ClickEvent, PlacementDictionaryType } from '../../types'; | ||
import { fullWidthModeKeys } from './useDropdownAriaProps'; | ||
|
||
type DropdownContextType = { | ||
dropdownRef: MutableRefObject<HTMLElement | null>; | ||
fullWidthMode?: keyof typeof fullWidthModeKeys; | ||
id: string; | ||
isOpen: boolean; | ||
onToggle: (event: ClickEvent) => void; | ||
placement?: PlacementDictionaryType; | ||
triggerRef: MutableRefObject<HTMLElement | undefined>; | ||
}; | ||
|
||
const defaultContext: DropdownContextType = { | ||
dropdownRef: { current: null }, | ||
fullWidthMode: fullWidthModeKeys.off, | ||
id: '', | ||
isOpen: false, | ||
onToggle: () => {}, | ||
placement: Placements.BOTTOM_LEFT, | ||
triggerRef: { current: undefined }, | ||
}; | ||
|
||
const DropdownContext = createContext<DropdownContextType>(defaultContext); | ||
const DropdownProvider = DropdownContext.Provider; | ||
const DropdownConsumer = DropdownContext.Consumer; | ||
const useDropdownContext = (): DropdownContextType => useContext(DropdownContext); | ||
|
||
export default DropdownContext; | ||
export { DropdownConsumer, DropdownProvider, useDropdownContext }; | ||
export type { DropdownContextType }; |
67 changes: 67 additions & 0 deletions
67
packages/web-react/src/components/Dropdown/DropdownModern.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React, { useRef } from 'react'; | ||
import classNames from 'classnames'; | ||
import { useClickOutside, useDeprecationMessage, useStyleProps } from '../../hooks'; | ||
import { ChildrenProps, SpiritDropdownModernProps } from '../../types'; | ||
import { DropdownProvider } from './DropdownContext'; | ||
import { useDropdownStyleProps } from './useDropdownStyleProps'; | ||
|
||
interface DropdownProps extends ChildrenProps, SpiritDropdownModernProps {} | ||
|
||
export const DropdownModern = (props: DropdownProps) => { | ||
const { | ||
children, | ||
enableAutoClose = true, | ||
fullWidthMode, | ||
id, | ||
isOpen = false, | ||
onAutoClose, | ||
onToggle, | ||
placement, | ||
...rest | ||
} = props; | ||
const { classProps } = useDropdownStyleProps(); | ||
const { styleProps, props: otherProps } = useStyleProps({ ...rest }); | ||
|
||
const dropdownRef = useRef<HTMLDivElement>(null); | ||
const triggerRef = useRef<HTMLDivElement>(); | ||
|
||
const closeHandler = (event: Event) => { | ||
if (!enableAutoClose) { | ||
return; | ||
} | ||
|
||
if (!triggerRef?.current?.contains(event?.target as Node)) { | ||
if (onAutoClose) { | ||
onAutoClose(event); | ||
} | ||
|
||
onToggle && isOpen && onToggle(); | ||
} | ||
}; | ||
|
||
useClickOutside({ ref: dropdownRef, callback: closeHandler }); | ||
|
||
useDeprecationMessage({ | ||
method: 'component', | ||
trigger: true, | ||
componentName: 'DropdownModern', | ||
componentProps: { | ||
newName: 'Dropdown', | ||
}, | ||
}); | ||
|
||
return ( | ||
<DropdownProvider value={{ id, isOpen, fullWidthMode, placement, onToggle, dropdownRef, triggerRef }}> | ||
<div | ||
ref={dropdownRef} | ||
{...styleProps} | ||
{...otherProps} | ||
className={classNames(classProps.wrapperClassName, styleProps.className)} | ||
> | ||
{children} | ||
</div> | ||
</DropdownProvider> | ||
); | ||
}; | ||
|
||
export default DropdownModern; |
29 changes: 29 additions & 0 deletions
29
packages/web-react/src/components/Dropdown/DropdownPopover.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
import { useStyleProps } from '../../hooks'; | ||
import { ChildrenProps, StyleProps } from '../../types'; | ||
import { useDropdownContext } from './DropdownContext'; | ||
import { useDropdownAriaProps } from './useDropdownAriaProps'; | ||
import { useDropdownStyleProps } from './useDropdownStyleProps'; | ||
|
||
interface DropdownPopoverProps extends ChildrenProps, StyleProps {} | ||
|
||
export const DropdownPopover = (props: DropdownPopoverProps) => { | ||
const { children, ...rest } = props; | ||
const { id, isOpen, onToggle, fullWidthMode, placement } = useDropdownContext(); | ||
const { classProps, props: modifiedProps } = useDropdownStyleProps({ isOpen, placement, ...rest }); | ||
const { styleProps: contentStyleProps, props: contentOtherProps } = useStyleProps({ ...modifiedProps }); | ||
const { contentProps } = useDropdownAriaProps({ id, isOpen, toggleHandler: onToggle, fullWidthMode }); | ||
|
||
return ( | ||
<div | ||
className={classNames(classProps.contentClassName, contentStyleProps.className)} | ||
{...contentOtherProps} | ||
{...contentProps} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export default DropdownPopover; |
36 changes: 36 additions & 0 deletions
36
packages/web-react/src/components/Dropdown/DropdownTrigger.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { ElementType } from 'react'; | ||
import { useStyleProps } from '../../hooks'; | ||
import { StyleProps } from '../../types'; | ||
import { useDropdownContext } from './DropdownContext'; | ||
import { useDropdownAriaProps } from './useDropdownAriaProps'; | ||
import { useDropdownStyleProps } from './useDropdownStyleProps'; | ||
|
||
interface DropdownTriggerProps extends StyleProps { | ||
elementType?: ElementType | string; | ||
children: string | React.ReactNode | ((props: { isOpen: boolean }) => React.ReactNode); | ||
} | ||
|
||
export const DropdownTrigger = (props: DropdownTriggerProps) => { | ||
const { elementType = 'button', children, ...rest } = props; | ||
const { id, isOpen, onToggle, fullWidthMode, triggerRef } = useDropdownContext(); | ||
|
||
const Component = elementType; | ||
|
||
const { classProps } = useDropdownStyleProps({ isOpen, ...rest }); | ||
const { styleProps: triggerStyleProps } = useStyleProps({ | ||
UNSAFE_className: classProps.triggerClassName, | ||
}); | ||
const { triggerProps } = useDropdownAriaProps({ id, isOpen, toggleHandler: onToggle, fullWidthMode }); | ||
|
||
return ( | ||
<Component id={id} {...rest} ref={triggerRef} {...triggerStyleProps} {...triggerProps}> | ||
{typeof children === 'function' ? children({ isOpen }) : children} | ||
</Component> | ||
); | ||
}; | ||
|
||
DropdownTrigger.defaultProps = { | ||
elementType: 'button', | ||
}; | ||
|
||
export default DropdownTrigger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/web-react/src/components/Dropdown/UncontrolledDropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { useRef } from 'react'; | ||
import classNames from 'classnames'; | ||
import { useStyleProps } from '../../hooks'; | ||
import { UncontrolledDropdownProps } from '../../types'; | ||
import { DropdownProvider } from './DropdownContext'; | ||
import { useDropdownStyleProps } from './useDropdownStyleProps'; | ||
import { useDropdown } from './useDropdown'; | ||
|
||
export const UncontrolledDropdown = (props: UncontrolledDropdownProps) => { | ||
const { children, enableAutoClose = true, fullWidthMode, id, onAutoClose, placement, ...rest } = props; | ||
const { classProps } = useDropdownStyleProps(); | ||
const { styleProps, props: otherProps } = useStyleProps({ ...rest }); | ||
|
||
const dropdownRef = useRef<HTMLDivElement>(null); | ||
const triggerRef = useRef<HTMLDivElement>(); | ||
|
||
const { isOpen, toggleHandler: onToggle } = useDropdown({ dropdownRef, triggerRef, enableAutoClose, onAutoClose }); | ||
|
||
return ( | ||
<DropdownProvider value={{ id, isOpen, fullWidthMode, placement, onToggle, dropdownRef, triggerRef }}> | ||
<div | ||
ref={dropdownRef} | ||
{...styleProps} | ||
{...otherProps} | ||
className={classNames(classProps.wrapperClassName, styleProps.className)} | ||
> | ||
{children} | ||
</div> | ||
</DropdownProvider> | ||
); | ||
}; | ||
|
||
export default UncontrolledDropdown; |
Oops, something went wrong.