Skip to content

Commit

Permalink
feat(ui): badge support custom text
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed May 4, 2023
1 parent b56080b commit 5dbc1da
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/ui/src/components/badge/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getClassName } from '@react-devui/utils';
import { registerComponentMate, TTANSITION_DURING_BASE } from '../../utils';
import { DTransition } from '../_transition';
import { useComponentConfig, usePrefixConfig } from '../root';
import { DBadgeText } from './BadgeText';
import { DNumber } from './Number';

export interface DBadgeProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> {
Expand All @@ -19,7 +20,10 @@ export interface DBadgeProps extends Omit<React.HTMLAttributes<HTMLDivElement>,
}

const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DBadge' as const });
export function DBadge(props: DBadgeProps): JSX.Element | null {
export const DBadge: {
(props: DBadgeProps): JSX.Element | null;
Text: typeof DBadgeText;
} = (props) => {
const {
dValue,
dTheme = 'danger',
Expand Down Expand Up @@ -122,4 +126,6 @@ export function DBadge(props: DBadgeProps): JSX.Element | null {
}}
</DTransition>
);
}
};

DBadge.Text = DBadgeText;
85 changes: 85 additions & 0 deletions packages/ui/src/components/badge/BadgeText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { getClassName } from '@react-devui/utils';

import { registerComponentMate, TTANSITION_DURING_BASE } from '../../utils';
import { DTransition } from '../_transition';
import { useComponentConfig, usePrefixConfig } from '../root';

export interface DBadgeTextProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> {
dText: string;
dTheme?: 'primary' | 'success' | 'warning' | 'danger';
dColor?: string;
dOffset?: [number | string, number | string];
dAlone?: boolean;
}

const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DBadge.Text' as const });
export function DBadgeText(props: DBadgeTextProps): JSX.Element | null {
const {
dText,
dTheme = 'danger',
dColor,
dOffset = [0, '100%'],
dAlone = false,

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

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

return (
<DTransition dIn={dText.length > 0} dDuring={TTANSITION_DURING_BASE}>
{(state) => {
let transitionStyle: React.CSSProperties = {};
switch (state) {
case 'enter':
transitionStyle = { transform: 'scale(0)', opacity: 0 };
break;

case 'entering':
transitionStyle = {
transition: ['transform', 'opacity'].map((attr) => `${attr} ${TTANSITION_DURING_BASE}ms ease-out`).join(', '),
};
break;

case 'leaving':
transitionStyle = {
transform: 'scale(0)',
opacity: 0,
transition: ['transform', 'opacity'].map((attr) => `${attr} ${TTANSITION_DURING_BASE}ms ease-in`).join(', '),
};
break;

default:
break;
}

return state === 'leaved' ? null : (
<div
{...restProps}
className={getClassName(restProps.className, `${dPrefix}badge`, {
[`t-${dTheme}`]: dTheme,
[`${dPrefix}badge--alone`]: dAlone,
})}
style={{
...restProps.style,
...(dAlone
? {}
: {
top: dOffset[0],
left: dOffset[1],
}),
[`--${dPrefix}badge-color`]: dColor,
}}
title={restProps.title ?? dText}
>
<div className={`${dPrefix}badge__wrapper`} style={transitionStyle}>
{dText}
</div>
</div>
);
}}
</DTransition>
);
}
3 changes: 3 additions & 0 deletions packages/ui/src/components/badge/demos/1.Basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export default function Demo() {
<div className="app-demo-badge">
<DBadge dValue={5} />
</div>
<div className="app-demo-badge">
<DBadge.Text dText="M" />
</div>
</>
);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/src/components/badge/demos/2.Theme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default function Demo() {
<div className="app-demo-badge">
<DBadge dValue={5} dTheme={theme} />
</div>
<div className="app-demo-badge">
<DBadge.Text dText="M" dTheme={theme} />
</div>
</>
);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/ui/src/components/badge/demos/3.Color.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ export default function Demo() {
<div className="app-demo-badge">
<DBadge dValue={5} dColor="#21b0b5" />
</div>
<div className="app-demo-badge">
<DBadge.Text dText="M" dColor="#21b0b5" />
</div>
<div className="app-demo-badge">
<DBadge dValue={5} dDot dColor="#7b4acb" />
</div>
<div className="app-demo-badge">
<DBadge dValue={5} dColor="#7b4acb" />
</div>
<div className="app-demo-badge">
<DBadge.Text dText="M" dColor="#7b4acb" />
</div>
</>
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/badge/demos/7.Alone.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function Demo() {
<>
<DBadge dValue={5} dDot dAlone />
<DBadge dValue={5} dAlone />
<DBadge.Text dText="M" dAlone />
</>
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/badge/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './Badge';
export * from './BadgeText';
2 changes: 1 addition & 1 deletion packages/ui/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export { DAutoComplete } from './auto-complete';
export type { DAvatarProps } from './avatar';
export { DAvatar } from './avatar';

export type { DBadgeProps } from './badge';
export type { DBadgeProps, DBadgeTextProps } from './badge';
export { DBadge } from './badge';

export type { DBreadcrumbProps } from './breadcrumb';
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 @@ -6,7 +6,7 @@ import type { DAlertProps } from '../alert';
import type { DAnchorProps } from '../anchor';
import type { DAutoCompleteProps } from '../auto-complete';
import type { DAvatarProps } from '../avatar';
import type { DBadgeProps } from '../badge';
import type { DBadgeProps, DBadgeTextProps } from '../badge';
import type { DBreadcrumbProps } from '../breadcrumb';
import type { DButtonProps } from '../button';
import type { DCardProps, DCardActionsProps, DCardActionProps, DCardHeaderProps, DCardContentProps } from '../card';
Expand Down Expand Up @@ -74,6 +74,7 @@ export type DComponentConfig = {
DAutoComplete: DAutoCompleteProps<any>;
DAvatar: DAvatarProps;
DBadge: DBadgeProps;
'DBadge.Text': DBadgeTextProps;
DBreadcrumb: DBreadcrumbProps<any, any>;
DButton: DButtonProps;
DCard: DCardProps;
Expand Down

0 comments on commit 5dbc1da

Please # to comment.