@mui/x-date-pickers
Version:
The community edition of the Date and Time Picker components (MUI X).
76 lines • 1.93 kB
JavaScript
export const getHourSectionOptions = ({
now,
value,
utils,
ampm,
isDisabled,
resolveAriaLabel,
timeStep,
valueOrReferenceDate
}) => {
const currentHours = value ? utils.getHours(value) : null;
const result = [];
const isSelected = (hour, overriddenCurrentHours) => {
const resolvedCurrentHours = overriddenCurrentHours ?? currentHours;
if (resolvedCurrentHours === null) {
return false;
}
if (ampm) {
if (hour === 12) {
return resolvedCurrentHours === 12 || resolvedCurrentHours === 0;
}
return resolvedCurrentHours === hour || resolvedCurrentHours - 12 === hour;
}
return resolvedCurrentHours === hour;
};
const isFocused = hour => {
return isSelected(hour, utils.getHours(valueOrReferenceDate));
};
const endHour = ampm ? 11 : 23;
for (let hour = 0; hour <= endHour; hour += timeStep) {
let label = utils.format(utils.setHours(now, hour), ampm ? 'hours12h' : 'hours24h');
const ariaLabel = resolveAriaLabel(parseInt(label, 10).toString());
label = utils.formatNumber(label);
result.push({
value: hour,
label,
isSelected,
isDisabled,
isFocused,
ariaLabel
});
}
return result;
};
export const getTimeSectionOptions = ({
value,
utils,
isDisabled,
timeStep,
resolveLabel,
resolveAriaLabel,
hasValue = true
}) => {
const isSelected = timeValue => {
if (value === null) {
return false;
}
return hasValue && value === timeValue;
};
const isFocused = timeValue => {
return value === timeValue;
};
return [...Array.from({
length: Math.ceil(60 / timeStep)
}, (_, index) => {
const timeValue = timeStep * index;
return {
value: timeValue,
label: utils.formatNumber(resolveLabel(timeValue)),
isDisabled,
isSelected,
isFocused,
ariaLabel: resolveAriaLabel(timeValue.toString())
};
})];
};