Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Oct 4, 2024
1 parent 9e41610 commit 130a287
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 55 deletions.
53 changes: 10 additions & 43 deletions packages/x-date-pickers/src/internals/hooks/useField/useField.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import * as React from 'react';
import { PickerAnyValueManagerV8, PickerManagerProperties } from '../../../models';
import { useLocalizationContext } from '../useUtils';
import {
UseFieldWithKnownDOMStructureParameters,
UseFieldResponse,
UseFieldForwardedProps,
} from './useField.types';
import { UseFieldResponse, UseFieldForwardedProps, UseFieldParameters } from './useField.types';
import { useFieldAccessibleDOMStructure } from './useFieldAccessibleDOMStructure';
import { useFieldLegacyDOMStructure } from './useFieldLegacyDOMStructure';

Expand All @@ -20,41 +14,14 @@ export const useField = <
PickerManagerProperties<TManager>['enableAccessibleFieldDOMStructure'],
TForwardedProps
> => {
const { valueManager, forwardedProps, internalProps } = parameters;
const useFieldWithKnownDOMStructure = (parameters.internalProps.enableAccessibleFieldDOMStructure
? useFieldAccessibleDOMStructure
: useFieldLegacyDOMStructure) as unknown as (
params: UseFieldParameters<TManager, TForwardedProps>,
) => UseFieldResponse<
PickerManagerProperties<TManager>['enableAccessibleFieldDOMStructure'],
TForwardedProps
>;

const localizationContext = useLocalizationContext<PickerManagerProperties<TManager>['date']>();

const internalPropsWithDefaults = React.useMemo(
() =>
valueManager.applyDefaultsToFieldInternalProps({
...localizationContext,
internalProps,
}),
[valueManager, localizationContext, internalProps],
);

const useFieldWithKnownDOMStructure = (
internalPropsWithDefaults.enableAccessibleFieldDOMStructure
? useFieldAccessibleDOMStructure
: useFieldLegacyDOMStructure
) as (
params: UseFieldWithKnownDOMStructureParameters<TManager, TForwardedProps>,
) => UseFieldResponse<true, TForwardedProps>;

return useFieldWithKnownDOMStructure({
valueManager,
forwardedProps,
internalPropsWithDefaults,
});
return useFieldWithKnownDOMStructure(parameters);
};

export interface UseFieldParameters<
TManager extends PickerAnyValueManagerV8,
TForwardedProps extends UseFieldForwardedProps<
PickerManagerProperties<TManager>['enableAccessibleFieldDOMStructure']
>,
> {
valueManager: TManager;
forwardedProps: TForwardedProps;
internalProps: PickerManagerProperties<TManager>['internalProps'];
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import {
import type { PickersSectionElement } from '../../../PickersSectionList';
import { ExportedUseClearableFieldProps } from '../../../hooks/useClearableField';

export interface UseFieldWithKnownDOMStructureParameters<
export interface UseFieldParameters<
TManager extends PickerAnyValueManagerV8,
TForwardedProps extends UseFieldForwardedProps<
PickerManagerProperties<TManager>['enableAccessibleFieldDOMStructure']
>,
> {
valueManager: TManager;
forwardedProps: TForwardedProps;
internalPropsWithDefaults: PickerManagerProperties<TManager>['internalPropsWithDefaults'];
internalProps: PickerManagerProperties<TManager>['internalProps'];
}

export interface UseFieldInternalProps<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as React from 'react';
import useForkRef from '@mui/utils/useForkRef';
import { PickerAnyAccessibleValueManagerV8 } from '../../../models';
import { PickerAnyAccessibleValueManagerV8, PickerManagerProperties } from '../../../models';
import {
UseFieldAccessibleDOMGetters,
UseFieldForwardedProps,
UseFieldWithKnownDOMStructureParameters,
UseFieldParameters,
UseFieldResponse,
} from './useField.types';
import type { PickersSectionElement } from '../../../PickersSectionList';
Expand All @@ -17,21 +17,32 @@ import { useFieldAccessibleContainerProps } from './useFieldAccessibleContainerP
import { useFieldAccessibleSectionContentProps } from './useFieldAccessibleSectionContentProps';
import { useFieldAccessibleSectionContainerProps } from './useFieldAccessibleSectionContainerProps';
import { useFieldAccessibleHiddenInputProps } from './useFieldAccessibleHiddenInputProps';
import { useLocalizationContext } from '../useUtils';

export const useFieldAccessibleDOMStructure = <
TManager extends PickerAnyAccessibleValueManagerV8,
TForwardedProps extends UseFieldForwardedProps<true>,
>(
parameters: UseFieldWithKnownDOMStructureParameters<TManager, TForwardedProps>,
parameters: UseFieldParameters<TManager, TForwardedProps>,
): UseFieldResponse<true, TForwardedProps> => {
const {
internalPropsWithDefaults,
internalPropsWithDefaults: { disabled, readOnly = false },
internalProps,
forwardedProps,
forwardedProps: { sectionListRef: sectionListRefProp, focused: focusedProp, autoFocus = false },
valueManager,
} = parameters;

const localizationContext = useLocalizationContext<PickerManagerProperties<TManager>['date']>();

const internalPropsWithDefaults = React.useMemo(
() =>
valueManager.applyDefaultsToFieldInternalProps({
...localizationContext,
internalProps,
}),
[valueManager, localizationContext, internalProps],
);

// Management of `sectionListRef` (won't be present in `PickersField`)
const sectionListRef = React.useRef<UseFieldAccessibleDOMGetters>(null);
const handleSectionListRef = useForkRef(sectionListRefProp, sectionListRef);
Expand Down Expand Up @@ -158,7 +169,7 @@ export const useFieldAccessibleDOMStructure = <
enableAccessibleFieldDOMStructure: true,
elements,
areAllSectionsEmpty: stateResponse.areAllSectionsEmpty,
disabled,
readOnly,
disabled: internalProps.disabled,
readOnly: internalProps.readOnly,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import {
UseFieldWithKnownDOMStructureParameters,
UseFieldResponse,
} from './useField.types';
import { FieldSection, PickerAnyValueManagerV8 } from '../../../models';
import { FieldSection, PickerAnyValueManagerV8, PickerManagerProperties } from '../../../models';
import { getActiveElement } from '../../utils/utils';
import { buildDefaultSectionOrdering, getSectionVisibleValue } from './useField.utils';
import { useFieldHandleKeyDown } from './useFieldHandleKeyDown';
import { useFieldClearValueProps } from './useFieldClearValueProps';
import { useFieldState } from './useFieldState';
import { useFieldCharacterEditing } from './useFieldCharacterEditing';
import { useFieldValidation } from './useFieldValidation';
import { useLocalizationContext } from '../useUtils';

type FieldSectionWithPositions<TSection> = TSection & {
/**
Expand Down Expand Up @@ -160,6 +161,7 @@ export const useFieldLegacyDOMStructure = <
const isRtl = useRtl();
const focusTimeoutRef = React.useRef<ReturnType<typeof setTimeout>>();
const selectionSyncTimeoutRef = React.useRef<ReturnType<typeof setTimeout>>();
const localizationContext = useLocalizationContext<PickerManagerProperties<TManager>['date']>();

const {
forwardedProps,
Expand All @@ -171,12 +173,22 @@ export const useFieldLegacyDOMStructure = <
inputRef: inputRefProp,
placeholder: placeholderProp,
},
internalPropsWithDefaults,
internalPropsWithDefaults: { unstableFieldRef, readOnly = false, disabled = false },
internalProps,
valueManager,
valueManager: { fieldValueManager, legacyValueManager },
} = parameters;

const internalPropsWithDefaults = React.useMemo(
() =>
valueManager.applyDefaultsToFieldInternalProps({
...localizationContext,
internalProps,
}),
[valueManager, localizationContext, internalProps],
);

const { unstableFieldRef, readOnly = false, disabled = false } = internalPropsWithDefaults;

const inputRef = React.useRef<HTMLInputElement>(null);
const handleRef = useForkRef(inputRefProp, inputRef);

Expand Down

0 comments on commit 130a287

Please sign in to comment.