Skip to content
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

chore(Dropdown): ts & docs &test #4684

Closed

Conversation

woshilaoge
Copy link

No description provided.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这是您为 Fusion/Next 提的第一个 pr,感谢您为 Fusion 做出的贡献,我们会尽快进行处理。

@woshilaoge woshilaoge changed the title chore(Typography): 「Dropdown」 ts & docs &test chore(Technical upgrade): 「Dropdown」 ts & docs &test Dec 27, 2023
@YSMJ1994 YSMJ1994 changed the title chore(Technical upgrade): 「Dropdown」 ts & docs &test chore(Dropdown): ts & docs &test Dec 27, 2023
@woshilaoge woshilaoge requested a review from YSMJ1994 December 27, 2023 08:02
@@ -1,3 +1,5 @@
/* eslint-disable */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can not do this...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done + 1


describe('Dropdown A11y', () => {
it('should not have any violations', async () => {
portalContainer = createContainer(portalContainerId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应使用locally variable定义

</Dropdown>
);
// eslint-disable-next-line
// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't do this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done+1

it('should not have any violations', async () => {
portalContainer = createContainer(portalContainerId);
await testReact(
// eslint-disable-next-line jsx-a11y/anchor-is-valid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我来禁用测试用例文件的 jsx-a11y/anchor-is-valid ,你这里可以移除

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done+1

@@ -182,8 +154,14 @@ describe('Dropdown', () => {
const mountNode = document.createElement('div');
document.body.appendChild(mountNode);

// eslint-disable-next-line react/no-deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该使用cypress 书写用例

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cy.document().then(document => {
// 创建新的 div 元素
const mountNode = document.createElement('div');
// 添加该 div 到 body 中
document.body.appendChild(mountNode);

@@ -10,7 +13,10 @@ const Popup = Overlay.Popup;
* Dropdown
* @description 继承 Popup 的 API,除非特别说明
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use @remarks instead of @description

const child = Children.only(this.props.children) as React.ReactPortal;
let content = child as React.ReactChild;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (typeof child.type === 'function' && child.type.isNextMenu) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use "as" instead of ignore comment

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

child.type as unknown as ReactElementNextMenuType).isNextMenu

@@ -1,198 +1,249 @@
/// <reference types="react" />

/* eslint-disable tsdoc/syntax */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -1,198 +1,249 @@
/// <reference types="react" />

/* eslint-disable tsdoc/syntax */
import * as React from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api类型定义中有大量未知的类型,需要根据代码补全相关类型

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

*/
followTrigger?: boolean;
}

export default class Dropdown extends React.Component<DropdownProps, any> {}
export default class Dropdown extends React.Component<DropdownProps, unknown> {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个组件类型导出可以移除了

@@ -98,7 +108,7 @@ export default class Dropdown extends Component {
onPosition: noop,
};

constructor(props) {
constructor(props: DropdownProps | Readonly<DropdownProps>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只需要 DropdownProps 就可以了

@@ -109,8 +119,8 @@ export default class Dropdown extends Component {
bindCtx(this, ['onTriggerKeyDown', 'onMenuClick', 'onVisibleChange']);
}

static getDerivedStateFromProps(nextProps) {
const state = {};
static getDerivedStateFromProps(nextProps: { visible: unknown }) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里类型沿用 DropdownProps 和 DropdownState

export default class Dropdown extends Component {
export default class Dropdown extends Component<
DropdownProps,
{ visible: unknown; autoFocus: boolean | undefined }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个State类型下面会有复用,可以在 types.ts 里定义 DropdownState 类型。

visible类型应该为 boolean | undefined

}

onTriggerKeyDown() {
let autoFocus = true;

if ('autoFocus' in this.props) {
autoFocus = this.props.autoFocus;
autoFocus = !!this.props.autoFocus;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该是给props.autoFocus添加存在断言,而不是修改实现行, autoFocus = this.props.autoFocus!

用户可能传递了 autoFocus,但传递了一个 undefined,原来的行为会设置 state.autoFocus = undefined ,并向Popup 组件传递 autoFocus = undefined ,这样就会应用 Popup 的默认 autoFocus 处理,你这里直接转成 boolean 值,改变了原有的行为

let content = child;
if (typeof child.type === 'function' && child.type.isNextMenu) {
const { rtl, autoClose } = this.props;
const trigger = this.props.trigger as React.ReactPortal;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从下面推断出,trigger 类型必须是 React.cloneElement 允许的类型,且需要拥有 onKeyDown prop,查看React.cloneElement 的第一个参数类型是 ReactElement ,所以你这里需要将 props.trigger 类型修正为 ReactElement<{ onKeyDown?: (...args: unknown[]) => unknown }>

@@ -177,7 +191,7 @@ export default class Dropdown extends Component {
rtl={rtl}
autoFocus={this.state.autoFocus}
trigger={newTrigger}
visible={this.getVisible()}
visible={this.getVisible() as boolean}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该调整的是 this.getVisible 的返回值类型,如果你正确的定义了State.visible类型,这里应该不需要做任何处理

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里面其实有大量的属性来自 overlay.PopupProps,除了需要输出文档的属性,其它来自PopupProps的属性应该使用继承的方式添加在 DropdownProps 里面。
参考:
import {PopupProps} from '../overlay';
interface DropdownProps extends React.HTMLAttributes, CommonProps, PopupProps {}

然后把 skip 且属于 PopupProps 的属性都移除

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -179,36 +151,45 @@ describe('Dropdown', () => {
// });

it('autoFocus=false should not have any activeElement', done => {
const mountNode = document.createElement('div');
document.body.appendChild(mountNode);
cy.document().then(document => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为何不用 cy.mount

@@ -1,7 +1,10 @@
import React, { Component, Children } from 'react';
import PropTypes from 'prop-types';
/* eslint-disable tsdoc/syntax */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里忘记移除了

@woshilaoge woshilaoge force-pushed the chore/xiaoxian-dropdown branch from a864802 to 2747890 Compare January 4, 2024 02:11
@woshilaoge woshilaoge closed this Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

3 participants