diff --git a/packages/x-date-pickers/src/internals/hooks/useField/useField.test.tsx b/packages/x-date-pickers/src/internals/hooks/useField/useField.test.tsx index 760e445b9477b..c78f3037f089c 100644 --- a/packages/x-date-pickers/src/internals/hooks/useField/useField.test.tsx +++ b/packages/x-date-pickers/src/internals/hooks/useField/useField.test.tsx @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { getSectionVisibleValue } from './useField.utils'; +import { getSectionVisibleValue, parseSelectedSections } from './useField.utils'; const COMMON_PROPERTIES = { startSeparator: '', @@ -56,4 +56,10 @@ describe('useField utility functions', () => { ).to.equal('\u20681\u2069'); }); }); + + describe('parseSelectedSections', () => { + it('should return null when selectedSections is not available in sections', () => { + expect(parseSelectedSections('year', [])).to.equal(null); + }); + }); }); diff --git a/packages/x-date-pickers/src/internals/hooks/useField/useField.utils.ts b/packages/x-date-pickers/src/internals/hooks/useField/useField.utils.ts index e56f7029886f7..4713cb93bca7a 100644 --- a/packages/x-date-pickers/src/internals/hooks/useField/useField.utils.ts +++ b/packages/x-date-pickers/src/internals/hooks/useField/useField.utils.ts @@ -812,7 +812,8 @@ export const parseSelectedSections = ( } if (typeof selectedSections === 'string') { - return sections.findIndex((section) => section.type === selectedSections); + const index = sections.findIndex((section) => section.type === selectedSections); + return index === -1 ? null : index; } return selectedSections;