Skip to content

Commit

Permalink
feat(ui): Action support indirect nesting children
Browse files Browse the repository at this point in the history
  • Loading branch information
dengfuping committed Jan 13, 2025
1 parent bec040e commit ad9bfe7
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 7 deletions.
28 changes: 23 additions & 5 deletions packages/ui/src/Action/Group.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { Button, Dropdown, Menu, Space, Tooltip, Typography } from '@oceanbase/design';
import React, { useContext } from 'react';
import {
Button,
Dropdown,
Menu,
Space,
Tooltip,
Typography,
ConfigProvider,
} from '@oceanbase/design';
import type { ButtonSize } from '@oceanbase/design/es/button';
import { EllipsisOutlined, LoadingOutlined } from '@oceanbase/icons';
import classNames from 'classnames';
import { isBoolean, max, omit } from 'lodash';
import React from 'react';
import type { BaseProps } from './Item';
import useStyle from './style';

export interface GroupProps {
prefixCls?: string;
size?: number;
dropDownPlacement?:
| 'topLeft'
Expand Down Expand Up @@ -38,6 +49,7 @@ const getOrder = ({ type, fixed }: { type?: string; fixed?: boolean }) => {
};

export default ({
prefixCls: customizePrefixCls,
size = 3,
children,
dropDownPlacement,
Expand All @@ -48,6 +60,11 @@ export default ({
moreType,
buttonSize,
}: GroupProps) => {
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);

const prefixCls = getPrefixCls('action', customizePrefixCls);
const { wrapSSR } = useStyle(prefixCls);

const visibleActions = Array.isArray(children)
? children.filter(c => {
if (isBoolean(c.props.visible) && shouldVisible)
Expand Down Expand Up @@ -107,7 +124,7 @@ export default ({
);
}

return (
return wrapSSR(
<Space size={ellipsisType === 'button' ? 8 : 16}>
{mainActions.map(action => {
return React.cloneElement(action, {
Expand All @@ -125,7 +142,7 @@ export default ({
<Dropdown
placement={dropDownPlacement}
overlay={
<Menu>
<Menu className={`${prefixCls}-more-menu`}>
{ellipsisActions.map((action, index) => {
const actionKey = action.key;
let disabled = false;
Expand All @@ -150,7 +167,8 @@ export default ({
disabled={actionDisabled}
>
<Tooltip title={action.props.tooltip}>
{action.props.loading && <LoadingOutlined />} {action.props.children}
{action.props.loading && <LoadingOutlined />}{' '}
{action.props.children || action}
</Tooltip>
</Menu.Item>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/Action/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface BaseProps extends ButtonProps {
/** 固定展示、不会被折叠 */
fixed?: boolean;
onClick?: (e: React.MouseEvent<HTMLElement, MouseEvent>) => Promise<void> | void;
children?: React.ReactElement | string;
children?: React.ReactElement | React.ReactNode | string;
enableLoading?: boolean;
tooltip?: string;
loading?: boolean;
Expand Down
98 changes: 98 additions & 0 deletions packages/ui/src/Action/demo/children-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import React, { useState } from 'react';
import { Action } from '@oceanbase/ui';
import { Drawer, Space } from '@oceanbase/design';

const ActionButton4 = () => {
const [open, setOpen] = useState(false);
return (
<>
<Action.Button
onClick={() => {
setOpen(true);
}}
>
action4
</Action.Button>
<Drawer
open={open}
title="Drawer Title"
onOk={() => {
setOpen(false);
}}
onCancel={() => {
setOpen(false);
}}
>
Drawer Content
</Drawer>
</>
);
};

const ActionButton5 = () => <Action.Button disabled>action5</Action.Button>;

const ActionLink4 = () => {
const [open, setOpen] = useState(false);
return (
<>
<Action.Link
onClick={() => {
setOpen(true);
}}
>
action4
</Action.Link>
<Drawer
open={open}
title="Drawer Title"
onOk={() => {
setOpen(false);
}}
onCancel={() => {
setOpen(false);
}}
>
Drawer Content
</Drawer>
</>
);
};

const ActionLink5 = () => <Action.Link disabled>action5</Action.Link>;

export default () => {
return (
<Space direction="vertical">
<Action.Group>
<Action.Button>action1</Action.Button>
<Action.Button disabled tooltip={'禁用展示tooltip'}>
禁用提示
</Action.Button>
<Action.Button
onClick={() => {
console.log('hello~~');
}}
>
action3
</Action.Button>
<ActionButton4 />
<ActionButton5 />
</Action.Group>
<Action.Group>
<Action.Link>action1</Action.Link>
<Action.Link disabled tooltip={'禁用展示tooltip'}>
禁用提示
</Action.Link>
<Action.Link
onClick={() => {
console.log('hello~~');
}}
>
action3
</Action.Link>
<ActionLink4 />
<ActionLink5 />
</Action.Group>
</Space>
);
};
3 changes: 2 additions & 1 deletion packages/ui/src/Action/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ nav:
<code src="./demo/more-text.tsx" title="自定义更多操作"></code>
<code src="./demo/with-tooltip-popconfirm.tsx" title="带 Tooltip 和 Popconfirm"></code>
<code src="./demo/fixed.tsx" title="固定展示、不被折叠的 Action"></code>
<code src="./demo/groupControl.tsx" title="整体控制状态"></code>
<code src="./demo/group-control.tsx" title="整体控制状态"></code>
<code src="./demo/children-wrapper.tsx" title="子元素间接嵌套"></code>

## API

Expand Down
54 changes: 54 additions & 0 deletions packages/ui/src/Action/style/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { CSSObject } from '@ant-design/cssinjs';
import type { GenerateStyle } from '@oceanbase/design/es/theme';
import { genComponentStyleHook } from '../../_util/genComponentStyleHook';
import type { OBToken } from '../../_util/genComponentStyleHook';

export const genActionStyle: GenerateStyle<OBToken> = (token: OBToken): CSSObject => {
const { componentCls, antCls } = token;
const paddingVertical = (token.controlHeight - token.fontSize * token.lineHeight) / 2;
const paddingHorizontal = token.paddingSM;
const padding = `${paddingVertical}px ${paddingHorizontal}px`;
const margin = `-${paddingVertical}px -${paddingHorizontal}px`;
return {
[`${componentCls}-more-menu`]: {
background: 'red',
[`${antCls}-dropdown-menu-item`]: {
[`${antCls}-typography`]: {
display: 'block',
margin,
padding: `${padding} !important`,
[`&:not(${antCls}-typography-disabled)`]: {
color: token.colorText,
},
},
[`${antCls}-btn`]: {
padding,
margin,
border: 'none',
display: 'block',
width: `calc(100% + ${paddingHorizontal * 2}px)`,
textAlign: 'left',
height: 'inherit',
background: 'transparent',
[`&:not(:disabled):not(${antCls}-btn-disabled)`]: {
color: token.colorText,
[`&:hover`]: {
background: 'transparent',
},
},
// remove button click animation
[`${antCls}-wave`]: {
display: 'none',
},
},
},
},
};
};

export default (prefixCls: string) => {
const useStyle = genComponentStyleHook('Action', token => {
return [genActionStyle(token as OBToken)];
});
return useStyle(prefixCls);
};

0 comments on commit ad9bfe7

Please sign in to comment.