Skip to content

Commit

Permalink
fix(list): list mode=cascader onChange return value and options (#1779)
Browse files Browse the repository at this point in the history
  • Loading branch information
berber1016 committed Jan 7, 2022
1 parent ee271d4 commit 63a4e16
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
6 changes: 1 addition & 5 deletions src/list-picker/interfance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { OptionProps } from '../list/interfance';
import { Placement, TriggerAction } from '../popover/interface';
import { ListProps } from '../list';

export interface ListPickerProps extends Pick<ListProps, 'model' | 'empty' | 'needEmpty' | 'max'> {
export interface ListPickerProps extends Pick<ListProps, 'model' | 'empty' | 'needEmpty' | 'max' | 'valueSeparator'> {
size?: 'small' | 'normal';
/**
* 触发方式
Expand Down Expand Up @@ -67,10 +67,6 @@ export interface ListPickerProps extends Pick<ListProps, 'model' | 'empty' | 'ne
* cascader 级联文本连接符
*/
separator?: string;
/**
* value解析连接符 默认为 '.'
*/
valueSeparator?: string;
/**
* 是否允许clear
*/
Expand Down
6 changes: 4 additions & 2 deletions src/list-picker/listPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useRef, useState } from 'react';
import classNames from 'classnames';
import { isEqual, isNil } from 'lodash';
import { useLocale , usePrefixCls } from '@gio-design/utils';
import { useLocale, usePrefixCls } from '@gio-design/utils';
import { ListPickerProps } from './interfance';
import Popover from '../popover';
import Trigger from './Trigger';
Expand Down Expand Up @@ -61,7 +61,8 @@ export const ListPicker: React.FC<ListPickerProps> = (props) => {
const defaultPrefix = usePrefixCls(prefixCls);
const [visible, setVisible] = useControlledState(controlledVisible, false);
const [value, setValue] = useState(controlledValue || defaultValue);
const { options, setOptions, getOptionByValue, getLabelByValue, getOptionsByValue } = useCacheOptions();
const { options, setOptions, getOptionByValue, getLabelByValue, getOptionTreeByValue, getOptionsByValue } =
useCacheOptions();
const triggerRef = useRef<HTMLInputElement | undefined>(undefined);
useEffect(() => {
setValue(controlledValue);
Expand Down Expand Up @@ -172,6 +173,7 @@ export const ListPicker: React.FC<ListPickerProps> = (props) => {
getOptionByValue,
getOptionsByValue,
getLabelByValue,
getOptionTreeByValue,
emptyNode: empty,
isEmpty: needEmpty,
recentId: propsRecentId,
Expand Down
14 changes: 12 additions & 2 deletions src/list/List.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import classNames from 'classnames';
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { isArray, isEmpty } from 'lodash';
import { useLocale , usePrefixCls } from '@gio-design/utils';
import { useLocale, usePrefixCls } from '@gio-design/utils';
import { OptionProps, ListProps } from './interfance';
import { PREFIX } from './constants';
import Item from './Item';
Expand Down Expand Up @@ -36,6 +36,7 @@ export const InnerList = WithRef<HTMLDivElement, ListProps>((props, ref?) => {
empty,
max,
needEmpty = false,
valueSeparator,
...listRestProps
} = props;

Expand All @@ -52,6 +53,7 @@ export const InnerList = WithRef<HTMLDivElement, ListProps>((props, ref?) => {
disabled: contextDisabled,
onChange: contextOnChange,
setOptions: contextSetOptions,
getOptionTreeByValue: contextGetOptionTreeByValue,
isSelection,
} = context;
const mergedModel = useMemo(() => model ?? contextModel, [contextModel, model]);
Expand Down Expand Up @@ -108,7 +110,11 @@ export const InnerList = WithRef<HTMLDivElement, ListProps>((props, ref?) => {
}
// cascader
else if (mergedModel === 'cascader') {
onChange?.(val);
onChange?.(
val,
contextGetOptionTreeByValue?.(val, valueSeparator, mergedModel) ??
cache?.getOptionTreeByValue(val, valueSeparator, mergedModel)
);
}
// normal
else if (value !== val) {
Expand Down Expand Up @@ -185,6 +191,10 @@ export const InnerList = WithRef<HTMLDivElement, ListProps>((props, ref?) => {
emptyNode: mergedEmpty,
value,
disabled: mergedDisabled,
getLabelByValue: context.getLabelByValue ?? cache.getLabelByValue,
getOptionTreeByValue: context.getOptionTreeByValue ?? cache.getOptionTreeByValue,
getOptionByValue: context.getOptionByValue ?? cache.getOptionByValue,
getOptionsByValue: context.getOptionsByValue ?? cache.getOptionsByValue,
prefix,
suffix,
onChange,
Expand Down
2 changes: 2 additions & 0 deletions src/list/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface ListContextProps {
valueSeparator?: string,
model?: ModelType
) => any;
getOptionTreeByValue?: (val?: string | number, valueSeparator?: string, model?: string) => any;
}
const defaultList: ListContextProps = {
value: '',
Expand All @@ -46,6 +47,7 @@ const defaultList: ListContextProps = {
getOptionByValue: undefined,
getOptionsByValue: undefined,
getLabelByValue: undefined,
getOptionTreeByValue: undefined,
};
export const ListContext = React.createContext<ListContextProps>(defaultList);

Expand Down
4 changes: 4 additions & 0 deletions src/list/interfance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export interface ListProps {
itemStrategy?: 'fixed' | 'absolute';
empty?: React.ReactNode;
needEmpty?: boolean;
/**
* value解析连接符 默认为'.'
*/
valueSeparator?: string;
}

export interface DragListProps extends Omit<ListProps, 'model' | 'onChange' | 'value' | 'children'> {
Expand Down

1 comment on commit 63a4e16

@vercel
Copy link

@vercel vercel bot commented on 63a4e16 Jan 7, 2022

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.