Skip to content

Commit

Permalink
feat(popover): add distoryOnHide attribute (#1516)
Browse files Browse the repository at this point in the history
Co-authored-by: berber1016 <c1094282069@gmail.com>
  • Loading branch information
berber1016 and berber1016 authored Nov 23, 2021
1 parent f02637d commit 187aa45
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/cascader/Cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const Cascader: React.FC<CascaderProps> = (props) => {
<Popover
content={renderOverlay()}
trigger="click"
distoryOnHide={false}
visible={visible}
onVisibleChange={handVisibleChange}
getContainer={getContainer}
Expand Down
1 change: 1 addition & 0 deletions src/list-picker/interfance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface ListPickerProps extends Pick<ListProps, 'model' | 'disabled' |
onConfim?: (value: string | string[] | undefined, options?: OptionProps | OptionProps[]) => void;
placement?: Placement;
overlayStyle?: React.CSSProperties;
overlayClassName?: string;
}

// export interface StaticListPickerProps extends Omit<ListProps, 'options'> {
Expand Down
5 changes: 3 additions & 2 deletions src/list-picker/listPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import useControlledState from '../utils/hooks/useControlledState';
import usePrefixCls from '../utils/hooks/use-prefix-cls';
import { OptionProps } from '../list/interfance';
import Button from '../button';
// import Page from '../page';
import { ListContext } from '../list/context';
import useCacheOptions from '../list/hooks/useCacheOptions';

Expand All @@ -31,6 +30,7 @@ const ListPicker: React.FC<ListPickerProps> = (props) => {
getContainer,
placement = 'bottomLeft',
overlayStyle,
overlayClassName,
children,
onConfim,
confimText = '确定',
Expand Down Expand Up @@ -121,12 +121,13 @@ const ListPicker: React.FC<ListPickerProps> = (props) => {
return (
<ListContext.Provider value={{ value, model, onChange: handleChange, options, setOptions }}>
<Popover
distoryOnHide={false}
content={renderOverlay()}
trigger={trigger}
visible={visible}
onVisibleChange={handVisibleChange}
getContainer={getContainer}
overlayClassName={`${defaultPrefix}--content`}
overlayClassName={classNames(`${defaultPrefix}--content`, overlayClassName)}
placement={placement}
overlayStyle={overlayStyle}
>
Expand Down
24 changes: 1 addition & 23 deletions src/list/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Item from './Item';
import { convertChildrenToData, isCascader, isMultipe } from './util';
import WithRef from '../utils/withRef';
import './style';
import Popover from '../popover';
import { ListContext } from './context';
import useValue from './hooks/useValue';
import useCacheOptions from './hooks/useCacheOptions';
Expand Down Expand Up @@ -37,10 +36,7 @@ const List: React.ForwardRefRenderFunction<HTMLDivElement, ListProps> & {
prefix,
suffix,
onChange: controlledOnChange,
showPreview,
renderItem,
previewRender,
previewRenderContainer,
} = props;

const prefixCls = usePrefixCls(PREFIX);
Expand Down Expand Up @@ -97,24 +93,6 @@ const List: React.ForwardRefRenderFunction<HTMLDivElement, ListProps> & {
}
};

const renderPreview = (option: OptionProps, content: React.ReactElement) => {
if (showPreview) {
return (
<div className={`${prefixCls}--preview`}>
<Popover
placement="rightTop"
strategy="fixed"
getContainer={previewRenderContainer}
content={previewRender?.(option)}
overlayClassName={`${prefixCls}--preview--overlay`}
>
{content}
</Popover>
</div>
);
}
return content;
};
const renderChildren = (option: OptionProps) => {
const renderedItem = renderItem?.(option);
return (
Expand All @@ -138,7 +116,7 @@ const List: React.ForwardRefRenderFunction<HTMLDivElement, ListProps> & {
const renderChildrens = (child: React.ReactNode[] | OptionProps[]) => {
// options render
if (!isEmpty(initOptions)) {
return (child as OptionProps[])?.map((option: OptionProps) => renderPreview(option, renderChildren(option)));
return (child as OptionProps[])?.map((option: OptionProps) => renderChildren(option));
}
// childrens render
return (child as React.ReactNode[])?.map(
Expand Down
1 change: 1 addition & 0 deletions src/list/inner/CascaderItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const CascaderItem: React.ForwardRefRenderFunction<
overlayClassName={popoverClassName}
content={content()}
getContainer={(node) => node || document.body}
distoryOnHide={false}
>
{element}
</Popover>
Expand Down
6 changes: 0 additions & 6 deletions src/list/interfance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ export interface ListProps {
* 仅支持options 形式。自定义 item render 自定义render时会劫持onClick方法提供给List来使用
*/
renderItem?: (option: OptionProps) => React.ReactElement;
/**
* 是否显示preview 弹出面板 目前仅支持了options参数形式
*/
showPreview?: boolean;
previewRender?: (option: OptionProps) => React.ReactNode;
previewRenderContainer?: (node: HTMLElement) => HTMLElement;
/**
* empty
*/
Expand Down
10 changes: 8 additions & 2 deletions src/popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const Popover = (props: PopoverProps) => {
triggerClassName,
triggerStyle,
getContainer,
distoryOnHide = true,
} = props;

const prefixCls = usePrefixCls('popover-new', customPrefixCls);
Expand Down Expand Up @@ -230,14 +231,19 @@ const Popover = (props: PopoverProps) => {
triggerNode = React.cloneElement(child as React.ReactElement, cloneProps);
}

return (
const renderContent = (
<>
{triggerNode}
{typeof getContainer === 'function'
? ReactDOM.createPortal(contentRender, getContainer(referenceElement as HTMLDivElement))
: contentRender}
</>
);
return (
<>
{triggerNode}
{distoryOnHide ? visible && renderContent : renderContent}
</>
);
};

export default Popover;
1 change: 1 addition & 0 deletions src/popover/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,5 @@ export interface PopoverProps {
*/
triggerStyle?: React.CSSProperties;
triggerClassName?: string;
distoryOnHide?: boolean;
}
1 change: 1 addition & 0 deletions src/select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const Select: React.FC<SelectProps> = (props) => {
return (
<ListContext.Provider value={{ value, onChange: handleChange }}>
<Popover
distoryOnHide={false}
content={renderOverlay()}
trigger="click"
visible={visible}
Expand Down

1 comment on commit 187aa45

@vercel
Copy link

@vercel vercel bot commented on 187aa45 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.