Skip to content

Commit

Permalink
[pickers] Fix controlled view behavior (@LukasTy) (#13559)
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas <llukas.tyla@gmail.com>
Co-authored-by: Lukas <llukas.tyla@gmail.com>
  • Loading branch information
github-actions[bot] and LukasTy authored Jun 20, 2024
1 parent 2dcc38c commit 35aeef0
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 @@ -135,6 +135,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 @@ -164,12 +164,12 @@ export const usePickerViews = <
TAdditionalProps
>): UsePickerViewsResponse<TView> => {
const { onChange, open, onSelectedSectionsChange, 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 35aeef0

Please sign in to comment.