Skip to content

Commit

Permalink
Merge pull request kubesphere#152 from chenz24/hotfix
Browse files Browse the repository at this point in the history
refactor: Improve expand feature of Entity.
  • Loading branch information
chenz24 authored Mar 28, 2022
2 parents c899efe + 60f37cd commit 339fd93
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/lemon-hairs-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@kubed/components': patch
---

1. refactor: Improve expand feature of Entity.
2. feat: StatusDot support set className for label.
2 changes: 1 addition & 1 deletion packages/components/src/Entity/Entity.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const EntityExpand = () => {
);
return (
<Wrapper>
<Entity expandable expandContent={expandContent}>
<Entity expandable hoverable expandContent={expandContent} className="test-classname">
<Field avatar={Avatar} label="存储类型: rocksdb" value={<a href="/">rocksdbpvc</a>} />
<Field label="存储卷" value="rocksdbpvc" />
<Field label="容量" value="1Gi" width={100} />
Expand Down
62 changes: 52 additions & 10 deletions packages/components/src/Entity/Entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import styled, { css } from 'styled-components';
import cx from 'classnames';
import { useToggle } from '@kubed/hooks';
import { Dropdown } from '../Dropdown/Dropdown';
import { Field } from './Field';
import { DefaultProps, KubedTheme } from '../theme';
import forwardRef from '../utils/forwardRef';
Expand All @@ -20,6 +21,14 @@ const getHoverStyle = (hoverable, theme: KubedTheme) => {
return null;
};

const Wrapper = styled.div`
position: relative;
& > div:nth-child(2) {
width: 100%;
}
`;

const EntityWrapper = styled('div')<EntityProps>`
display: flex;
flex-direction: column;
Expand All @@ -31,7 +40,8 @@ const EntityWrapper = styled('div')<EntityProps>`
transition: all 0.3s ease-in-out;
cursor: ${({ expandable }) => (expandable ? 'pointer' : null)};
&:hover {
&:hover,
&.is-expand {
${({ hoverable, theme }) => getHoverStyle(hoverable, theme)};
}
`;
Expand All @@ -52,17 +62,11 @@ const EntityFooter = styled('div')`
`;

const ExpandContainer = styled('div')`
position: absolute;
width: 100%;
left: 0;
bottom: 0;
transform: translate3d(0, 100%, 0);
padding: 12px;
z-index: 1;
border: 1px solid ${({ theme }) => theme.palette.accents_5};
border-top: 0;
border-radius: 0 0 4px 4px;
box-shadow: 0 4px 8px 0 ${({ theme }) => addColorAlpha(theme.palette.accents_8, 0.2)};
`;

export interface EntityProps extends DefaultProps {
Expand All @@ -87,7 +91,16 @@ export interface EntityProps extends DefaultProps {

export const Entity = forwardRef<EntityProps, 'div'>(
(
{ children, footer, gap = 20, bordered = true, expandable = false, expandContent, ...rest },
{
children,
className,
footer,
gap = 20,
bordered = true,
expandable = false,
expandContent,
...rest
},
ref
) => {
const [expand, setExpand] = useToggle(false, [true, false]);
Expand All @@ -99,20 +112,49 @@ export const Entity = forwardRef<EntityProps, 'div'>(
}
};

if (expandable) {
return (
<Wrapper>
<Dropdown
content={
<ExpandContainer className="expand-container">{expandContent}</ExpandContainer>
}
appendTo="parent"
className="entity-dropdown"
maxWidth="100%"
offset={[0, -3]}
>
<EntityWrapper
ref={ref}
bordered={bordered}
expandable={expandable}
onClick={handleExpand}
className={cx({ 'is-expand': expandable && expand }, className)}
{...rest}
>
<EntityContainer $gap={gap} className="entity-main">
{children}
</EntityContainer>
{footer && <EntityFooter className="entity-footer">{footer}</EntityFooter>}
</EntityWrapper>
</Dropdown>
</Wrapper>
);
}

return (
<EntityWrapper
ref={ref}
bordered={bordered}
expandable={expandable}
onClick={handleExpand}
className={cx({ 'is-expand': expandable && expand })}
className={cx({ 'is-expand': expandable && expand }, className)}
{...rest}
>
<EntityContainer $gap={gap} className="entity-main">
{children}
</EntityContainer>
{footer && <EntityFooter className="entity-footer">{footer}</EntityFooter>}
{expand && <ExpandContainer className="expand-container">{expandContent}</ExpandContainer>}
</EntityWrapper>
);
}
Expand Down
9 changes: 6 additions & 3 deletions packages/components/src/StatusDot/StatusDot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,19 @@ export interface StatusDotProps {
/** Dot color from theme */
color?: string | 'default' | 'primary' | 'secondary' | 'success' | 'warning' | 'error';

/** Disable animation status or not */
/** Disable animation status or not */
motion?: boolean;

/** className of label */
labelClassName?: string;
}

export const StatusDot = forwardRef<StatusDotProps, 'div'>(
({ color, motion = false, children, ...rest }, ref) => {
({ color, motion = false, labelClassName, children, ...rest }, ref) => {
return (
<DotWrapper ref={ref} {...rest}>
<Dot color={color} motion={motion} />
{children && <Label>{children}</Label>}
{children && <Label className={labelClassName}>{children}</Label>}
</DotWrapper>
);
}
Expand Down

0 comments on commit 339fd93

Please sign in to comment.