Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support dropdownStyle and merge dropdownMenuColumnStyle to styles prop #413

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions src/Cascader.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { BuildInPlacements } from '@rc-component/trigger/lib/interface';
import type { BaseSelectProps, BaseSelectPropsWithoutPrivate, BaseSelectRef } from 'rc-select';
import type { BaseSelectProps,BaseSelectPropsWithoutPrivate,BaseSelectRef } from 'rc-select';
import { BaseSelect } from 'rc-select';
import type { DisplayValueType, Placement } from 'rc-select/lib/BaseSelect';
import type { DisplayValueType,Placement } from 'rc-select/lib/BaseSelect';
import useId from 'rc-select/lib/hooks/useId';
import { conductCheck } from 'rc-tree/lib/utils/conductUtil';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
Expand All @@ -14,9 +14,9 @@ import useRefFunc from './hooks/useRefFunc';
import useSearchConfig from './hooks/useSearchConfig';
import useSearchOptions from './hooks/useSearchOptions';
import OptionList from './OptionList';
import { fillFieldNames, SHOW_CHILD, SHOW_PARENT, toPathKey, toPathKeys } from './utils/commonUtil';
import { formatStrategyValues, toPathOptions } from './utils/treeUtil';
import warningProps, { warningNullOptions } from './utils/warningPropsUtil';
import { fillFieldNames,SHOW_CHILD,SHOW_PARENT,toPathKey,toPathKeys } from './utils/commonUtil';
import { formatStrategyValues,toPathOptions } from './utils/treeUtil';
import warningProps,{ warningNullOptions } from './utils/warningPropsUtil';

export interface ShowSearchType<OptionType extends BaseOptionType = DefaultOptionType> {
filter?: (inputValue: string, options: OptionType[], fieldNames: FieldNames) => boolean;
Expand Down Expand Up @@ -57,7 +57,7 @@ export interface DefaultOptionType extends BaseOptionType {
disableCheckbox?: boolean;
}

interface BaseCascaderProps<OptionType extends BaseOptionType = DefaultOptionType>
export interface BaseCascaderProps<OptionType extends BaseOptionType = DefaultOptionType>
extends Omit<
BaseSelectPropsWithoutPrivate,
'tokenSeparators' | 'labelInValue' | 'mode' | 'showSearch'
Expand Down Expand Up @@ -97,8 +97,15 @@ interface BaseCascaderProps<OptionType extends BaseOptionType = DefaultOptionTyp
/** @deprecated Use `dropdownClassName` instead */
popupClassName?: string;
dropdownClassName?: string;
/** @deprecated Use `styles.dropdownMenuColumn` instead */
BoyYangzai marked this conversation as resolved.
Show resolved Hide resolved
dropdownMenuColumnStyle?: React.CSSProperties;

// styles
styles?: {
dropdown?: React.CSSProperties;
BoyYangzai marked this conversation as resolved.
Show resolved Hide resolved
dropdownMenuColumn?: React.CSSProperties;
};

/** @deprecated Use `placement` instead */
popupPlacement?: Placement;
placement?: Placement;
Expand Down Expand Up @@ -197,8 +204,11 @@ const Cascader = React.forwardRef<CascaderRef, InternalCascaderProps>((props, re
popupVisible,
open,

styles,

popupClassName,
dropdownClassName,
dropdownStyle,
dropdownMenuColumnStyle,

popupPlacement,
Expand Down Expand Up @@ -448,7 +458,7 @@ const Cascader = React.forwardRef<CascaderRef, InternalCascaderProps>((props, re
expandTrigger,
expandIcon,
loadingIcon,
dropdownMenuColumnStyle,
dropdownMenuColumnStyle: styles?.dropdownMenuColumn ?? dropdownMenuColumnStyle,
}),
[
mergedOptions,
Expand All @@ -465,6 +475,7 @@ const Cascader = React.forwardRef<CascaderRef, InternalCascaderProps>((props, re
expandIcon,
loadingIcon,
dropdownMenuColumnStyle,
styles?.dropdownMenuColumn,
],
);

Expand All @@ -473,14 +484,21 @@ const Cascader = React.forwardRef<CascaderRef, InternalCascaderProps>((props, re
// ==============================================================
const emptyOptions = !(mergedSearchValue ? searchOptions : mergedOptions).length;

const dropdownStyle: React.CSSProperties =
const mergedDropdownStyle: React.CSSProperties =
// Search to match width
(mergedSearchValue && searchConfig.matchInputWidth) ||
// Empty keep the width
styles?.dropdown ??
dropdownStyle ??
(mergedSearchValue && searchConfig.matchInputWidth) ??
emptyOptions
? {}
? // Empty keep the width
{
...styles?.dropdown,
...dropdownStyle,
}
: {
minWidth: 'auto',
BoyYangzai marked this conversation as resolved.
Show resolved Hide resolved
...styles?.dropdown,
...dropdownStyle,
};

return (
Expand All @@ -492,7 +510,7 @@ const Cascader = React.forwardRef<CascaderRef, InternalCascaderProps>((props, re
id={mergedId}
prefixCls={prefixCls}
dropdownMatchSelectWidth={dropdownMatchSelectWidth}
dropdownStyle={dropdownStyle}
dropdownStyle={mergedDropdownStyle}
// Value
displayValues={displayValues}
onDisplayValuesChange={onDisplayValuesChange}
Expand Down
10 changes: 7 additions & 3 deletions src/utils/warningPropsUtil.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import warning from 'rc-util/lib/warning';
import type { DefaultOptionType, FieldNames, InternalCascaderProps } from '../Cascader';
import type { DefaultOptionType, FieldNames, BaseCascaderProps } from '../Cascader';

function warningProps(props: InternalCascaderProps) {
const { onPopupVisibleChange, popupVisible, popupClassName, popupPlacement } = props;
function warningProps(props: BaseCascaderProps) {
const { onPopupVisibleChange, popupVisible, popupClassName, popupPlacement,dropdownMenuColumnStyle} = props;

warning(
!onPopupVisibleChange,
Expand All @@ -17,6 +17,10 @@ function warningProps(props: InternalCascaderProps) {
popupPlacement === undefined,
'`popupPlacement` is deprecated. Please use `placement` instead.',
);
warning(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面还有 popupXX 废弃请用 dropdown 的wanring 也需要清理掉

dropdownMenuColumnStyle === undefined,
'`dropdownMenuColumnStyle` is deprecated. Please use `styles.dropdownMenuColumn` instead.',
);
}

// value in Cascader options should not be null
Expand Down
31 changes: 26 additions & 5 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import { resetWarned } from 'rc-util/lib/warning';
import React from 'react';
import Cascader from '../src';
import { addressOptions, addressOptionsForUneven, optionsForActiveMenuItems } from './demoOptions';
import { addressOptions,addressOptionsForUneven,optionsForActiveMenuItems } from './demoOptions';
import { mount } from './enzyme';

describe('Cascader.Basic', () => {
Expand Down Expand Up @@ -655,7 +655,8 @@ describe('Cascader.Basic', () => {
changeOnSelect
expandTrigger="hover"
options={addressOptionsForUneven}
onChange={onChange}>
onChange={onChange}
>
<input readOnly />
</Cascader>,
);
Expand Down Expand Up @@ -684,7 +685,7 @@ describe('Cascader.Basic', () => {
wrapper.update();
expect(selectedValue).toBeFalsy();
expect(wrapper.isOpen()).toBeTruthy();
})
});

describe('focus test', () => {
let domSpy;
Expand Down Expand Up @@ -803,9 +804,8 @@ describe('Cascader.Basic', () => {
expect(activeItems).toHaveLength(2);
expect(activeItems.last().text()).toEqual('高雄');
});
})
});
});

});

it('defaultValue not exist', () => {
Expand Down Expand Up @@ -1063,4 +1063,25 @@ describe('Cascader.Basic', () => {
);
errorSpy.mockReset();
});

it('`dropdownMenuColumnStyle`in Cascader options should throw a warning', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => null);
mount(<Cascader dropdownMenuColumnStyle={{}} options={[]} />);

expect(errorSpy).toHaveBeenCalledWith(
'Warning: `dropdownMenuColumnStyle` is deprecated. Please use `styles.dropdownMenuColumn` instead.',
);
errorSpy.mockReset();
});

it('`dropdownMenuColumnStyle`in Cascader options should throw a warning', () => {
const wrapper = mount(<Cascader
styles={{ dropdown: { backgroundColor: 'red' }, dropdownMenuColumn: { backgroundColor: 'blue' } }}
options={[]}
open
/>);
expect(wrapper.find('.rc-cascader-dropdown').props().style.backgroundColor).toEqual('red');
expect(wrapper.find('.rc-cascader-menu-item').props().style.backgroundColor).toEqual('blue');
});

});