Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const base = require('@umijs/fabric/dist/eslint');
const base = require("@umijs/fabric/dist/eslint");

module.exports = {
...base,
rules: {
...base.rules,
'no-template-curly-in-string': 0,
'prefer-promise-reject-errors': 0,
'react/no-array-index-key': 0,
'react/sort-comp': 0,
'@typescript-eslint/no-explicit-any': 0,
'jsx-a11y/label-has-associated-control': 0,
'jsx-a11y/label-has-for': 0,
'no-shadow': 0
},
"arrow-parens": 0,
"no-template-curly-in-string": 0,
"prefer-promise-reject-errors": 0,
"react/no-array-index-key": 0,
"react/sort-comp": 0,
"@typescript-eslint/no-explicit-any": 0,
"jsx-a11y/label-has-associated-control": 0,
"jsx-a11y/label-has-for": 0,
"no-shadow": 0
}
};
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"trailingComma": "all",
"proseWrap": "never",
"printWidth": 100
}
89 changes: 60 additions & 29 deletions src/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import classNames from 'classnames';
import { AnimationType, AlignType, BuildInPlacements, ActionType } from 'rc-trigger/lib/interface';
import Placements from './placements';

// Used for accessibility
let uuid = 0;

export interface DropdownProps extends Pick<TriggerProps, 'getPopupContainer' | 'children'> {
minOverlayWidthMatchTrigger?: boolean;
arrow?: boolean;
Expand All @@ -30,6 +33,7 @@ function Dropdown(props: DropdownProps, ref) {
const {
arrow = false,
prefixCls = 'rc-dropdown',
children,
transitionName,
animation,
align,
Expand Down Expand Up @@ -83,10 +87,63 @@ function Dropdown(props: DropdownProps, ref) {
}
};

const getMinOverlayWidthMatchTrigger = () => {
const { minOverlayWidthMatchTrigger, alignPoint } = props;
if ('minOverlayWidthMatchTrigger' in props) {
return minOverlayWidthMatchTrigger;
}

return !alignPoint;
};

const getOpenClassName = () => {
const { openClassName } = props;
if (openClassName !== undefined) {
return openClassName;
}
return `${prefixCls}-open`;
};

// ======================= Children ========================
let childNode: React.ReactElement = children;
const childrenProps = children.props ? children.props : {};

const [innerId, setInnerId] = React.useState<string>();
const mergedId = childrenProps.id || innerId;
const popupId = `${mergedId}-popup`;

// Dynamic add id if needed
React.useEffect(() => {
setInnerId(`${prefixCls}-${uuid}`);
uuid += 1;
}, []);

if (children) {
const additionalProps: { className?: string } = {};

if (triggerVisible) {
additionalProps.className = classNames(childrenProps.className, getOpenClassName());
}

childNode = React.cloneElement(children, {
'aria-haspopup': 'listbox',
'aria-controls': popupId,
'aria-expanded': triggerVisible,
...childrenProps,
...additionalProps,
id: mergedId,
});
}

// ========================= Popup =========================
const getMenuElement = () => {
const overlayElement = getOverlayElement();
const extraOverlayProps = {
prefixCls: `${prefixCls}-menu`,
id: popupId,
tabIndex: -1,
role: 'listbox',
// 'aria-activedescendant'
onClick,
};
if (typeof overlayElement.type === 'string') {
Expand All @@ -108,34 +165,7 @@ function Dropdown(props: DropdownProps, ref) {
return getMenuElement();
};

const getMinOverlayWidthMatchTrigger = () => {
const { minOverlayWidthMatchTrigger, alignPoint } = props;
if ('minOverlayWidthMatchTrigger' in props) {
return minOverlayWidthMatchTrigger;
}

return !alignPoint;
};

const getOpenClassName = () => {
const { openClassName } = props;
if (openClassName !== undefined) {
return openClassName;
}
return `${prefixCls}-open`;
};

const renderChildren = () => {
const { children } = props;
const childrenProps = children.props ? children.props : {};
const childClassName = classNames(childrenProps.className, getOpenClassName());
return triggerVisible && children
? React.cloneElement(children, {
className: childClassName,
})
: children;
};

// ======================== Trigger ========================
let triggerHideAction = hideAction;
if (!triggerHideAction && trigger.indexOf('contextMenu') !== -1) {
triggerHideAction = ['click'];
Expand All @@ -145,6 +175,7 @@ function Dropdown(props: DropdownProps, ref) {
? `${overlayClassName || ''} ${prefixCls}-show-arrow`
: `${overlayClassName || ''}`;

// ======================== Render =========================
return (
<Trigger
{...otherProps}
Expand All @@ -166,7 +197,7 @@ function Dropdown(props: DropdownProps, ref) {
onPopupVisibleChange={onVisibleChange}
getPopupContainer={getPopupContainer}
>
{renderChildren()}
{childNode}
</Trigger>
);
}
Expand Down