Skip to content

Commit

Permalink
[pickers] Fix controlled view behavior (#13552)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasTy authored Jun 20, 2024
1 parent 83b2695 commit 0b0f3b4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ describe('<DesktopDatePicker />', () => {
fireEvent.click(screen.getByText('2020'));
expect(document.activeElement).to.have.text('5');
});

it('should go to the relevant `view` when `view` prop changes', () => {
const { setProps } = render(
<DesktopDatePicker
defaultValue={adapterToUse.date('2018-01-01')}
views={['year', 'month', 'day']}
view="month"
/>,
);

openPicker({ type: 'date', variant: 'desktop' });

expect(screen.getByRole('radio', { checked: true, name: 'January' })).to.not.equal(null);

// Dismiss the picker
userEvent.keyPress(document.activeElement!, { key: 'Escape' });
setProps({ view: 'year' });
openPicker({ type: 'date', variant: 'desktop' });
// wait for all pending changes to be flushed
clock.runToLast();

// should have changed the open view
expect(screen.getByRole('radio', { checked: true, name: '2018' })).to.not.equal(null);
});
});

describe('scroll', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ export const usePickerViews = <
TAdditionalProps
>): UsePickerViewsResponse<TView> => {
const { onChange, open, onClose } = propsFromPickerValue;
const { views, openTo, onViewChange, viewRenderers, timezone } = props;
const { view: inView, views, openTo, onViewChange, viewRenderers, timezone } = props;
const { className, sx, ...propsToForwardToView } = props;

const { view, setView, defaultView, focusedView, setFocusedView, setValueAndGoToNextView } =
useViews({
view: undefined,
view: inView,
views,
openTo,
onChange,
Expand Down

0 comments on commit 0b0f3b4

Please sign in to comment.