Skip to content

Commit

Permalink
fix(list-picker): fix Recent multiple error (#1590)
Browse files Browse the repository at this point in the history
  • Loading branch information
berber1016 committed Dec 2, 2021
1 parent d2dd29a commit 17e367c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/list-picker/Recent.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { slice } from 'lodash';
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { List, OptionProps } from '../list';
import { PREFIX } from '../list/constants';
import { ListContext } from '../list/context';
import usePrefixCls from '../utils/hooks/use-prefix-cls';

export const ITEM_KEY = '__GIO_SELECTION_KEY';

Expand All @@ -13,8 +15,9 @@ interface RecentProps {
const Recent: React.FC<RecentProps> & { isRecent: boolean } = (props) => {
const { max = 5, title = '最近使用' } = props;
const [mayBeArray, setMayBearray] = useState<Map<string, OptionProps> | undefined>(new Map());
const selectionPrefixCls = `${usePrefixCls(PREFIX)}--selection`;
const context = useContext(ListContext);
const { options } = context;
const { options, model } = context;
const localStorageValue = window?.localStorage?.getItem(ITEM_KEY) || '[]';
const matchValue = JSON.parse(localStorageValue); // localStorage.getItem('__GIO_SELECTION_KEY')
useEffect(() => {
Expand All @@ -39,7 +42,15 @@ const Recent: React.FC<RecentProps> & { isRecent: boolean } = (props) => {
);
const handleOnChange = (value?: string | string[], opts?: OptionProps | OptionProps[]) =>
context?.onChange?.(value, opts);
return <List title={title} id={ITEM_KEY} options={listOptions} value="" onChange={handleOnChange} />;
if (!!listOptions?.length && model !== 'multiple') {
return (
<div className={`${selectionPrefixCls}--item`}>
{title && <div className={`${selectionPrefixCls}--title`}>{title}</div>}
<List title={title} id={ITEM_KEY} options={listOptions} value="" onChange={handleOnChange} />
</div>
);
}
return <></>;
};
Recent.isRecent = true;
export default Recent;
2 changes: 1 addition & 1 deletion src/list-picker/demos/List-picker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const Template: Story<ListPickerProps> = () => {
<Tabs value={activeTab} defaultValue="tab1" onChange={(key: string) => setActiveTab(key)}>
<Tab label="tab1" value="tab1">
<List.Selection>
<Recent title="最近使用" />
<Recent />
<List
id="group1"
title="分组1"
Expand Down
5 changes: 3 additions & 2 deletions src/list/Selection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const Selection: React.FC<SelectionProps> & { isSelection?: boolean } = (props)
contextSetOptions?.(opts);
};
const IsRenderSelectionItem = (node: nodeType) => {
if (node.type?.isRecent) {
return true;
if (node?.type?.isRecent) {
return false;
}
return (
!isEmpty(node?.props.options) ||
Expand Down Expand Up @@ -69,6 +69,7 @@ const Selection: React.FC<SelectionProps> & { isSelection?: boolean } = (props)
if (options.length) {
const renderOptionsChildren = (
<div className={classNames(prefixCls, className)} style={style}>
{toArray(children)?.map((node: nodeType) => renderContent(node))}
{isSelection &&
(selectionOptions as { groupId: string; groupName: string; options: OptionProps[] }[])?.map(
(option) =>
Expand Down

1 comment on commit 17e367c

@vercel
Copy link

@vercel vercel bot commented on 17e367c Dec 2, 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.