Skip to content

Commit

Permalink
refactor(ui): add DCardActions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: adjust api of card's actions.
  • Loading branch information
xiejay97 committed Mar 14, 2023
1 parent b83a36b commit 01176c2
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 64 deletions.
16 changes: 3 additions & 13 deletions packages/ui/src/components/card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { getClassName } from '@react-devui/utils';

import { registerComponentMate } from '../../utils';
import { useComponentConfig, usePrefixConfig } from '../root';
import { DSeparator } from '../separator';
import { DCardAction } from './CardAction';
import { DCardActions } from './CardActions';
import { DCardContent } from './CardContent';
import { DCardHeader } from './CardHeader';

export interface DCardProps extends React.HTMLAttributes<HTMLDivElement> {
dBorder?: boolean;
dShadow?: boolean | 'hover';
dActions?: React.ReactNode[];
}

const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DCard' as const });
export const DCard: {
(props: DCardProps): JSX.Element | null;
Actions: typeof DCardActions;
Action: typeof DCardAction;
Header: typeof DCardHeader;
Content: typeof DCardContent;
Expand All @@ -26,7 +26,6 @@ export const DCard: {
children,
dBorder = true,
dShadow = false,
dActions,

...restProps
} = useComponentConfig(COMPONENT_NAME, props);
Expand All @@ -45,20 +44,11 @@ export const DCard: {
})}
>
{children}
{dActions && (
<div className={`${dPrefix}card__actions`}>
{React.Children.map(dActions, (action, index) => (
<>
{action}
{index !== dActions.length - 1 && <DSeparator style={{ margin: 8 }} dVertical></DSeparator>}
</>
))}
</div>
)}
</div>
);
};

DCard.Actions = DCardActions;
DCard.Action = DCardAction;
DCard.Header = DCardHeader;
DCard.Content = DCardContent;
11 changes: 6 additions & 5 deletions packages/ui/src/components/card/CardAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { getClassName } from '@react-devui/utils';
import { registerComponentMate } from '../../utils';
import { useComponentConfig, usePrefixConfig } from '../root';

export type DCardActionProps = React.ButtonHTMLAttributes<HTMLButtonElement>;
export type DCardActionProps = React.ButtonHTMLAttributes<HTMLDivElement>;

const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DCard.Action' as const });
function CardAction(props: DCardActionProps, ref: React.ForwardedRef<HTMLButtonElement>): JSX.Element | null {
function CardAction(props: DCardActionProps, ref: React.ForwardedRef<HTMLDivElement>): JSX.Element | null {
const {
children,

Expand All @@ -20,14 +20,15 @@ function CardAction(props: DCardActionProps, ref: React.ForwardedRef<HTMLButtonE
//#endregion

return (
<button
<div
{...restProps}
ref={ref}
className={getClassName(restProps.className, `${dPrefix}card__action`)}
type={restProps['type'] ?? 'button'}
role={restProps['role'] ?? 'button'}
tabIndex={restProps['tabIndex'] ?? 0}
>
{children}
</button>
</div>
);
}

Expand Down
35 changes: 35 additions & 0 deletions packages/ui/src/components/card/CardActions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';

import { getClassName } from '@react-devui/utils';

import { registerComponentMate } from '../../utils';
import { useComponentConfig, usePrefixConfig } from '../root';
import { DSeparator } from '../separator';

export interface DCardActionsProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> {
dActions: React.ReactNode[];
}

const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DCard.Actions' as const });
export function DCardActions(props: DCardActionsProps): JSX.Element | null {
const {
dActions,

...restProps
} = useComponentConfig(COMPONENT_NAME, props);

//#region Context
const dPrefix = usePrefixConfig();
//#endregion

return (
<div {...restProps} className={getClassName(restProps.className, `${dPrefix}card__actions`)}>
{React.Children.map(dActions, (action, index) => (
<>
{action}
{index !== dActions.length - 1 && <DSeparator style={{ margin: 8 }} dVertical></DSeparator>}
</>
))}
</div>
);
}
28 changes: 14 additions & 14 deletions packages/ui/src/components/card/demos/1.Basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,7 @@ import { DCard, DButton } from '@react-devui/ui';

export default function Demo() {
return (
<DCard
style={{ width: 300 }}
dActions={[
<DCard.Action title="edit">
<EditOutlined />
</DCard.Action>,
<DCard.Action title="delete">
<DeleteOutlined />
</DCard.Action>,
<DCard.Action title="more">
<EllipsisOutlined />
</DCard.Action>,
]}
>
<DCard style={{ width: 300 }}>
<DCard.Header dAction={<DButton dType="link">More</DButton>}>Card title</DCard.Header>
<DCard.Content>
<div className="app-demo-text-container">
Expand All @@ -40,6 +27,19 @@ export default function Demo() {
<div>Some contents...</div>
</div>
</DCard.Content>
<DCard.Actions
dActions={[
<DCard.Action title="edit">
<EditOutlined />
</DCard.Action>,
<DCard.Action title="delete">
<DeleteOutlined />
</DCard.Action>,
<DCard.Action title="more">
<EllipsisOutlined />
</DCard.Action>,
]}
></DCard.Actions>
</DCard>
);
}
Expand Down
30 changes: 14 additions & 16 deletions packages/ui/src/components/card/demos/2.Style.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,27 @@ export default function Demo() {
onModelChange={setShadow}
/>
<br />
<DCard
style={{ width: 300 }}
dBorder={border}
dShadow={shadow}
dActions={[
<DCard.Action title="edit">
<EditOutlined />
</DCard.Action>,
<DCard.Action title="delete">
<DeleteOutlined />
</DCard.Action>,
<DCard.Action title="more">
<EllipsisOutlined />
</DCard.Action>,
]}
>
<DCard style={{ width: 300 }} dBorder={border} dShadow={shadow}>
<DCard.Content>
<div className="app-demo-text-container">
<div>Some contents...</div>
<div>Some contents...</div>
<div>Some contents...</div>
</div>
</DCard.Content>
<DCard.Actions
dActions={[
<DCard.Action title="edit">
<EditOutlined />
</DCard.Action>,
<DCard.Action title="delete">
<DeleteOutlined />
</DCard.Action>,
<DCard.Action title="more">
<EllipsisOutlined />
</DCard.Action>,
]}
></DCard.Actions>
</DCard>
</>
);
Expand Down
28 changes: 14 additions & 14 deletions packages/ui/src/components/card/demos/3.Media.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,7 @@ import { DCard } from '@react-devui/ui';

export default function Demo() {
return (
<DCard
style={{ width: 300 }}
dActions={[
<DCard.Action title="edit">
<EditOutlined />
</DCard.Action>,
<DCard.Action title="delete">
<DeleteOutlined />
</DCard.Action>,
<DCard.Action title="more">
<EllipsisOutlined />
</DCard.Action>,
]}
>
<DCard style={{ width: 300 }}>
<div style={{ margin: '-1px -1px 0 -1px' }}>
<img
style={{ width: '100%', borderRadius: 'var(--rd-border-radius) var(--rd-border-radius) 0 0' }}
Expand All @@ -46,6 +33,19 @@ export default function Demo() {
<div>Some contents...</div>
</div>
</DCard.Content>
<DCard.Actions
dActions={[
<DCard.Action title="edit">
<EditOutlined />
</DCard.Action>,
<DCard.Action title="delete">
<DeleteOutlined />
</DCard.Action>,
<DCard.Action title="more">
<EllipsisOutlined />
</DCard.Action>,
]}
></DCard.Actions>
</DCard>
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/card/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './Card';
export * from './CardActions';
export * from './CardAction';
export * from './CardHeader';
export * from './CardContent';
2 changes: 1 addition & 1 deletion packages/ui/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export { DBreadcrumb } from './breadcrumb';
export type { DButtonProps } from './button';
export { DButton } from './button';

export type { DCardProps, DCardActionProps, DCardHeaderProps, DCardContentProps } from './card';
export type { DCardProps, DCardActionsProps, DCardActionProps, DCardHeaderProps, DCardContentProps } from './card';
export { DCard } from './card';

export type { DCascaderProps } from './cascader';
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/components/root/contex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { DAvatarProps } from '../avatar';
import type { DBadgeProps } from '../badge';
import type { DBreadcrumbProps } from '../breadcrumb';
import type { DButtonProps } from '../button';
import type { DCardProps, DCardActionProps, DCardHeaderProps, DCardContentProps } from '../card';
import type { DCardProps, DCardActionsProps, DCardActionProps, DCardHeaderProps, DCardContentProps } from '../card';
import type { DCascaderProps } from '../cascader';
import type { DCheckboxProps, DCheckboxGroupProps } from '../checkbox';
import type { DComposeProps, DComposeItemProps } from '../compose';
Expand Down Expand Up @@ -77,6 +77,7 @@ export type DComponentConfig = {
DBreadcrumb: DBreadcrumbProps<any, any>;
DButton: DButtonProps;
DCard: DCardProps;
'DCard.Actions': DCardActionsProps;
'DCard.Action': DCardActionProps;
'DCard.Header': DCardHeaderProps;
'DCard.Content': DCardContentProps;
Expand Down

0 comments on commit 01176c2

Please sign in to comment.