From a04e420383d95bc376e427a5076f7192a69c4f73 Mon Sep 17 00:00:00 2001 From: noraleonte Date: Thu, 25 May 2023 00:21:35 +0300 Subject: [PATCH] WIP hook and event propagation --- .../src/DateField/DateField.tsx | 35 +++++++++++++++---- .../src/internals/hooks/useField/useField.ts | 18 ++++++---- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/packages/x-date-pickers/src/DateField/DateField.tsx b/packages/x-date-pickers/src/DateField/DateField.tsx index 4c028be57abbc..3b265ea6392ee 100644 --- a/packages/x-date-pickers/src/DateField/DateField.tsx +++ b/packages/x-date-pickers/src/DateField/DateField.tsx @@ -8,6 +8,26 @@ import { useSlotProps } from '@mui/base/utils'; import { DateFieldProps } from './DateField.types'; import { useDateField } from './useDateField'; +const useClearEndAdornment = ({ + clearable, + InputProps, + onClear, + clearIcon = , +}) => { + return { + endAdornment: clearable ? ( + + {InputProps?.endAdornment} + + {clearIcon} + + + ) : ( + InputProps?.endAdornment + ), + }; +}; + type DateFieldComponent = (( props: DateFieldProps & React.RefAttributes, ) => JSX.Element) & { propTypes?: any }; @@ -51,18 +71,21 @@ const DateField = React.forwardRef(function DateField( inputRef: externalInputRef, }); + console.log(fieldProps); + + const ProcessedInputProps = useClearEndAdornment({ + onClear, + clearable, + InputProps: fieldProps.InputProps, + }); + return ( - - - ) : undefined, }} inputProps={{ ...fieldProps.inputProps, inputMode, onPaste, ref: inputRef }} /> diff --git a/packages/x-date-pickers/src/internals/hooks/useField/useField.ts b/packages/x-date-pickers/src/internals/hooks/useField/useField.ts index 0fe5961f7be32..3135012a194f9 100644 --- a/packages/x-date-pickers/src/internals/hooks/useField/useField.ts +++ b/packages/x-date-pickers/src/internals/hooks/useField/useField.ts @@ -143,13 +143,13 @@ export const useField = < }); const handleInputBlur = useEventCallback((event: React.FocusEvent, ...args) => { - const isClearButton: Boolean = Boolean( + const isClearButton = Boolean( event.relatedTarget?.className?.split(' ')?.includes('deleteIcon'), ); - if (!isClearButton) { - onBlur?.(event, ...(args as [])); - setSelectedSections(null); - } + // if (!isClearButton) { + onBlur?.(event, ...(args as [])); + setSelectedSections(null); + // } }); const handleInputPaste = useEventCallback((event: React.ClipboardEvent) => { @@ -480,13 +480,17 @@ export const useField = < setSelectedSections: (activeSectionIndex) => setSelectedSections(activeSectionIndex), })); - const handleClearValue = (...args) => { + const handleClearValue = (event, ...args) => { + event.stopPropagation(); + event.preventDefault(); onClear?.(...(args as [])); clearValue(); - inputRef?.current?.focus(); setSelectedSections(0); + inputRef?.current?.focus(); }; + console.log(selectedSectionIndexes); + return { placeholder, autoComplete: 'off',