Skip to content

Commit

Permalink
refactor: 采用usePrefix以及withConfigConsumer进行组件 prefixCls 构建 (#485)
Browse files Browse the repository at this point in the history
* feat: use-prefix-cls

* fix: import

* refactor(components): use component-prefix-cl in all components

affects: @gio-design/components

* refactor(components): use context rootPrefixCls in components

affects: @gio-design/components

* refactor(breadcrumb): change prefixCls in BreadcrumbItem

affects: @gio-design/components

Co-authored-by: i18u <iahu1988@gmail.com>
  • Loading branch information
phyzess and iahu authored Nov 18, 2020
1 parent 79974bc commit d80ea0c
Show file tree
Hide file tree
Showing 110 changed files with 1,218 additions and 1,123 deletions.
22 changes: 12 additions & 10 deletions packages/components/src/components/alert/alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import classnames from 'classnames';
import _ from 'lodash';
import { CheckCircleFilled, WarningFilled, InformationFilled, CloseCircleFilled, Close } from '@gio-design/icons';
import { AlertProps } from './interfaces';
import usePrefixCls from '../../utils/hooks/use-prefix-cls';

const Alert: React.FC<AlertProps> = (props: AlertProps) => {
const prefixCls = usePrefixCls('alert');
const [alertStatus, setAlertStatus] = useState(true);
const {
message,
description,
closeable,
showIcon = false,
colseText,
closeText,
onClose,
icon,
type = 'info',
Expand Down Expand Up @@ -42,31 +44,31 @@ const Alert: React.FC<AlertProps> = (props: AlertProps) => {
<div>
<div
className={classnames(
'gio-alert',
size === 'small' ? 'gio-alert-small' : '',
alertStatus ? `gio-alert-${type}` : 'gio-alert-close'
prefixCls,
size === 'small' ? `${prefixCls}-small` : '',
alertStatus ? `${prefixCls}-${type}` : `${prefixCls}-close`
)}
>
<div className="gio-alert-icon" style={{ display: showIcon ? 'block' : 'none' }}>
<div className={`${prefixCls}-icon`} style={{ display: showIcon ? 'block' : 'none' }}>
{getIcon()}
</div>
<div className="gio-alert-content">
<div className="gio-alert-content-title" style={{ display: message ? 'block' : 'none' }}>
<div className={`${prefixCls}-content`}>
<div className={`${prefixCls}-content-title`} style={{ display: message ? 'block' : 'none' }}>
{message || null}
</div>
<div className="gio-alert-content-description" style={{ display: description ? 'block' : 'none' }}>
<div className={`${prefixCls}-content-description`} style={{ display: description ? 'block' : 'none' }}>
{description || null}
</div>
</div>
<div
className="gio-alert-closeIcon"
className={`${prefixCls}-closeIcon`}
style={{ display: closeable ? 'block' : 'none' }}
onClick={closeAlert}
role="button"
tabIndex={0}
onKeyPress={_.noop}
>
{colseText || <Close />}
{closeText || <Close />}
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/alert/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface AlertProps {
/**
自定义关闭按钮
*/
colseText?: React.ReactNode;
closeText?: React.ReactNode;
/**
警告提示的辅助性文字介绍
*/
Expand Down
42 changes: 22 additions & 20 deletions packages/components/src/components/alert/style/index.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@import '../../../stylesheet/index.less';

.gio-alert {
@alert-prefix-cls: ~'@{component-prefix}-alert';

.@{alert-prefix-cls} {
display: flex;
align-items: center;
width: 100%;
Expand All @@ -16,95 +18,95 @@
padding-top: 1px;
}

& .gio-alert-content {
& .@{alert-prefix-cls}-content {
flex: 1;
margin: 0 8px;
color: @color-text-alert-content;
font-size: @size-font-14;
font-family: @font-family-primary;
line-height: 22px;

& .gio-alert-content-title {
& .@{alert-prefix-cls}-content-title {
width: 100%;
overflow: hidden;
font-weight: @weight-font-medium;
text-overflow: ellipsis;
}

& .gio-alert-content-description {
& .@{alert-prefix-cls}-content-description {
width: 100%;
letter-spacing: 0;
}
}

& .gio-alert-closeIcon {
& .@{alert-prefix-cls}-closeIcon {
flex-shrink: 0;
align-self: flex-start;
font-weight: @weight-font-semi-bold;
cursor: pointer;
}
}

.gio-alert-close {
.@{alert-prefix-cls}-close {
display: none;
}

.gio-alert-info {
.@{alert-prefix-cls}-info {
background-color: @color-background-alert-information;

& .gio-alert-closeIcon {
& .@{alert-prefix-cls}-closeIcon {
svg {
fill: @palette-blue-6;
}
}

& .gio-alert-icon {
& .@{alert-prefix-cls}-icon {
svg {
fill: @palette-blue-6;
}
}
}

.gio-alert-success {
.@{alert-prefix-cls}-success {
background-color: @color-background-alert-success;
& .gio-alert-closeIcon {
& .@{alert-prefix-cls}-closeIcon {
fill: @palette-green-6;
}

& .gio-alert-icon {
& .@{alert-prefix-cls}-icon {
fill: @palette-green-6;
}
}

.gio-alert-warning {
.@{alert-prefix-cls}-warning {
background-color: @color-background-alert-warning;
& .gio-alert-closeIcon {
& .@{alert-prefix-cls}-closeIcon {
svg {
fill: @palette-yellow-6;
}
}

& .gio-alert-icon {
& .@{alert-prefix-cls}-icon {
svg {
fill: @palette-yellow-6;
}
}
}

.gio-alert-error {
.@{alert-prefix-cls}-error {
background-color: @color-background-alert-error;
& .gio-alert-closeIcon {
& .@{alert-prefix-cls}-closeIcon {
fill: @palette-red-6;
}

& .gio-alert-icon {
& .@{alert-prefix-cls}-icon {
fill: @palette-red-6;
}
}

.gio-alert-small {
.@{alert-prefix-cls}-small {
padding: 8px 16px;
& .gio-alert-content {
& .@{alert-prefix-cls}-content {
font-size: @size-font-12;
}
}
7 changes: 3 additions & 4 deletions packages/components/src/components/avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { useState, useEffect, useContext, useRef } from 'react';
import React, { useState, useEffect, useRef } from 'react';
import classNames from 'classnames';
import { More } from '@gio-design/icons';
import { isNil } from 'lodash';
import Tooltip from '../tooltip';
import { AvatarProps } from './interfaces';
import { ConfigContext } from '../config-provider';
import usePrefixCls from '../../utils/hooks/use-prefix-cls';
import composeRef from '../../utils/composeRef';

const Avatar = React.forwardRef<HTMLSpanElement, AvatarProps>((props: AvatarProps, ref: React.Ref<HTMLSpanElement>) => {
Expand All @@ -22,7 +22,6 @@ const Avatar = React.forwardRef<HTMLSpanElement, AvatarProps>((props: AvatarProp
...rest
} = props;

const { getPrefixCls } = useContext(ConfigContext);
const [isImgExist, setIsImgExist] = useState<boolean>(src !== undefined);
const [scale, setScale] = useState<number>(1);
const nodeRef = useRef<HTMLSpanElement>(null);
Expand All @@ -37,7 +36,7 @@ const Avatar = React.forwardRef<HTMLSpanElement, AvatarProps>((props: AvatarProp
}
}, [userName]);

const prefixCls = getPrefixCls('avatar', customizePrefixCls);
const prefixCls = usePrefixCls('avatar', customizePrefixCls);
const classString = classNames(className, prefixCls, {
[`${prefixCls}-sm`]: size === 'small',
[`${prefixCls}-df`]: size === 'default',
Expand Down
7 changes: 3 additions & 4 deletions packages/components/src/components/avatar/AvatarGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import React, { useContext } from 'react';
import React from 'react';
import classNames from 'classnames';
import _ from 'lodash';
import Avatar from './Avatar';
import { AvatarGroupProps, UserAvatarType } from './interfaces';
import { ConfigContext } from '../config-provider';
import usePrefixCls from '../../utils/hooks/use-prefix-cls';

const AvatarGroup: React.FC<AvatarGroupProps> = (props: AvatarGroupProps) => {
const { number = 5, users = [], className, placement = 'bottom', style, displayTooltip = true } = props;
const { getPrefixCls } = useContext(ConfigContext);
const prefixCls = getPrefixCls('avatar');
const prefixCls = usePrefixCls('avatar');

let children = null;
const renderAvatarGroup = (sliceUsers: UserAvatarType[]) =>
Expand Down
18 changes: 6 additions & 12 deletions packages/components/src/components/banner/Banner.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import React, { useState, useContext } from 'react';
import React, { useState } from 'react';
import { Close } from '@gio-design/icons';
import className from 'classnames';
import { ConfigContext } from '../config-provider';
import usePrefixCls from '../../utils/hooks/use-prefix-cls';
import { BannerProps } from './interface';

const Banner: React.FC<BannerProps> = (props: BannerProps) => {
const {
type = 'normal', content, closeable = true, onClose, button, prefixCls: customizePrefixCls,
} = props;
const { type = 'normal', content, closeable = true, onClose, button, prefixCls: customizePrefixCls } = props;
const [visible, setVisible] = useState(true);
const { getPrefixCls } = useContext(ConfigContext);
const prefixCls = getPrefixCls('banner', customizePrefixCls);
const prefixCls = usePrefixCls('banner', customizePrefixCls);
const onCloseBanner = () => {
setVisible(false);
onClose?.();
Expand All @@ -22,13 +19,10 @@ const Banner: React.FC<BannerProps> = (props: BannerProps) => {
`${prefixCls}`,
`${prefixCls}-${type}`,
closeable && `${prefixCls}-closeable`,
!visible && `${prefixCls}-close`,
!visible && `${prefixCls}-close`
)}
>
<div className={className(`${prefixCls}-content`, button && `${prefixCls}-content-button`)}>
{' '}
{content}
</div>
<div className={className(`${prefixCls}-content`, button && `${prefixCls}-content-button`)}> {content}</div>
<div className={className(`${prefixCls}-button`)}>{button}</div>
<div
className={className(`${prefixCls}-closeIcon`)}
Expand Down
6 changes: 2 additions & 4 deletions packages/components/src/components/breadcrumb/Breadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import classNames from 'classnames';
import toArray from 'rc-util/lib/Children/toArray';
import BreadcrumbItem from './BreadcrumbItem';
import BreadcrumbSeparator from './BreadcrumbSeparator';
import { ConfigContext } from '../config-provider';
import usePrefixCls from '../../utils/hooks/use-prefix-cls';
import { cloneElement } from '../../utils/reactNode';
import devWarning from '../../utils/devWarning';
import { Route, BreadcrumbProps } from './interface';
Expand Down Expand Up @@ -55,10 +55,8 @@ const Breadcrumb: BreadcrumbInterface = ({
params = {},
...restProps
}: BreadcrumbProps) => {
const { getPrefixCls } = React.useContext(ConfigContext);

let crumbs;
const prefixCls = getPrefixCls('breadcrumb', customizePrefixCls);
const prefixCls = usePrefixCls('breadcrumb', customizePrefixCls);
if (routes && routes.length > 0) {
const paths: string[] = [];
crumbs = routes.map((route: Route) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@ import React from 'react';
import classnames from 'classnames';
import BreadcrumbSeparator from './BreadcrumbSeparator';
import { BreadcrumbItemProps } from './interface';
import usePrefixCls from '../../utils/hooks/use-prefix-cls';

interface BreadcrumbItemInterface extends React.FC<BreadcrumbItemProps> {
GIO_BREADCRUMB_ITEM: boolean;
}

const BreadcrumbItem: BreadcrumbItemInterface = (props: BreadcrumbItemProps) => {
const { children, separator, ...restProps } = props;
const prefixCls = usePrefixCls('breadcrumb');
const link =
'href' in restProps ? (
<a className="gio-breadcrumb-item-link-target" {...restProps}>
<a className={`${prefixCls}-item-link-target`} {...restProps}>
{children}
</a>
) : (
<span className="gio-breadcrumb-item-link-target" {...restProps}>
<span className={`${prefixCls}-item-link-target`} {...restProps}>
{children}
</span>
);

if (children) {
return (
<span className={classnames('gio-breadcrumb-item', 'gio-breadcrumb-item-link')}>
<span className={classnames(`${prefixCls}-item`, `${prefixCls}-item-link`)}>
{link}
{separator && separator !== '' && <BreadcrumbSeparator separator={separator} />}
</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import usePrefixCls from '../../utils/hooks/use-prefix-cls';
import { BreadcrumbSeparatorProps } from './interface';

interface BreadcrumbSeparatorInterface extends React.FC<BreadcrumbSeparatorProps> {
Expand All @@ -7,7 +8,8 @@ interface BreadcrumbSeparatorInterface extends React.FC<BreadcrumbSeparatorProps

const BreadcrumbSeparator: BreadcrumbSeparatorInterface = (props: BreadcrumbSeparatorProps) => {
const { separator = '/' } = props;
return <span className="gio-breadcrumb-separator">{separator}</span>;
const prefixCls = usePrefixCls('breadcrumb');
return <span className={`${prefixCls}-separator`}>{separator}</span>;
};

BreadcrumbSeparator.GIO_BREADCRUMB_SEPARATOR = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
@import '../../../stylesheet/index.less';

.gio-breadcrumb {
@breadcrumb-prefix-cls: ~'@{component-prefix}-breadcrumb';

.@{breadcrumb-prefix-cls} {
height: 22px;
color: @color-text-breadcrumb-normal;
font-size: 14px;

&-item.gio-breadcrumb-item-link {
.gio-breadcrumb-item-link-target {
&-item.@{breadcrumb-prefix-cls}-item-link {
.@{breadcrumb-prefix-cls}-item-link-target {
cursor: pointer;

&:hover {
Expand Down
5 changes: 3 additions & 2 deletions packages/components/src/components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ConfigContext } from '../config-provider';
import SizeContext from '../config-provider/SizeContext';
import { ButtonProps } from './interfaces';
import { cloneElement } from '../../utils/reactNode';
import usePrefixCls from '../../utils/hooks/use-prefix-cls';

const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
Expand Down Expand Up @@ -79,7 +80,8 @@ const InternalButton: React.ForwardRefRenderFunction<unknown, ButtonProps> = (pr
const size = React.useContext(SizeContext);
const [innerLoading, setLoading] = React.useState<boolean | undefined>(loading);
const [hasTwoCNChar, setHasTwoCNChar] = React.useState(false);
const { getPrefixCls, autoInsertSpaceInButton } = React.useContext(ConfigContext);
const { autoInsertSpaceInButton } = React.useContext(ConfigContext);
const prefixCls = usePrefixCls('btn', customizePrefixCls);
const buttonRef = (ref as any) || React.createRef<HTMLElement>();

const isNeedInserted = () => React.Children.count(children) === 1 && !icon;
Expand Down Expand Up @@ -118,7 +120,6 @@ const InternalButton: React.ForwardRefRenderFunction<unknown, ButtonProps> = (pr
}
};

const prefixCls = getPrefixCls('btn', customizePrefixCls);
const autoInsertSpace = autoInsertSpaceInButton !== false;

const sizeCls = customizeSize || size;
Expand Down
Loading

1 comment on commit d80ea0c

@vercel
Copy link

@vercel vercel bot commented on d80ea0c Nov 18, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.