forked from kubesphere/kube-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubesphere#157 from chenz24/feat/collapse
Feat/collapse
- Loading branch information
Showing
10 changed files
with
252 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@kubed/components': patch | ||
--- | ||
|
||
1. Add Collapse Component |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as React from 'react'; | ||
import { Error, Pod } from '@kubed/icons'; | ||
import Collapse from './Collapse'; | ||
import { BadgeAnchor, Entity, Field, Tooltip, Text, Card } from '../index'; | ||
|
||
export default { | ||
title: 'Components/Collapse', | ||
component: Collapse, | ||
}; | ||
|
||
const { Panel } = Collapse; | ||
|
||
const Avatar = ( | ||
<BadgeAnchor offset={[5, 5]}> | ||
<Tooltip content="Badge information"> | ||
<Error className="badge" size={18} color="#fff" fill="rgb(245, 166, 35)" /> | ||
</Tooltip> | ||
<Pod size={40} /> | ||
</BadgeAnchor> | ||
); | ||
|
||
export const basic = () => ( | ||
<Collapse accordion> | ||
<Panel header="This is panel header 1" key="1"> | ||
<p>Panel content Panel content Panel content</p> | ||
</Panel> | ||
<Panel header="This is panel header 2" key="2"> | ||
<p>Panel content 2 Panel content 2 Panel content 2</p> | ||
</Panel> | ||
<Panel header="This is panel header 3" key="3"> | ||
<p>Panel content3 Panel content3 Panel content3</p> | ||
</Panel> | ||
</Collapse> | ||
); | ||
|
||
export const EntityCollapse = () => { | ||
const header = ( | ||
<Entity hoverable className="test-classname"> | ||
<Field avatar={Avatar} label="存储类型: rocksdb" value={<a href="/">rocksdbpvc</a>} /> | ||
<Field label="存储卷" value="rocksdbpvc" /> | ||
<Field label="容量" value="1Gi" width={100} /> | ||
<Field label="访问模式" value="ReadWriteOnce" /> | ||
</Entity> | ||
); | ||
|
||
return ( | ||
<Collapse accordion> | ||
<Panel header={header} key="1"> | ||
<p>Panel content1 Panel content1</p> | ||
</Panel> | ||
<Panel header="This is panel header 2" key="2"> | ||
<p>Panel content2 Panel content2 Panel content2</p> | ||
</Panel> | ||
<Panel header="This is panel header 3" key="3"> | ||
<p>Panel content3 Panel content3 Panel content3</p> | ||
</Panel> | ||
</Collapse> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import styled from 'styled-components'; | ||
import RcCollapse from 'rc-collapse'; | ||
|
||
export const StyledCollapse = styled(RcCollapse)` | ||
.kubed-collapse-content-hidden { | ||
display: none; | ||
} | ||
.motion-collapse { | ||
overflow: hidden; | ||
transition: height 0.2s cubic-bezier(0.645, 0.045, 0.355, 1), | ||
opacity 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) !important; | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// https://github.com/ant-design/ant-design/blob/master/components/collapse/Collapse.tsx | ||
|
||
import React, { PropsWithChildren } from 'react'; | ||
import { CSSMotionProps } from 'rc-motion'; | ||
import { omit } from 'lodash'; | ||
|
||
// import { CaretRight } from '@kubed/icons'; | ||
import CollapsePanel, { CollapsibleType } from './CollapsePanel'; | ||
import collapseMotion from '../utils/motion'; | ||
import toArray from '../utils/toArray'; | ||
import { cloneElement } from '../utils/reactNode'; | ||
import { StyledCollapse } from './Collapse.styles'; | ||
|
||
export type ExpandIconPosition = 'left' | 'right' | undefined; | ||
|
||
export interface CollapseProps { | ||
activeKey?: Array<string | number> | string | number; | ||
defaultActiveKey?: Array<string | number> | string | number; | ||
accordion?: boolean; | ||
destroyInactivePanel?: boolean; | ||
onChange?: (key: string | string[]) => void; | ||
style?: React.CSSProperties; | ||
className?: string; | ||
bordered?: boolean; | ||
prefixCls?: string; | ||
expandIcon?: (panelProps: PanelProps) => React.ReactNode; | ||
expandIconPosition?: ExpandIconPosition; | ||
ghost?: boolean; | ||
collapsible?: CollapsibleType; | ||
children?: React.ReactNode; | ||
} | ||
|
||
interface PanelProps { | ||
isActive?: boolean; | ||
header?: React.ReactNode; | ||
className?: string; | ||
style?: React.CSSProperties; | ||
showArrow?: boolean; | ||
forceRender?: boolean; | ||
/** @deprecated Use `collapsible="disabled"` instead */ | ||
disabled?: boolean; | ||
extra?: React.ReactNode; | ||
collapsible?: CollapsibleType; | ||
} | ||
|
||
interface CollapseInterface extends React.FC<CollapseProps> { | ||
Panel: typeof CollapsePanel; | ||
} | ||
|
||
const Collapse: CollapseInterface = (props: PropsWithChildren<CollapseInterface>) => { | ||
const openMotion: CSSMotionProps = { | ||
...collapseMotion, | ||
motionAppear: false, | ||
leavedClassName: 'kubed-collapse-content-hidden', | ||
}; | ||
|
||
const getItems = () => { | ||
const { children } = props; | ||
return toArray(children).map((child: React.ReactElement, index: number) => { | ||
if (child.props?.disabled) { | ||
const key = child.key || String(index); | ||
const { disabled, collapsible } = child.props; | ||
const childProps: CollapseProps & { key: React.Key } = { | ||
...omit(child.props, ['disabled']), | ||
key, | ||
collapsible: collapsible ?? (disabled ? 'disabled' : undefined), | ||
}; | ||
return cloneElement(child, childProps); | ||
} | ||
return child; | ||
}); | ||
}; | ||
|
||
return ( | ||
<StyledCollapse openMotion={openMotion} prefixCls="kubed-collapse" {...props}> | ||
{getItems()} | ||
</StyledCollapse> | ||
); | ||
}; | ||
|
||
Collapse.Panel = CollapsePanel; | ||
|
||
export default Collapse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// https://github.com/ant-design/ant-design/blob/master/components/collapse/CollapsePanel.tsx | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
import RcCollapse from 'rc-collapse'; | ||
|
||
export type CollapsibleType = 'header' | 'disabled'; | ||
|
||
export interface CollapsePanelProps { | ||
key: string | number; | ||
header: React.ReactNode; | ||
disabled?: boolean; | ||
className?: string; | ||
style?: React.CSSProperties; | ||
showArrow?: boolean; | ||
prefixCls?: string; | ||
forceRender?: boolean; | ||
id?: string; | ||
extra?: React.ReactNode; | ||
collapsible?: CollapsibleType; | ||
children?: React.ReactNode; | ||
} | ||
|
||
const CollapsePanel: React.FC<CollapsePanelProps> = (props: CollapsePanelProps) => { | ||
const { className = '', showArrow } = props; | ||
const collapsePanelClassName = classNames( | ||
{ | ||
'collapse-panel-no-arrow': !showArrow, | ||
}, | ||
className | ||
); | ||
|
||
return <RcCollapse.Panel {...props} className={collapsePanelClassName} />; | ||
}; | ||
|
||
export default CollapsePanel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as React from 'react'; | ||
|
||
export const { isValidElement } = React; | ||
|
||
type AnyObject = Record<any, any>; | ||
|
||
type RenderProps = undefined | AnyObject | ((originProps: AnyObject) => AnyObject | undefined); | ||
|
||
export function replaceElement( | ||
element: React.ReactNode, | ||
replacement: React.ReactNode, | ||
props: RenderProps | ||
): React.ReactNode { | ||
if (!isValidElement(element)) return replacement; | ||
|
||
return React.cloneElement( | ||
element, | ||
typeof props === 'function' ? props(element.props || {}) : props | ||
); | ||
} | ||
|
||
export function cloneElement(element: React.ReactNode, props?: RenderProps): React.ReactElement { | ||
return replaceElement(element, element, props) as React.ReactElement; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters