Skip to content

Commit 703ca12

Browse files
committed
fix: align dropdown docs and open class handling
1 parent 73add1c commit 703ca12

5 files changed

Lines changed: 38 additions & 16 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ pnpm-lock.yaml
3636
.dumi/tmp-test
3737
.dumi/tmp-production
3838
.docs
39+
docs-dist

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Additional props are passed to the underlying [`@rc-component/trigger`](https://
7373
| --- | --- | --- | --- |
7474
| alignPoint | Align popup to the click point | boolean | false |
7575
| animation | Popup animation name | string | - |
76-
| defaultVisible | Initial visible state | boolean | - |
76+
| arrow | Whether to show dropdown arrow | boolean | false |
7777
| getPopupContainer | Container where dropdown is rendered | `(node: HTMLElement) => HTMLElement` | `() => document.body` |
7878
| minOverlayWidthMatchTrigger | Whether overlay width should be at least trigger width | boolean | `true` unless `alignPoint` is set |
7979
| openClassName | Class name added to trigger when dropdown is open | string | `${prefixCls}-open` |
@@ -85,7 +85,7 @@ Additional props are passed to the underlying [`@rc-component/trigger`](https://
8585
| transitionName | Popup transition class name | string | - |
8686
| trigger | Trigger action | `ActionType \| ActionType[]` | `['hover']` |
8787
| visible | Controlled visible state | boolean | - |
88-
| onOverlayClick | Callback when overlay is clicked | `(event: React.MouseEvent) => void` | - |
88+
| onOverlayClick | Callback when overlay is clicked | `(event: Event) => void` | - |
8989
| onVisibleChange | Callback when visibility changes | `(visible: boolean) => void` | - |
9090

9191
## Development

README.zh-CN.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
## 特性
3030

31-
- Built on `@rc-component/trigger`.
31+
- 基于 `@rc-component/trigger` 构建。
3232
- 支持悬停、单击、上下文菜单和自定义触发操作。
3333
- 接受 React 元素或渲染函数作为下拉覆盖。
3434
- 支持对齐点行为和触发宽度匹配。
@@ -72,20 +72,20 @@ npm start
7272
| 参数 | 说明 | 类型 | 默认值 |
7373
| --- | --- | --- | --- |
7474
| alignPoint | 将弹层窗口与点击点对齐 | boolean | false |
75-
| 动画片 | 弹层动画名称 | string | - |
76-
| defaultVisible | 初始可见状态 | boolean | - |
75+
| animation | 弹层动画名称 | string | - |
76+
| arrow | 是否显示下拉箭头 | boolean | false |
7777
| getPopupContainer | 呈现下拉列表的容器 | `(node: HTMLElement) => HTMLElement` | `() => document.body` |
7878
| minOverlayWidthMatchTrigger | 覆盖宽度是否至少应为触发宽度 | boolean | `true` 除非设置了 `alignPoint` |
79-
| openClassName | 打开下拉菜单时添加到触发器的className称 | string | `${prefixCls}-open` |
80-
| overlay | Dropdown overlay | `React.ReactElement \| (() => React.ReactElement)` | - |
81-
| overlayClassName | 附加覆盖className称 | string | - |
79+
| openClassName | 打开下拉菜单时添加到触发器的类名 | string | `${prefixCls}-open` |
80+
| overlay | 下拉菜单内容 | `React.ReactElement \| (() => React.ReactElement)` | - |
81+
| overlayClassName | 附加弹层类名 | string | - |
8282
| overlayStyle | 叠加样式 | `React.CSSProperties` | - |
83-
| placement | Dropdown placement | string | `bottomLeft` |
84-
| prefixCls | 组件className前缀 | string | `rc-dropdown` |
85-
| transitionName | 弹层过渡className称 | string | - |
86-
| 扳机 | 触发动作 | `ActionType \| ActionType[]` | `['hover']` |
87-
| 可见的 | 受控可见状态 | boolean | - |
88-
| onOverlayClick | 点击覆盖时的回调 | `(event: React.MouseEvent) => void` | - |
83+
| placement | 下拉菜单位置 | string | `bottomLeft` |
84+
| prefixCls | 组件类名前缀 | string | `rc-dropdown` |
85+
| transitionName | 弹层过渡类名 | string | - |
86+
| trigger | 触发动作 | `ActionType \| ActionType[]` | `['hover']` |
87+
| visible | 受控可见状态 | boolean | - |
88+
| onOverlayClick | 点击下拉菜单内容时的回调 | `(event: Event) => void` | - |
8989
| onVisibleChange | 可见性变化时的回调 | `(visible: boolean) => void` | - |
9090

9191
## 本地开发

src/Dropdown.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ const Dropdown = React.forwardRef<TriggerRef, DropdownProps>((props, ref) => {
146146
className: childClassName,
147147
ref: composeRef(childRef, elementChild && getNodeRef(elementChild)),
148148
};
149+
const fallbackChild = elementChild
150+
? React.cloneElement(
151+
elementChild as React.ReactElement<React.HTMLAttributes<HTMLElement>>,
152+
{
153+
className: childClassName,
154+
},
155+
)
156+
: child;
149157

150158
const childrenNode =
151159
elementChild && supportRef(elementChild) ? (
@@ -156,8 +164,8 @@ const Dropdown = React.forwardRef<TriggerRef, DropdownProps>((props, ref) => {
156164
triggerChildProps,
157165
)
158166
) : (
159-
<span className={childClassName} ref={childRef}>
160-
{child}
167+
<span className={!elementChild ? childClassName : undefined} ref={childRef}>
168+
{fallbackChild}
161169
</span>
162170
);
163171

tests/basic.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,19 @@ describe('dropdown', () => {
286286
).toBeFalsy();
287287
});
288288

289+
it('should pass openClassName to child without ref support', () => {
290+
const LegacyButton = (props: HTMLAttributes<HTMLButtonElement>) => (
291+
<button {...props}>open</button>
292+
);
293+
const { container } = render(
294+
<Dropdown overlay={<div style={{ width: 50 }}>Test</div>} visible>
295+
<LegacyButton className="my-button" />
296+
</Dropdown>,
297+
);
298+
299+
expect(container.querySelector('.my-button')).toHaveClass('rc-dropdown-open');
300+
});
301+
289302
it('overlay callback', async () => {
290303
const overlay = <div style={{ width: 50 }}>Test</div>;
291304
const { container, baseElement } = render(

0 commit comments

Comments
 (0)