-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hover state and separate hook for endAdornment
- Loading branch information
1 parent
a04e420
commit 557a597
Showing
6 changed files
with
131 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/x-date-pickers/src/internals/hooks/useClearEndAdornment/useClearEndAdornment.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as React from 'react'; | ||
|
||
import { useSlotProps } from '@mui/base'; | ||
import ClearIcon from '@mui/icons-material/Clear'; | ||
import IconButton from '@mui/material/IconButton'; | ||
import { SlotsAndSlotProps } from '../../utils/slots-migration'; | ||
import { FieldSlotsComponents, FieldSlotsComponentsProps } from '../useField/useField.types'; | ||
|
||
type UseClearEndAdornmentProps< | ||
TInputProps extends { endAdornment?: React.ReactNode } | undefined, | ||
TFieldSlotsComponents extends FieldSlotsComponents, | ||
TFieldSlotsComponentsProps extends FieldSlotsComponentsProps, | ||
> = { | ||
clearable: boolean; | ||
InputProps: TInputProps; | ||
onClear: React.MouseEventHandler<HTMLButtonElement>; | ||
} & SlotsAndSlotProps<TFieldSlotsComponents, TFieldSlotsComponentsProps>; | ||
|
||
export const useClearEndAdornment = < | ||
TInputProps extends { endAdornment?: React.ReactNode } | undefined, | ||
TFieldSlotsComponents extends FieldSlotsComponents, | ||
TFieldSlotsComponentsProps extends FieldSlotsComponentsProps, | ||
>({ | ||
clearable, | ||
InputProps: ForwardedInputProps, | ||
onClear, | ||
slots, | ||
slotProps, | ||
}: UseClearEndAdornmentProps<TInputProps, TFieldSlotsComponents, TFieldSlotsComponentsProps>) => { | ||
const EndClearIcon = slots?.clearIcon ?? ClearIcon; | ||
const endClearIconProps = useSlotProps({ | ||
elementType: ClearIcon, | ||
externalSlotProps: slotProps?.clearIcon, | ||
externalForwardedProps: {}, | ||
ownerState: {}, | ||
}); | ||
|
||
const InputProps = { | ||
...ForwardedInputProps, | ||
endAdornment: clearable ? ( | ||
<React.Fragment> | ||
{ForwardedInputProps?.endAdornment} | ||
<IconButton className="clearButton" onClick={onClear} tabIndex={-1}> | ||
<EndClearIcon {...endClearIconProps} /> | ||
</IconButton> | ||
</React.Fragment> | ||
) : ( | ||
ForwardedInputProps?.endAdornment | ||
), | ||
}; | ||
|
||
return InputProps; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters