Skip to content

Commit

Permalink
[docs] Add example to add a second icon next to the field's opening b…
Browse files Browse the repository at this point in the history
…utton
  • Loading branch information
flaviendelangle committed Mar 21, 2024
1 parent 9045cc0 commit b5eb832
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as React from 'react';
import dayjs from 'dayjs';
import InputAdornment from '@mui/material/InputAdornment';
import PriorityHighIcon from '@mui/icons-material/PriorityHigh';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { DatePicker } from '@mui/x-date-pickers/DatePicker';

function CustomInputAdornment(props) {
const { hasError, children, sx, ...other } = props;
return (
<InputAdornment {...other}>
<PriorityHighIcon
color="error"
sx={{ marginLeft: 1, opacity: hasError ? 1 : 0 }}
/>
{children}
</InputAdornment>
);
}

export default function SecondIconNextToOpeningButton() {
const [hasError, setHasError] = React.useState(false);

return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DatePicker']}>
<DatePicker
label="Picker with error icon"
maxDate={dayjs('2022-04-17')}
defaultValue={dayjs('2022-04-18')}
onError={(error) => setHasError(!!error)}
slots={{ inputAdornment: CustomInputAdornment }}
slotProps={{
inputAdornment: { hasError },
}}
/>
</DemoContainer>
</LocalizationProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as React from 'react';
import dayjs from 'dayjs';
import InputAdornment, { InputAdornmentProps } from '@mui/material/InputAdornment';
import PriorityHighIcon from '@mui/icons-material/PriorityHigh';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { DatePicker } from '@mui/x-date-pickers/DatePicker';

function CustomInputAdornment(props: InputAdornmentProps & { hasError?: boolean }) {
const { hasError, children, sx, ...other } = props;
return (
<InputAdornment {...other}>
<PriorityHighIcon
color="error"
sx={{ marginLeft: 1, opacity: hasError ? 1 : 0 }}
/>
{children}
</InputAdornment>
);
}

export default function SecondIconNextToOpeningButton() {
const [hasError, setHasError] = React.useState(false);

return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DatePicker']}>
<DatePicker
label="Picker with error icon"
maxDate={dayjs('2022-04-17')}
defaultValue={dayjs('2022-04-18')}
onError={(error) => setHasError(!!error)}
slots={{ inputAdornment: CustomInputAdornment }}
slotProps={{
inputAdornment: { hasError } as any,
}}
/>
</DemoContainer>
</LocalizationProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<DatePicker
label="Picker with error icon"
maxDate={dayjs('2022-04-17')}
defaultValue={dayjs('2022-04-18')}
onError={(error) => setHasError(!!error)}
slots={{ inputAdornment: CustomInputAdornment }}
slotProps={{
inputAdornment: { hasError } as any,
}}
/>
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ If you want to track the opening of the picker, you should use the `onOpen` / `o
```

:::

## Add a second icon next to the opening button

If you want to add a second icon next to the opening button, you can use the `inputAdornment` slot.
In the example below, the warning icon will be visible anytime the current value is invalid:

{{"demo": "SecondIconNextToOpeningButton.js"}}

0 comments on commit b5eb832

Please sign in to comment.