Skip to content

Commit 1a35959

Browse files
authored
fix: control panel not working (#462)
1 parent 40a1db1 commit 1a35959

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

examples/panel.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,33 @@ const addressOptions = [
5757
];
5858

5959
export default () => {
60+
const [value, setValue] = React.useState([]);
61+
6062
return (
6163
<>
6264
<h1>Panel</h1>
65+
<button
66+
onClick={() => {
67+
setValue(['bj', 'haidian']);
68+
}}
69+
>
70+
Set Value
71+
</button>
72+
<Cascader.Panel
73+
value={value}
74+
options={addressOptions}
75+
onChange={nextValue => {
76+
console.log('Change:', nextValue);
77+
setValue(nextValue);
78+
}}
79+
/>
80+
6381
<Cascader.Panel
6482
checkable
83+
value={value}
6584
options={addressOptions}
66-
onChange={value => {
67-
console.log('Change:', value);
85+
onChange={nextValue => {
86+
console.log('Change:', nextValue);
6887
}}
6988
/>
7089

src/OptionList/useActive.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ export default (
1010
): [React.Key[], (activeValueCells: React.Key[]) => void] => {
1111
const { values } = React.useContext(CascaderContext);
1212

13+
const firstValueCells = values[0];
14+
1315
// Record current dropdown active options
1416
// This also control the open status
1517
const [activeValueCells, setActiveValueCells] = React.useState<React.Key[]>([]);
1618

1719
React.useEffect(
1820
() => {
1921
if (open && !multiple) {
20-
const firstValueCells = values[0];
2122
setActiveValueCells(firstValueCells || []);
2223
}
2324
},
2425
/* eslint-disable react-hooks/exhaustive-deps */
25-
[open],
26+
[open, firstValueCells],
2627
/* eslint-enable react-hooks/exhaustive-deps */
2728
);
2829

tests/Panel.spec.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,11 @@ describe('Cascader.Panel', () => {
9292

9393
expect(container.querySelector('.rc-cascader-panel-empty').textContent).toEqual('Hello World');
9494
});
95+
96+
it('control', () => {
97+
const { container } = render(<Cascader.Panel options={options} value={['bamboo', 'little']} />);
98+
99+
const checkedLi = container.querySelector('[aria-checked="true"]');
100+
expect(checkedLi.textContent).toEqual('Little');
101+
});
95102
});

0 commit comments

Comments
 (0)