Skip to content
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

feat: prop to change calendar header format #69

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/components/CalendarContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface CalendarContainerProps {
onChange?: (date: dayjs.Dayjs) => void;
/** TodayPanel show or hide */
showToday?: boolean;
/** Change calendar header format to monthName, year */
monthNameOnHeader?: boolean;
}

interface PrivateProps {
Expand Down Expand Up @@ -60,13 +62,22 @@ class CalendarContainer extends React.Component<Props, State> {
super(props);
}

private formatHeaderTitle = (current: dayjs.Dayjs) => {
if (this.props.monthNameOnHeader) {
return dayjs(current).format('MMM, YYYY');
}

return dayjs(current).format('YYYY.MM');
}

public getHeaderTitle = () => {

const { current } = this.props;
const year = dayjs(current).year();
return {
[IDatePicker.ViewMode.YEAR]: `${year - 4} - ${year + 5}`,
[IDatePicker.ViewMode.MONTH]: `${year}`,
[IDatePicker.ViewMode.DAY]: dayjs(current).format('YYYY.MM'),
[IDatePicker.ViewMode.DAY]: this.formatHeaderTitle(current),
}[this.state.viewMode];
};

Expand Down
3 changes: 3 additions & 0 deletions stories/Calendar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ storiesOf('Calendar', module)
const showMontCnt = number('showMonthCnt', 2);
return <CalendarSelectedController showMonthCnt={showMontCnt} />;
})
.add('monthNameOnHeader', () => {
return <CalendarSelectedController monthNameOnHeader />
})
.add('disableDay', () => {
const disableDay = (date: dayjs.Dayjs) => {
return dayjs(date).date() < 7;
Expand Down
3 changes: 3 additions & 0 deletions stories/DatePicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ storiesOf('DatePicker', module)
.add('showMonthCnt', () => {
return <DatePicker {...defaultProps} showMonthCnt={2} />;
})
.add('monthNameOnHeader', () => {
return <DatePicker {...defaultProps} monthNameOnHeader />;
})
.add('onTop', () => {
return (
<div style={{ paddingTop: '300px' }}>
Expand Down
26 changes: 26 additions & 0 deletions test/CalendarContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ describe('<CalendarContainer/>', () => {
});
});

describe('prop: monthNameOnHeader', () => {
let component: ReactWrapper<Props>;
let componentNameOnHeader: ReactWrapper<Props>;

beforeEach(() => {
component = mount(
<CalendarContainer {...defaultProps} />
);
componentNameOnHeader = mount(
<CalendarContainer {...defaultProps} monthNameOnHeader />
);
});

it('should not change format of calendar header', () => {
expect(
component.find('.calendar__head--title').first().text()
).toEqual('2018.12');
});

it('should change format of calendar header', () => {
expect(
componentNameOnHeader.find('.calendar__head--title').first().text()
).toEqual('Dec, 2018');
});
});

describe('handle base test(onPrev, onNext)', () => {
beforeEach(() => {
mountComponent = mount(
Expand Down