Skip to content

Commit

Permalink
fix: some code enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
wildergd committed May 21, 2020
1 parent f7b783b commit b3b3bc9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 41 deletions.
38 changes: 14 additions & 24 deletions src/components/Calendar/hooks/useWeeksBuilder.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
import React, { useMemo } from 'react';
import { addDays } from '../helpers';
import { addDays, getLastDayMonth } from '../helpers';
import Week from '../week';

export default function useWeeksBuilder(
value,
firstDayMonth,
lastDayMonth,
minDate,
maxDate,
onChange,
) {
export default function useWeeksBuilder(value, firstDayMonth, minDate, maxDate, onChange) {
return useMemo(() => {
let date = new Date(firstDayMonth);
const weeks = [];
const date = new Date(firstDayMonth);
const lastDayMonth = getLastDayMonth(firstDayMonth);
const dayOfWeek = date.getDay();
const daysAfter = 6 - dayOfWeek;
const totalWeeks = (lastDayMonth.getDate() + dayOfWeek + 6 - lastDayMonth.getDay()) / 7;
const week = addDays(date, -dayOfWeek);

while (date <= lastDayMonth || addDays(date, -dayOfWeek) <= lastDayMonth) {
const startDate = addDays(date, -dayOfWeek);
const endDate = addDays(date, daysAfter);
return Array.from(Array(totalWeeks), (_, index) => {
const startDate = addDays(week, 7 * index);
const endDate = addDays(startDate, 6);

startDate.setHours(0, 0, 0, 0);
endDate.setHours(11, 59, 59, 999);

weeks.push(
return (
<Week
value={value}
startDate={startDate}
Expand All @@ -32,13 +25,10 @@ export default function useWeeksBuilder(
maxDate={maxDate}
firstDayMonth={firstDayMonth}
lastDayMonth={lastDayMonth}
key={date.getTime()}
key={startDate.getTime()}
onChange={onChange}
/>,
/>
);

date = addDays(date, 7);
}
return weeks;
}, [value, firstDayMonth, lastDayMonth, minDate, maxDate, onChange]);
});
}, [value, firstDayMonth, minDate, maxDate, onChange]);
}
6 changes: 2 additions & 4 deletions src/components/Calendar/month.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import React from 'react';
import PropTypes from 'prop-types';
import { getLastDayMonth } from './helpers';
import { useWeeksBuilder } from './hooks';

export default function Month(props) {
const { firstDayMonth, value, minDate, maxDate, onChange } = props;
const lastDayMonth = getLastDayMonth(firstDayMonth);
const Weeks = useWeeksBuilder(value, firstDayMonth, lastDayMonth, minDate, maxDate, onChange);
const weeks = useWeeksBuilder(value, firstDayMonth, minDate, maxDate, onChange);

return <tbody>{Weeks}</tbody>;
return <tbody>{weeks}</tbody>;
}

Month.propTypes = {
Expand Down
22 changes: 9 additions & 13 deletions src/components/Calendar/styled/dayButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const StyledDayButton = attachThemeAttrs(styled.button)`
font: inherit;
border: none;
outline: none;
transition: all 0.2s ease;
background-color: transparent;
border-radius: 48px;
line-height: 36px;
Expand All @@ -19,11 +18,14 @@ const StyledDayButton = attachThemeAttrs(styled.button)`
text-transform: none;
appearance: button;
&:hover {
transition: all 0.2s ease;
background-color: transparent;
border: 1px solid ${props => props.palette.brand.main};
}
${props =>
!props.isHovered &&
`
&:hover {
background-color: transparent;
border: 1px solid ${props.palette.brand.main};
}
`}
&:active {
transform: scale(0.85);
Expand Down Expand Up @@ -70,14 +72,8 @@ const StyledDayButton = attachThemeAttrs(styled.button)`
${props =>
props.isHovered &&
`
transition: all 0.2s ease;
color: ${props.palette.getContrastText(props.palette.brand.main)};
background-color: ${props.palette.brand.main};
:hover {
color: ${props.palette.getContrastText(props.palette.brand.main)};
background-color: ${props.palette.brand.main};
}
background-color: ${props.palette.brand.dark};
`};
`;

Expand Down

0 comments on commit b3b3bc9

Please sign in to comment.