Skip to content

Commit

Permalink
fix(feat/dropdown):补充文件
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-Rainy authored and jianbing.chen committed Dec 18, 2024
1 parent 8ef3632 commit 4f4085c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.formatOnSave": true,
"editor.formatOnSave": true
}
71 changes: 71 additions & 0 deletions packages/materials/Navigation/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { ComponentType } from '@materials/types';
import { forwardRef, useImperativeHandle, useState, useMemo, memo } from 'react';
import { Dropdown, MenuProps } from 'antd';
import * as icons from '@ant-design/icons';

/*泛型只需要定义组件本身用到的属性*/
export interface IConfig {
text: string;
type: 'primary' | 'dashed' | 'link' | 'text' | 'default' | undefined;
placement: 'bottom' | 'bottomLeft' | 'bottomRight' | 'top' | 'topLeft' | 'topRight' | undefined;
itemConfig: any[],
}

type MDropdownRef = {
show: () => void;
hide: () => void;
// enable: () => void,
// disable: () => void,
};

const MDropdown = forwardRef<MDropdownRef, ComponentType<IConfig>>(({ id, type, config, onMenuClick }, ref) => {
const [visible, setVisible] = useState(true);
const [disabled, setDisabled] = useState(false)

useImperativeHandle(ref, () => ({
show() {
setVisible(true);
},
hide() {
setVisible(false);
},
enable() {
setDisabled(false);
},
disable() {
setDisabled(true);
},
}));

const items = useMemo(() => {
const iconsList: { [key: string]: any } = icons;
return config.props.itemConfig.map((item: any) => ({ ...item, icon: item.icon && iconsList[item.icon].render() }))
}, [config.props.itemConfig])

const handleMenuClick: MenuProps['onClick'] = ({ key }) => onMenuClick?.({
['key']: key
})

if (!visible) {
return null;
}

const { itemConfig, ...rest } = config.props

return (
<Dropdown.Button
style={config.style}
disabled={disabled}
{...rest}
data-id={id}
data-type={type}
menu={{
items,
onClick: handleMenuClick
}}
>
{config.props.text || ''}
</Dropdown.Button>
);
});
export default memo(MDropdown);

0 comments on commit 4f4085c

Please sign in to comment.