Skip to content

Commit

Permalink
fix(list-picker): add recent id change localKey
Browse files Browse the repository at this point in the history
  • Loading branch information
berber1016 committed Dec 7, 2021
1 parent 1b9cfc8 commit 9019cab
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/list-picker/Recent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useLocale } from '@gio-design/utils';
import { slice } from 'lodash';
import { isNil, slice } from 'lodash';
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { List, OptionProps } from '../list';
import { PREFIX } from '../list/constants';
Expand All @@ -21,7 +21,8 @@ const Recent: React.FC<RecentProps> & { isRecent: boolean } = (props) => {
const selectionPrefixCls = `${usePrefixCls(PREFIX)}--selection`;
const context = useContext(ListContext);
const { options, model } = context;
const localStorageValue = window?.localStorage?.getItem(ITEM_KEY) || '[]';
const localKey = isNil(context?.recentId) ? ITEM_KEY : `${ITEM_KEY}_${context?.recentId}`;
const localStorageValue = window?.localStorage?.getItem(localKey) || '[]';
const matchValue = JSON.parse(localStorageValue); // localStorage.getItem('__GIO_SELECTION_KEY')
useEffect(() => {
setTimeout(() => {
Expand Down
1 change: 1 addition & 0 deletions src/list-picker/demos/List-picker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const Single = (args: ListPickerProps) => {
onClear={() => {
setValue('');
}}
recentId="test_2"
allowClear
placeholder="请选择"
>
Expand Down
4 changes: 4 additions & 0 deletions src/list-picker/interfance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export interface ListPickerProps extends Pick<ListProps, 'model'> {
* custom trigger render
*/
renderTrigger?: () => React.ReactElement;
/**
* recentId Recent取值
*/
recentId?: string;
}

// export interface StaticListPickerProps extends Omit<ListProps, 'options'> {
Expand Down
8 changes: 5 additions & 3 deletions src/list-picker/listPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ const ListPicker: React.FC<ListPickerProps> = (props) => {
triggerSuffix,
hidePrefix = false,
maxWidth,
recentId: propsRecentId,
} = props;
const defaultPrefix = usePrefixCls(prefixCls);
const [visible, setVisible] = useControlledState(controlledVisible, false);

const [value, setValue] = useState(controlledValue || defaultValue);
const { options, setOptions, getOptionByValue, getLabelByValue, getOptionsByValue } = useCacheOptions();

Expand All @@ -78,11 +78,12 @@ const ListPicker: React.FC<ListPickerProps> = (props) => {

const handleChange = (val?: string | string[], opts?: OptionProps | OptionProps[]) => {
if (model !== 'multiple') {
const localStorageValue = localStorage?.getItem(ITEM_KEY);
const localKey = isNil(propsRecentId) ? ITEM_KEY : `${ITEM_KEY}_${propsRecentId}`;
const localStorageValue = localStorage?.getItem(localKey);
const recentKey: string[] = (JSON.parse(isNil(localStorageValue) ? '[]' : localStorageValue) || []).filter(
(v: string) => v !== val
);
localStorage?.setItem(ITEM_KEY, JSON.stringify([val, ...recentKey].slice(0, 20)));
localStorage?.setItem(localKey, JSON.stringify([val, ...recentKey].slice(0, 50)));
onChange?.(val, opts);
handVisibleChange(false);
} else {
Expand Down Expand Up @@ -152,6 +153,7 @@ const ListPicker: React.FC<ListPickerProps> = (props) => {
getOptionByValue,
getOptionsByValue,
getLabelByValue,
recentId: propsRecentId,
}}
>
<Popover
Expand Down
3 changes: 3 additions & 0 deletions src/list/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export interface ListContextProps {
onChange?: (value?: MaybeArray<string | number>, options?: OptionProps | OptionProps[]) => void;
onClick?: (value?: string | number) => void;
options?: Map<string | number, OptionProps>;
recentId?: string;
prefix?: (option?: OptionProps) => string | React.ReactNode;
suffix?: (option?: OptionProps) => string | React.ReactNode;
setRecentId?: (value?: string) => void;
setOptions?: (options?: OptionProps[]) => void;
getOptionByValue?: (optValue?: string | number) => OptionProps | undefined;
getOptionsByValue?: (optValue?: MaybeArray<string | number>) => OptionProps | OptionProps[] | undefined;
Expand All @@ -26,6 +28,7 @@ const defaultList: ListContextProps = {
disabled: false,
selectParent: undefined,
isSelection: false,
recentId: undefined,
onChange: noop,
onClick: noop,
setOptions: noop,
Expand Down

0 comments on commit 9019cab

Please sign in to comment.