-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DateRangePicker] Add shortcut info in Date Range Picker to the field #11810
Comments
This should be possible when you follow the layout docs section. With this you can customize how the elements are placed and even add your proprietary elements to it. It should be possible to add a All of it should be controlled by a single state so that changes are reflected on each part of the layout. |
@michelengelen , could you please be more specific (looks like a polite RTFM, we have a paying support) ? |
Sure thing. Here is the example: Custom Layout This is purely a showcase and is far from perfect in terms of styling, etc. |
anything to add here @LukasTy? |
@michelengelen , our question is how to get the information when the calendar is closed of the shortcut used in the fields |
OK, what do you mean by that? 🤷🏼 |
@michelengelen , the question is how to add the shortcut information in the date range field, not in the date range calendar hope it helps |
@david-ic3 yes, that helps. import * as React from 'react';
import dayjs, { Dayjs } from 'dayjs';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateRange } from '@mui/x-date-pickers-pro';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { SingleInputDateRangeField } from '@mui/x-date-pickers-pro/SingleInputDateRangeField';
import InputAdornment from '@mui/material/InputAdornment';
import Chip from '@mui/material/Chip';
export default function DateRangeFieldValue() {
const [value, setValue] = React.useState<DateRange<Dayjs>>(() => [
dayjs().subtract(7, 'day'),
dayjs(),
]);
const getShortcutLabel = () => {
// do the calc to define the label here
const today = dayjs();
const isStartToday = value[0]?.isSame(today, 'day');
const isEndToday = value[1]?.isSame(today, 'day');
const daysDiff = value[0]?.diff(value[1], 'day');
console.log('daysdiff', daysDiff);
switch (true) {
case isEndToday && daysDiff === -7:
return 'Last 7 days';
case isStartToday && daysDiff === 7:
return 'Next 7 days';
}
};
const ShortcutAdornment = () => {
const label = getShortcutLabel();
console.log('label', label);
if (!label) return null;
return (
<InputAdornment position="start">
<Chip size="small" label={label} />
</InputAdornment>
);
};
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['SingleInputDateRangeField']}>
<SingleInputDateRangeField
label="Controlled field"
value={value}
onChange={(newValue) => setValue(newValue)}
slotProps={{
textField: {
InputProps: {
startAdornment: <ShortcutAdornment />,
},
},
}}
/>
</DemoContainer>
</LocalizationProvider>
);
} Or, if you want to use the shortcuts and only show it when you clicked one of those, you can store the shortcut value and label in a wrapper around the Hope that helps. |
The issue has been inactive for 7 days and has been automatically closed. If you think that it has been incorrectly closed, please reopen it and provide missing information (if any) or continue the last discussion. |
The problem in depth
The goal is to have a similar feature as the range picker from Google Analytics :
You can see how the shortcut is also visible to the end user when the range date picker is used (it's integrated into the model). It's visible also when the calendar is closed.
I've seen you have an example with shortcuts in a range picker here : https://codesandbox.io/p/sandbox/shortcuts-changeimportance-overall-58q5gk
some help on how to do this is appreciated
Your environment
`npx @mui/envinfo`
Search keywords: shortcut range picker
Order ID: #19458
The text was updated successfully, but these errors were encountered: