Skip to content

Commit

Permalink
fix: picker change won't change panel (#121)
Browse files Browse the repository at this point in the history
* fix: picker change won't change type

* fix: test
  • Loading branch information
07akioni committed Aug 10, 2020
1 parent 15ca05d commit d8d6e6b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/switchType.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { useState } from 'react';
import { Moment } from 'moment';
import Picker from '../src/Picker';
import momentGenerateConfig from '../src/generate/moment';
import zhCN from '../src/locale/zh_CN';
import '../assets/index.less';

const sharedProps = {
generateConfig: momentGenerateConfig,
};

function PickerWithType({ type, onChange }) {
if (type === 'date') return <Picker<Moment> {...sharedProps} onChange={onChange} locale={zhCN} />;
return <Picker<Moment> {...sharedProps} picker={type} onChange={onChange} locale={zhCN} />;
}

export default function SwitchablePicker() {
const [type, setType] = useState('date');
return (
<>
<select
value={type}
onChange={event => {
setType(event.target.value);
}}
>
<option value="date">Date</option>
<option value="week">Week</option>
<option value="month">Month</option>
<option value="quarter">Quarter</option>
<option value="year">Year</option>
</select>
<PickerWithType type={type} onChange={value => console.log(value)} />
</>
);
}
4 changes: 4 additions & 0 deletions src/PickerPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ function PickerPanel<DateType>(props: PickerPanelProps<DateType>) {
},
);

React.useEffect(() => {
setInnerMode(picker);
}, [picker]);

const [sourceMode, setSourceMode] = React.useState<PanelMode>(() => mergedMode);

const onInternalPanelChange = (newMode: PanelMode | null, viewValue: DateType) => {
Expand Down
9 changes: 9 additions & 0 deletions tests/picker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,15 @@ describe('Picker.Basic', () => {
expect(wrapper.render()).toMatchSnapshot();
});

it('change panel when `picker` changed', () => {
const wrapper = mount(<MomentPicker open picker="week" />);
expect(wrapper.find('.rc-picker-week-panel').length).toEqual(1);
wrapper.setProps({ picker: 'month' });
wrapper.update();
expect(wrapper.find('.rc-picker-week-panel').length).toEqual(0);
expect(wrapper.find('.rc-picker-month-panel').length).toEqual(1);
});

describe('hover value', () => {
it('should restore when leave', () => {
const wrapper = mount(<MomentPicker open defaultValue={getMoment('2020-07-22')} />);
Expand Down

0 comments on commit d8d6e6b

Please sign in to comment.