Skip to content

Commit

Permalink
feat: add highlightedDays prop to Calendar (#2636)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeandroTorresSicilia authored Feb 3, 2024
1 parent 58898e6 commit 7de9615
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/components/Calendar/day.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function DayComponent(props) {
focusedDate,
currentRange,
disabledDays,
highlightedDays,
privateKeyDown,
privateOnFocus,
privateOnBlur,
Expand All @@ -40,6 +41,7 @@ function DayComponent(props) {
compareDates(date, minDate) < 0 ||
(maxRangeEnd && compareDates(date, maxRangeEnd) > 0) ||
isInArray(date, disabledDays);
const isHighlighted = isInArray(date, highlightedDays);
const tabIndex = isSameDay(focusedDate, date) ? 0 : -1;
const buttonRef = useRef();
const isRangeStartDate = useRangeStartDate(date, currentRange);
Expand All @@ -63,6 +65,7 @@ function DayComponent(props) {
onBlur={privateOnBlur}
role="button"
aria-disabled="true"
isHighlighted={isHighlighted}
>
{day}
</StyledDayAdjacent>
Expand Down Expand Up @@ -94,6 +97,7 @@ function DayComponent(props) {
onBlur={privateOnBlur}
isWithinRange={isWithinRange}
isToday={isToday}
isHighlighted={isHighlighted}
>
{day}
</StyledDayButton>
Expand Down
7 changes: 7 additions & 0 deletions src/components/Calendar/doubleCalendar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default function DoubleCalendar(props) {
selectedRange,
selectionType,
disabledDays,
highlightedDays,
} = props;
const currentValue = useNormalizedValue(value);
const [focusedDate, setFocusedDate] = useState(currentValue);
Expand Down Expand Up @@ -241,6 +242,7 @@ export default function DoubleCalendar(props) {
selectedRange,
currentRange,
disabledDays,
highlightedDays,
maxRangeEnd,
privateOnFocus: handleOnDayFocus,
privateOnBlur: handleOnDayBlur,
Expand Down Expand Up @@ -270,6 +272,7 @@ export default function DoubleCalendar(props) {
selectedRange,
currentRange,
disabledDays,
highlightedDays,
maxRangeEnd,
privateOnFocus: handleOnDayFocus,
privateOnBlur: handleOnDayBlur,
Expand Down Expand Up @@ -310,6 +313,9 @@ DoubleCalendar.propTypes = {
disabledDays: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
),
highlightedDays: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
),
};

DoubleCalendar.defaultProps = {
Expand All @@ -324,4 +330,5 @@ DoubleCalendar.defaultProps = {
selectionType: 'single',
selectedRange: undefined,
disabledDays: [],
highlightedDays: [],
};
5 changes: 5 additions & 0 deletions src/components/Calendar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Calendar.propTypes = {
disabledDays: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
),
/** An array containing the days that should be highlighted with a mark. Used for example to show an indicator that there are notifications in these days. */
highlightedDays: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
),
};

Calendar.defaultProps = {
Expand All @@ -96,4 +100,5 @@ Calendar.defaultProps = {
selectionType: 'single',
variant: 'single',
disabledDays: [],
highlightedDays: [],
};
7 changes: 6 additions & 1 deletion src/components/Calendar/singleCalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,15 @@ class SingleCalendar extends Component {

getContext() {
const { focusedDate, currentRange, maxRangeEnd } = this.state;
const { selectionType, selectedRange, disabledDays } = this.props;
const { selectionType, selectedRange, disabledDays, highlightedDays } = this.props;
return {
focusedDate,
useAutoFocus: this.enableNavKeys,
selectionType,
selectedRange,
currentRange,
disabledDays,
highlightedDays,
maxRangeEnd,
privateKeyDown: this.handleKeyDown,
privateOnFocus: this.onDayFocus,
Expand Down Expand Up @@ -385,6 +386,9 @@ SingleCalendar.propTypes = {
disabledDays: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
),
highlightedDays: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
),
};

SingleCalendar.defaultProps = {
Expand All @@ -399,6 +403,7 @@ SingleCalendar.defaultProps = {
selectionType: 'single',
selectedRange: undefined,
disabledDays: [],
highlightedDays: [],
};

export default SingleCalendar;
17 changes: 17 additions & 0 deletions src/components/Calendar/styled/dayAdjacent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const StyledDayAdjacent = attachThemeAttrs(styled.span)`
cursor: not-allowed;
user-select: none;
border-radius: 48px;
position: relative;
@media (max-width: 600px) {
margin: 3px auto;
Expand All @@ -29,6 +30,22 @@ const StyledDayAdjacent = attachThemeAttrs(styled.span)`
line-height: 36px;
outline: none;
}
${props =>
props.isHighlighted &&
`
&::after {
content: '';
height: 6px;
width: 6px;
position: absolute;
bottom: 2px;
left: 50%;
transform: translateX(-50%);
border-radius: 50%;
background-color: ${props.palette.error.main};
}
`}
`;

export default StyledDayAdjacent;
16 changes: 16 additions & 0 deletions src/components/Calendar/styled/dayButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ const StyledDayButton = attachThemeAttrs(styled.button)`
color: ${props.palette.getContrastText(props.palette.brand.main)};
background-color: ${props.palette.brand.dark};
`};
${props =>
props.isHighlighted &&
`
&::after {
content: '';
height: 6px;
width: 6px;
position: absolute;
bottom: 0px;
left: 50%;
transform: translateX(-50%);
border-radius: 50%;
background-color: ${props.palette.error.main};
}
`}
`;

export default StyledDayButton;

0 comments on commit 7de9615

Please sign in to comment.