Skip to content

Commit 0b2c795

Browse files
refactor: Panel 默认展开属性重命名 & 移除在effect中使用 defaultActiveKey
1 parent f0c9f4c commit 0b2c795

File tree

7 files changed

+18
-31
lines changed

7 files changed

+18
-31
lines changed

examples/panel.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ export default () => {
6161

6262
const [value2, setValue2] = React.useState<string[][]>([]);
6363

64-
const [defaultActiveValueCells, setDefaultActiveValueCells] = React.useState<string[]>([]);
65-
6664
return (
6765
<>
6866
<h1>Panel</h1>
@@ -82,22 +80,16 @@ export default () => {
8280
}}
8381
/>
8482

85-
<button
86-
onClick={() => {
87-
setDefaultActiveValueCells(['bj', 'haidian']);
88-
}}
89-
>
90-
Set defaultActiveValueCells
91-
</button>
83+
<div>defaultActiveKey=[bj, haidian]</div>
9284
<Cascader.Panel
9385
checkable
9486
value={value2}
9587
options={addressOptions}
9688
onChange={nextValue => {
97-
console.log('Change:', nextValue);
98-
setValue2(nextValue);
89+
console.log('Change:', nextValue);
90+
setValue2(nextValue);
9991
}}
100-
defaultActiveValueCells={defaultActiveValueCells}
92+
defaultActiveKey={['bj', 'haidian']}
10193
/>
10294

10395
<Cascader.Panel options={addressOptions} direction="rtl" />

src/Cascader.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export interface CascaderProps<
159159
value: GetValueType<OptionType, ValueField, Multiple>,
160160
selectOptions: OptionType[],
161161
) => void;
162-
defaultActiveValueCells?: React.Key[];
162+
defaultActiveKey?: React.Key[];
163163
}
164164

165165
export type SingleValueType = (string | number)[];
@@ -176,7 +176,7 @@ export type InternalCascaderProps = Omit<CascaderProps, 'onChange' | 'value' | '
176176
value: InternalValueType,
177177
selectOptions: BaseOptionType[] | BaseOptionType[][],
178178
) => void;
179-
defaultActiveValueCells?: React.Key[];
179+
defaultActiveKey?: React.Key[];
180180
};
181181

182182
export type CascaderRef = Omit<BaseSelectRef, 'scrollTo'>;

src/OptionList/List.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import useKeyboard from './useKeyboard';
2222
export type RawOptionListProps = Pick<
2323
ReturnType<typeof useBaseProps>,
2424
'prefixCls' | 'multiple' | 'searchValue' | 'toggleOpen' | 'notFoundContent' | 'direction' | 'open'
25-
> & { defaultActiveValueCells?: React.Key[]; };
25+
> & { defaultActiveKey?: React.Key[]; };
2626

2727
const RawOptionList = React.forwardRef<RefOptionListProps, RawOptionListProps>((props, ref) => {
28-
const { prefixCls, multiple, searchValue, toggleOpen, notFoundContent, direction, open, defaultActiveValueCells } = props;
28+
const { prefixCls, multiple, searchValue, toggleOpen, notFoundContent, direction, open, defaultActiveKey } = props;
2929

3030
const containerRef = React.useRef<HTMLDivElement>(null);
3131
const rtl = direction === 'rtl';
@@ -89,7 +89,7 @@ const RawOptionList = React.forwardRef<RefOptionListProps, RawOptionListProps>((
8989
const halfCheckedSet = React.useMemo(() => new Set(toPathKeys(halfValues)), [halfValues]);
9090

9191
// ====================== Accessibility =======================
92-
const [activeValueCells, setActiveValueCells] = useActive(multiple, open);
92+
const [activeValueCells, setActiveValueCells] = useActive(multiple, open, defaultActiveKey);
9393

9494
// =========================== Path ===========================
9595
const onPathOpen = (nextValueCells: React.Key[]) => {
@@ -99,12 +99,6 @@ const RawOptionList = React.forwardRef<RefOptionListProps, RawOptionListProps>((
9999
internalLoadData(nextValueCells);
100100
};
101101

102-
React.useEffect(() => {
103-
if (defaultActiveValueCells && defaultActiveValueCells?.length > 0) {
104-
setActiveValueCells(defaultActiveValueCells)
105-
}
106-
}, [defaultActiveValueCells]);
107-
108102
const isSelectable = (option: DefaultOptionType) => {
109103
const { disabled } = option;
110104

src/OptionList/useActive.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import CascaderContext from '../context';
77
const useActive = (
88
multiple?: boolean,
99
open?: boolean,
10+
defaultActiveKey?: React.Key[],
1011
): [React.Key[], (activeValueCells: React.Key[]) => void] => {
1112
const { values } = React.useContext(CascaderContext);
1213

1314
const firstValueCells = values[0];
1415

1516
// Record current dropdown active options
1617
// This also control the open status
17-
const [activeValueCells, setActiveValueCells] = React.useState<React.Key[]>([]);
18+
const [activeValueCells, setActiveValueCells] = React.useState<React.Key[]>(defaultActiveKey ?? []);
1819

1920
React.useEffect(
2021
() => {

src/Panel.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export type PickType =
3636
| 'style'
3737
| 'direction'
3838
| 'notFoundContent'
39-
| 'defaultActiveValueCells';
39+
| 'defaultActiveKey';
4040

4141
export type PanelProps<
4242
OptionType extends DefaultOptionType = DefaultOptionType,
@@ -69,7 +69,7 @@ export default function Panel<
6969
loadingIcon,
7070
direction,
7171
notFoundContent = 'Not Found',
72-
defaultActiveValueCells
72+
defaultActiveKey
7373
} = props as Pick<InternalCascaderProps, PickType>;
7474

7575
// ======================== Multiple ========================
@@ -202,7 +202,7 @@ export default function Panel<
202202
toggleOpen={noop}
203203
open
204204
direction={direction}
205-
defaultActiveValueCells={defaultActiveValueCells}
205+
defaultActiveKey={defaultActiveKey}
206206
/>
207207
)}
208208
</div>

tests/Panel.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ describe('Cascader.Panel', () => {
7979
expect(onChange).toHaveBeenCalledWith([['bamboo', 'little']], expect.anything());
8080
});
8181

82-
it('multiple with defaultActiveValueCells', () => {
82+
it('multiple with defaultActiveKey', () => {
8383
const onChange = jest.fn();
8484
const { container } = render(
8585
<Cascader.Panel
8686
checkable
8787
options={options}
8888
onChange={onChange}
89-
defaultActiveValueCells={['bamboo', 'little']}
89+
defaultActiveKey={['bamboo', 'little']}
9090
/>,
9191
);
9292
expect(container.querySelectorAll('.rc-cascader-menu')).toHaveLength(2);

tests/selector.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import Cascader from '../src';
55
import { addressOptions } from './demoOptions';
66

77
// Mock `useActive` hook
8-
jest.mock('../src/OptionList/useActive', () => (multiple: boolean, open: boolean) => {
8+
jest.mock('../src/OptionList/useActive', () => (multiple: boolean, open: boolean, defaultActiveKey: React.Key[]) => {
99
// Pass to origin hooks
1010
const originHook = jest.requireActual('../src/OptionList/useActive').default;
11-
const [activeValueCells, setActiveValueCells] = originHook(multiple, open);
11+
const [activeValueCells, setActiveValueCells] = originHook(multiple, open, defaultActiveKey);
1212

1313
(global as any).activeValueCells = activeValueCells;
1414

0 commit comments

Comments
 (0)