Skip to content

Commit

Permalink
refactor: Remove all coupling code (#218)
Browse files Browse the repository at this point in the history
* chore: bump rc-trigger

* refactor: clean up content

* test: test fix
  • Loading branch information
zombieJ authored May 30, 2022
1 parent aee201b commit a0d1785
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 43 deletions.
2 changes: 1 addition & 1 deletion assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
display: none;
}

&-menu {
.rc-menu {
outline: none;
position: relative;
list-style-type: none;
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@
"rc-menu": "^9.5.2",
"react": "^16.11.0",
"react-dom": "^16.11.0",
"regenerator-runtime": "^0.13.9",
"typescript": "^4.0.2"
},
"peerDependencies": {
"react": ">=16.11.0",
"react-dom": ">=16.11.0"
},
"dependencies": {
"@babel/runtime": "^7.10.1",
"@babel/runtime": "^7.18.3",
"classnames": "^2.2.6",
"rc-trigger": "^5.0.4",
"rc-trigger": "^5.3.1",
"rc-util": "^5.17.0"
}
}
27 changes: 4 additions & 23 deletions src/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type {
} from 'rc-trigger/lib/interface';
import Placements from './placements';
import useAccessibility from './hooks/useAccessibility';
import { composeRef, supportRef } from 'rc-util/lib/ref';

export interface DropdownProps
extends Pick<
Expand Down Expand Up @@ -70,14 +69,10 @@ function Dropdown(props: DropdownProps, ref) {
const triggerRef = React.useRef(null);
React.useImperativeHandle(ref, () => triggerRef.current);

const menuRef = React.useRef(null);
const menuClassName = `${prefixCls}-menu`;

useAccessibility({
visible: mergedVisible,
setTriggerVisible,
triggerRef,
menuRef,
onVisibleChange: props.onVisibleChange,
autoFocus,
});
Expand All @@ -95,15 +90,12 @@ function Dropdown(props: DropdownProps, ref) {

const onClick = (e) => {
const { onOverlayClick } = props;
const overlayProps = getOverlayElement().props;
setTriggerVisible(false);

console.log('!!!!!!!!!!!!', onOverlayClick);
if (onOverlayClick) {
onOverlayClick(e);
}
if (overlayProps.onClick) {
overlayProps.onClick(e);
}
};

const onVisibleChange = (newVisible: boolean) => {
Expand All @@ -116,23 +108,11 @@ function Dropdown(props: DropdownProps, ref) {

const getMenuElement = () => {
const overlayElement = getOverlayElement();
// @ts-ignore
const composedMenuRef = composeRef(menuRef, overlayElement.ref);

const extraOverlayProps = {
prefixCls: menuClassName,
['data-dropdown-inject']: true,
onClick,
ref: supportRef(overlayElement) ? composedMenuRef : undefined,
};
if (typeof overlayElement.type === 'string') {
delete extraOverlayProps.prefixCls;
delete extraOverlayProps['data-dropdown-inject'];
}

return (
<>
{arrow && <div className={`${prefixCls}-arrow`} />}
{React.cloneElement(overlayElement, extraOverlayProps)}
{overlayElement}
</>
);
};
Expand Down Expand Up @@ -199,6 +179,7 @@ function Dropdown(props: DropdownProps, ref) {
stretch={getMinOverlayWidthMatchTrigger() ? 'minWidth' : ''}
popup={getMenuElementOrLambda()}
onPopupVisibleChange={onVisibleChange}
onPopupClick={onClick}
getPopupContainer={getPopupContainer}
>
{renderChildren()}
Expand Down
25 changes: 18 additions & 7 deletions src/hooks/useAccessibility.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as React from 'react';
import KeyCode from 'rc-util/lib/KeyCode';
import raf from 'rc-util/lib/raf';
import { getFocusNodeList } from 'rc-util/lib/Dom/focus';

const { ESC, TAB } = KeyCode;

interface UseAccessibilityProps {
visible: boolean;
setTriggerVisible: (visible: boolean) => void;
triggerRef: React.RefObject<any>;
menuRef: React.RefObject<HTMLUListElement>;
onVisibleChange?: (visible: boolean) => void;
autoFocus?: boolean;
}
Expand All @@ -17,7 +17,6 @@ export default function useAccessibility({
visible,
setTriggerVisible,
triggerRef,
menuRef,
onVisibleChange,
autoFocus,
}: UseAccessibilityProps) {
Expand All @@ -34,23 +33,35 @@ export default function useAccessibility({
};

const focusMenu = () => {
menuRef.current?.focus?.();
focusMenuRef.current = true;
const elements = getFocusNodeList(triggerRef.current?.popupRef?.current?.getElement?.());
const firstElement = elements[0];

if (firstElement?.focus) {
firstElement.focus();
focusMenuRef.current = true;
return true;
}
return false;
};

const handleKeyDown = (event) => {
switch (event.keyCode) {
case ESC:
handleCloseMenuAndReturnFocus();
break;
case TAB:
if (!focusMenuRef.current && menuRef.current?.focus) {
case TAB: {
let focusResult: boolean = false;
if (!focusMenuRef.current) {
focusResult = focusMenu();
}

if (focusResult) {
event.preventDefault();
focusMenu();
} else {
handleCloseMenuAndReturnFocus();
}
break;
}
}
};

Expand Down
9 changes: 4 additions & 5 deletions tests/__snapshots__/basic.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ Array [
style="opacity: 0;"
>
<ul
class="rc-dropdown-menu rc-dropdown-menu-root rc-dropdown-menu-vertical"
data-dropdown-inject="true"
class="rc-menu rc-menu-root rc-menu-vertical"
data-menu-list="true"
role="menu"
style="width: 140px;"
tabindex="0"
>
<li
class="rc-dropdown-menu-item"
class="rc-menu-item"
data-menu-id="rc-menu-uuid-test-1"
role="menuitem"
tabindex="-1"
Expand All @@ -33,10 +32,10 @@ Array [
</span>
</li>
<li
class="rc-dropdown-menu-item-divider"
class="rc-menu-item-divider"
/>
<li
class="rc-dropdown-menu-item"
class="rc-menu-item"
data-menu-id="rc-menu-uuid-test-2"
role="menuitem"
tabindex="-1"
Expand Down
7 changes: 2 additions & 5 deletions tests/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,12 @@ describe('dropdown', () => {

it('simply works', async () => {
let clicked;
let overlayClicked;

function onClick({ key }) {
clicked = key;
}

function onOverlayClick({ key }) {
overlayClicked = key;
}
const onOverlayClick = jest.fn();

const menu = (
<Menu style={{ width: 140 }} onClick={onClick}>
Expand All @@ -100,7 +97,7 @@ describe('dropdown', () => {

dropdown.find('.my-menuitem').simulate('click');
expect(clicked).toBe('1');
expect(overlayClicked).toBe('1');
expect(onOverlayClick).toHaveBeenCalled();
expect(getPopupDomNode(dropdown).classList.contains('rc-dropdown-hidden')).toBe(true);
});

Expand Down
2 changes: 2 additions & 0 deletions tests/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ global.requestAnimationFrame =
return setTimeout(cb, 0);
};

require('regenerator-runtime/runtime');

const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');

Expand Down

0 comments on commit a0d1785

Please sign in to comment.