Skip to content

Commit

Permalink
fix(list): list option support JSX preview (#1515)
Browse files Browse the repository at this point in the history
Co-authored-by: berber1016 <c1094282069@gmail.com>
  • Loading branch information
berber1016 and berber1016 committed Nov 23, 2021
1 parent 1b56d47 commit f02637d
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/cascader/demos/Cascader.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const defaultOptions = [
];
const options = [...new Array(12)].map((_, index) => ({
label: `第${index}条咸鱼`,
value: index,
value: index.toString(),
childrens: [
{ label: '要', value: 'yes' },
{ label: '不要', value: 'no' },
Expand Down
5 changes: 0 additions & 5 deletions src/cascader/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,4 @@
box-shadow: 0 8px 24px rgba(36, 46, 89, 0.1);
.scrollbar(@x: hidden, @y: overlay);
}
&--item--trigger {
& > .@{list-item-prefix-cls} {
margin-bottom: 0;
}
}
}
24 changes: 24 additions & 0 deletions src/list/demos/List.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import './style.less';
import DragList from '../Drag';
import Selection from '../Selection';
import CascaderItem from '../inner/CascaderItem';
import Popover from '../../popover';

export default {
title: 'Upgraded/List',
Expand All @@ -34,6 +35,19 @@ const options = [
const Template: Story<ListProps> = (props) => {
const [value, setValue] = useState([]);
const [cascaderValue, setCascadervalue] = useState('1.1-1');
const wrapper = (e: React.ReactNode) => (
<Popover
placement="rightTop"
triggerStyle={{ display: 'initial' }}
content={
<div style={{ width: '200px', height: '200px', background: 'white', border: '1px solid black' }}>
<h3>preview</h3>
</div>
}
>
{e}
</Popover>
);
return (
<>
<div className="demo-box">
Expand Down Expand Up @@ -69,6 +83,16 @@ const Template: Story<ListProps> = (props) => {
</CascaderItem>
</List>
</div>
<div className="demo-box">
<h3>JSX render preview</h3>
<List>
<Item value="1" wrapper={wrapper}>
1
</Item>
</List>
<h3>option render preview</h3>
<List options={[{ label: '1', value: '1', wrapper }]} />
</div>
<div className="demo-box">
<List {...props}>
<Item value="1" prefix={<PlusOutlined size="14px" />} suffix={<FilterOutlined size="14px" />}>
Expand Down
1 change: 0 additions & 1 deletion src/list/hooks/useCacheOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const useCacheOptions = () => {
if (val === '' || typeof val === 'undefined') {
return undefined;
}

if (val?.includes('.')) {
return (val as any)
?.split('.')
Expand Down
9 changes: 5 additions & 4 deletions src/list/inner/CascaderItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const CascaderItem: React.ForwardRefRenderFunction<
return <></>;
};

const PopoverRender = (trigger: React.ReactNode): React.ReactElement => {
const PopoverRender = (element: React.ReactNode): React.ReactElement => {
if (!isEmpty(childrens) || React.isValidElement(children)) {
return (
<div className={prefixClsItem}>
Expand All @@ -85,13 +85,13 @@ const CascaderItem: React.ForwardRefRenderFunction<
content={content()}
getContainer={(node) => node || document.body}
>
<div className={`${prefixClsItem}--trigger`}>{trigger as React.ReactElement}</div>
{element}
</Popover>
</div>
);
}

return trigger as React.ReactElement;
return <>{element}</>;
};
const renderItem = (
<BaseItem
Expand All @@ -104,9 +104,10 @@ const CascaderItem: React.ForwardRefRenderFunction<
onChange={noop}
onClick={React.isValidElement(children) || !isEmpty(childrens) ? noop : onClick}
selected={isSelected}
wrapper={PopoverRender}
/>
);
return PopoverRender(renderItem);
return renderItem;
};

export default WithRef(CascaderItem);
9 changes: 6 additions & 3 deletions src/list/inner/baseItem.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import classNames from 'classnames';
import React, { DOMAttributes } from 'react';
import React, { DOMAttributes, ReactElement } from 'react';
import { isEmpty, isString, noop } from 'lodash';
import usePrefixCls from '../../utils/hooks/use-prefix-cls';
import { PREFIX } from '../constants';
import { BaseItemProps } from '../interfance';
import Tooltip from '../../tooltip';
import WithRef from '../../utils/withRef';

const defaultContentRender = (element: React.ReactNode | Element) => element;
const defaultContentRender = (element: React.ReactNode | Element): React.ReactElement => element as React.ReactElement;
const renderIcon = (className: string, prefix: React.ReactNode) => <span className={className}>{prefix}</span>;
const BaseItem: React.ForwardRefRenderFunction<
HTMLLIElement,
Expand All @@ -26,6 +26,7 @@ const BaseItem: React.ForwardRefRenderFunction<
disabledTooltip,
onClick,
contentRender = defaultContentRender,
wrapper = defaultContentRender,
} = props;

const prefixCls = `${usePrefixCls(PREFIX)}--item`;
Expand All @@ -45,7 +46,7 @@ const BaseItem: React.ForwardRefRenderFunction<
) : (
<>{content}</>
);
return (
const renderElement = (
<Tooltip
disabled={!disabled || isEmpty(disabledTooltip)}
strategy="fixed"
Expand All @@ -72,6 +73,8 @@ const BaseItem: React.ForwardRefRenderFunction<
</li>
</Tooltip>
);

return wrapper(renderElement) as ReactElement;
};

export default WithRef(BaseItem);
7 changes: 5 additions & 2 deletions src/list/interfance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export interface OptionProps {
disabledTooltip?: string;
prefix?: string | React.ReactNode;
suffix?: string | React.ReactNode;
wrapper?: (element: React.ReactNode) => React.ReactElement;

[key: string]: unknown;
}

Expand Down Expand Up @@ -116,15 +118,16 @@ export interface ItemProps
| 'disabled'
| 'selected'
| 'disabledTooltip'
| 'wrapper'
> {
selectValue?: string | string[];
}

export interface BaseItemProps extends Pick<OptionProps, 'value' | 'disabled' | 'prefix' | 'suffix'> {
export interface BaseItemProps extends Pick<OptionProps, 'value' | 'disabled' | 'prefix' | 'suffix' | 'wrapper'> {
className?: string;
style?: React.CSSProperties;
label?: string | React.ReactNode;
contentRender?: (element: React.ReactNode) => React.ReactNode | Element;
contentRender?: (element: React.ReactNode) => React.ReactElement;
children?: React.ReactNode;
disabledTooltip?: string;
selected?: boolean;
Expand Down
13 changes: 13 additions & 0 deletions src/list/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@list-prefix-cls: ~'@{component-prefix}-list-new';
@cascader-prefix-cls: ~'@{component-prefix}-cascader--new';
@cascader-content-prefix-cls: ~'@{cascader-prefix-cls}--cascader--content';
@popover-new__popcorn-prefix-cls: ~'@{component-prefix}-popover-new__popcorn';

.@{cascader-content-prefix-cls} {
&[data-popper-placement^='top'] {
Expand Down Expand Up @@ -46,6 +47,18 @@
box-shadow: 0 8px 24px rgba(36, 46, 89, 0.1);
.scrollbar(@x: hidden, @y: overlay);
&--item {
margin-bottom: 4px;
.@{list-prefix-cls}--item {
margin-bottom: 0;
}
&:last-child {
margin-bottom: 0;
}
.@{popover-new__popcorn-prefix-cls} {
display: inherit;
}
}
& > .@{list-prefix-cls}--item {
margin-bottom: 4px;
&:last-child {
margin-bottom: 0;
Expand Down
9 changes: 8 additions & 1 deletion src/popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const Popover = (props: PopoverProps) => {
overlayStyle,
children,
strategy = 'absolute',
triggerClassName,
triggerStyle,
getContainer,
} = props;

Expand Down Expand Up @@ -206,7 +208,12 @@ const Popover = (props: PopoverProps) => {

// =============== refs =====================
let triggerNode = (
<div className={`${prefixCls}__popcorn`} ref={(instance) => setReferenceELement(instance)} {...divRoles}>
<div
className={classNames(`${prefixCls}__popcorn`, triggerClassName)}
style={triggerStyle}
ref={(instance) => setReferenceELement(instance)}
{...divRoles}
>
{children}
</div>
);
Expand Down
5 changes: 5 additions & 0 deletions src/popover/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,9 @@ export interface PopoverProps {
* 显示隐藏的回掉
*/
onVisibleChange?: (visible: boolean) => void;
/**
*
*/
triggerStyle?: React.CSSProperties;
triggerClassName?: string;
}

1 comment on commit f02637d

@vercel
Copy link

@vercel vercel bot commented on f02637d Nov 23, 2021

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.