Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietro Ghezzi committed Jan 25, 2018
1 parent 90ccdf3 commit 66d786e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
'error',
{
singleQuote: true,
trailingComma: 'all',
trailingComma: 'all',
},
],
'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx'] }],
Expand Down
1 change: 0 additions & 1 deletion lib/components/CalendarDate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import moment from 'moment';
import MdCalendar from 'react-icons/lib/md/date-range';
import { Calendar } from '../elements';
import NumPad from './NumPad';
Expand Down
2 changes: 2 additions & 0 deletions lib/components/NumPad.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export default ({
label: undefined,
theme: undefined,
dateFormat: undefined,
locale: 'en',
defaultValue: '',
};

Expand All @@ -114,6 +115,7 @@ export default ({
]),
placeholder: PropTypes.string,
label: PropTypes.string,
locale: PropTypes.string,
theme: PropTypes.string,
dateFormat: PropTypes.string,
defaultValue: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
Expand Down
81 changes: 36 additions & 45 deletions lib/elements/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ const createDateObjects = (startOfMonth, weekOffset = 0) => {
if (diff < 0) diff += 7;

const prevMonthDays = [];
for (let i = 0; i < diff; i++) {
for (let i = 0; i < diff; i += 1) {
prevMonthDays.push({
day: startOfMonth.clone().subtract(diff - i, 'days'),
prevMonth: true,
});
}

const currentMonthDays = [];
for (let i = 1; i < startOfMonth.daysInMonth() + 1; i++) {
for (let i = 1; i < startOfMonth.daysInMonth() + 1; i += 1) {
currentMonthDays.push({
day: moment([startOfMonth.year(), startOfMonth.month(), i]),
});
Expand Down Expand Up @@ -53,7 +53,7 @@ const Button = styled.button`
cursor: pointer;
&:focus {
color: yellow;
outline: none ;
outline: none;
}
margin-top: -5px;
`;
Expand Down Expand Up @@ -169,10 +169,11 @@ class Calendar extends Component {
this.onChange = this.onChange.bind(this);
}

handleClickOutside(evt) {
evt.preventDefault();
evt.stopPropagation();
this.onChange(this.state.date);
onChange(day) {
const { confirm, dateFormat } = this.props;
this.setState({ date: day }, () => {
confirm(this.state.date.format(dateFormat));
});
}

swipingLeft() {
Expand Down Expand Up @@ -211,16 +212,15 @@ class Calendar extends Component {
}));
}

onChange(day) {
let { confirm, dateFormat } = this.props;
this.setState({ date: day }, () => {
confirm(this.state.date.format(dateFormat));
});
handleClickOutside(evt) {
evt.preventDefault();
evt.stopPropagation();
this.onChange(this.state.date);
}

render() {
let { locale, weekOffset } = this.props;
let { date, calendarMonth } = this.state;
const { locale, weekOffset } = this.props;
const { date, calendarMonth } = this.state;

return (
<Swipeable
Expand Down Expand Up @@ -258,7 +258,7 @@ class Calendar extends Component {
.fill()
.map((_, i) => i + 1 + weekOffset)
.map(weekDay => (
<StyledGridItem key={`week-day-${weekDay}`} weekDays={true}>
<StyledGridItem key={`week-day-${weekDay}`} weekDays>
{moment()
.isoWeekday(weekDay)
.locale(locale)
Expand All @@ -268,9 +268,9 @@ class Calendar extends Component {
</WeekDays>

<Days>
{createDateObjects(calendarMonth, weekOffset).map((day, i) => (
{createDateObjects(calendarMonth, weekOffset).map(day => (
<GridItem
key={`day-${i}`}
key={`day-${day.day.format('DD.MM')}`}
day={day}
date={date}
onClick={this.onChange}
Expand All @@ -286,37 +286,28 @@ class Calendar extends Component {
}
}

class GridItem extends Component {
constructor(props) {
super(props);
this.onClick = this.onClick.bind(this);
}

onClick() {
this.props.onClick(this.props.day.day);
}

render() {
let { day, date } = this.props;

return (
<StyledGridItem
active={day.day.isSame(date)}
onClick={this.onClick}
nextMonth={day.nextMonth}
prevMonth={day.prevMonth}
>
{this.props.children}
</StyledGridItem>
);
}
}
const GridItem = ({ onClick, day, date, children }) => (
<StyledGridItem
active={day.day.isSame(date)}
onClick={() => onClick(day.day)}
nextMonth={day.nextMonth}
prevMonth={day.prevMonth}
>
{children}
</StyledGridItem>
);

GridItem.propTypes = {
onClick: PropTypes.func.isRequired,
day: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
date: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
children: PropTypes.string.isRequired,
};

Calendar.propTypes = {
confirm: PropTypes.func.isRequired,
cancel: PropTypes.func.isRequired,
weekOffset: PropTypes.number.isRequired,
currentDate: PropTypes.object.isRequired,
weekOffset: PropTypes.number,
currentDate: PropTypes.object, // eslint-disable-line react/forbid-prop-types
dateFormat: PropTypes.string,
locale: PropTypes.string,
};
Expand Down
6 changes: 6 additions & 0 deletions stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ storiesOf('Components', module)
theme="blackAndWhite"
onChange={value => console.log('changed', value)}
/>
<NumPad.Calendar
onChange={value => console.log('changed', value)}
label={'Data di nascita'}
locale="it"
dateFormat="DD.MM.YYYY"
/>
</DemoModal>
))
.add('Numpad date', () => [
Expand Down

0 comments on commit 66d786e

Please sign in to comment.