Skip to content

Commit

Permalink
fix: 删除重复声明
Browse files Browse the repository at this point in the history
  • Loading branch information
lihongxiangfrontend committed Nov 26, 2024
1 parent a29a6e4 commit 173d4de
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 17 deletions.
1 change: 1 addition & 0 deletions examples/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export default () => {
setValue2(nextValue);
}}
disabled={disabled}
defaultActiveKey={['bj', 'haidian']}
/>

<Cascader.Panel options={addressOptions} disabled={disabled} direction="rtl" />
Expand Down
4 changes: 2 additions & 2 deletions src/Cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export interface CascaderProps<
value: GetValueType<OptionType, ValueField, Multiple>,
selectOptions: OptionType[],
) => void;
defaultActiveValueCells?: React.Key[];
defaultActiveKey?: React.Key[];
}

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

export type CascaderRef = Omit<BaseSelectRef, 'scrollTo'>;
Expand Down
12 changes: 3 additions & 9 deletions src/OptionList/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type RawOptionListProps = Pick<
| 'direction'
| 'open'
| 'disabled'
> & { defaultActiveValueCells?: React.Key[]; };
> & { defaultActiveKey?: React.Key[]; };

const RawOptionList = React.forwardRef<RefOptionListProps, RawOptionListProps>((props, ref) => {
const {
Expand All @@ -41,7 +41,7 @@ const RawOptionList = React.forwardRef<RefOptionListProps, RawOptionListProps>((
direction,
open,
disabled,
defaultActiveValueCells,
defaultActiveKey,
} = props;

const containerRef = React.useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -106,7 +106,7 @@ const RawOptionList = React.forwardRef<RefOptionListProps, RawOptionListProps>((
const halfCheckedSet = React.useMemo(() => new Set(toPathKeys(halfValues)), [halfValues]);

// ====================== Accessibility =======================
const [activeValueCells, setActiveValueCells] = useActive(multiple, open);
const [activeValueCells, setActiveValueCells] = useActive(multiple, open, defaultActiveKey);

// =========================== Path ===========================
const onPathOpen = (nextValueCells: React.Key[]) => {
Expand All @@ -116,12 +116,6 @@ const RawOptionList = React.forwardRef<RefOptionListProps, RawOptionListProps>((
internalLoadData(nextValueCells);
};

React.useEffect(() => {
if (defaultActiveValueCells && defaultActiveValueCells?.length > 0) {
setActiveValueCells(defaultActiveValueCells)
}
}, [defaultActiveValueCells]);

const isSelectable = (option: DefaultOptionType) => {
if (disabled) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/OptionList/useActive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import CascaderContext from '../context';
const useActive = (
multiple?: boolean,
open?: boolean,
defaultActiveKey?: React.Key[],
): [React.Key[], (activeValueCells: React.Key[]) => void] => {
const { values } = React.useContext(CascaderContext);

const firstValueCells = values[0];

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

React.useEffect(
() => {
Expand Down
2 changes: 1 addition & 1 deletion src/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function Panel<
direction,
notFoundContent = 'Not Found',
disabled,
defaultActiveValueCells
defaultActiveKey,
} = props as Pick<InternalCascaderProps, PickType>;

// ======================== Multiple ========================
Expand Down
4 changes: 2 additions & 2 deletions tests/Panel.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ describe('Cascader.Panel', () => {
expect(onChange).toHaveBeenCalledWith([['bamboo', 'little']], expect.anything());
});

it('multiple with defaultActiveValueCells', () => {
it('multiple with defaultActiveKey', () => {
const onChange = jest.fn();
const { container } = render(
<Cascader.Panel
checkable
options={options}
onChange={onChange}
defaultActiveValueCells={['bamboo', 'little']}
defaultActiveKey={['bamboo', 'little']}
/>,
);
expect(container.querySelectorAll('.rc-cascader-menu')).toHaveLength(2);
Expand Down
4 changes: 2 additions & 2 deletions tests/selector.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import Cascader from '../src';
import { addressOptions } from './demoOptions';

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

(global as any).activeValueCells = activeValueCells;

Expand Down

0 comments on commit 173d4de

Please sign in to comment.