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

Calendar: add first-day-of-week attribute #16047

Merged
merged 1 commit into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions examples/docs/en-US/calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ Display date.
:::

### Attributes
| Attribute | Description | Type | Accepted Values | Default |
|-----------------|-------------- |---------- |---------------------- |--------- |
| value / v-model | binding value | Date/string/number | — | — |
| range | time range, including start time and end time. Start time must be Monday, end time must be Sunday, the time span cannot exceed two months | Array | — | — |
| Attribute | Description | Type | Accepted Values | Default |
|-----------------|------------------- |---------- |---------------------- |--------- |
| value / v-model | binding value | Date/string/number | — | — |
| range | time range, including start time and end time. Start time must be Monday, end time must be Sunday, the time span cannot exceed two months | Array | — | — |
| first-day-of-week | fisrt day of week| Number | 1 to 7 | 1 |

### dateCell scoped slot 参数
| Attribute | Description | Type | Accepted Values | Default |
Expand Down
7 changes: 4 additions & 3 deletions examples/docs/es/calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ Muestra fechas.
:::

### Atributos
| Atributo | Descripción | Tipo | Valores aceptados | Por defecto |
|-----------------|-------------- |---------- |---------------------- |--------- |
| value / v-model | valor vinculante | Date/string/number | — | — |
| Atributo | Descripción | Tipo | Valores aceptados | Por defecto |
|-----------------|------------------- |---------- |---------------------- |------------ |
| value / v-model | valor vinculante | Date/string/number | — | — |
| range | rango de tiempo, incluyendo el tiempo de inicio y el tiempo final. El tiempo de inicio debe ser el lunes, el tiempo final debe ser el domingo, el período no puede exceder los dos meses. | Array | — | — |
| first-day-of-week | fisrt day of week| Number | 1 to 7 | 1 |

### dateCell scoped slot
| Atributo | Descripción | Tipo | Valores aceptados | Por defecto |
Expand Down
9 changes: 5 additions & 4 deletions examples/docs/fr-FR/calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ Affiche un calendrier.

### Attributs

| Attribut | Description | Type | Valeurs acceptées | Défaut |
|-----------------|-------------- |---------- |---------------------- |--------- |
| value / v-model | Valeur liée. | Date/string/number | — | — |
| range | Intervalle de dates, début et fin inclus. Le début doit être un lundi et la fin un dimanche, l'intervalle ne pouvant excéder deux mois. | Array | — | — |
| Attribut | Description | Type | Valeurs acceptées | Défaut |
|------------------ |-------------- |---------- |---------------------- |--------- |
| value / v-model | Valeur liée. | Date/string/number | — | — |
| range | Intervalle de dates, début et fin inclus. Le début doit être un lundi et la fin un dimanche, l'intervalle ne pouvant excéder deux mois. | Array | — | — |
| first-day-of-week | fisrt day of week| Number | 1 to 7 | 1 |

### Slot dateCell

Expand Down
1 change: 1 addition & 0 deletions examples/docs/zh-CN/calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
|-----------------|-------------- |---------- |------------ |-------- |
| value / v-model | 绑定值 | Date/string/number | — | — |
| range | 时间范围,包括开始时间与结束时间。开始时间必须是周一,结束时间必须是周日,且时间跨度不能超过两个月。 | Array | — | — |
| first-day-of-week | 周起始日 | Number | 1 到 7 | 1 |

### dateCell scoped slot 参数
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
Expand Down
27 changes: 16 additions & 11 deletions packages/calendar/src/date-table.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script>
import fecha from 'element-ui/src/utils/date';
import { range as rangeArr, getFirstDayOfMonth, getPrevMonthLastDays, getMonthDays, getI18nSettings, validateRangeInOneMonth } from 'element-ui/src/utils/date-util';
export default {

const WEEK_DAYS = getI18nSettings().dayNames;
export default {
props: {
selectedDay: String, // formated date yyyy-MM-dd
range: {
Expand All @@ -14,7 +15,8 @@ export default {
}
},
date: Date,
hideHeader: Boolean
hideHeader: Boolean,
firstDayOfWeek: Number
},

inject: ['elCalendar'],
Expand Down Expand Up @@ -119,7 +121,8 @@ export default {
const date = this.date;
let firstDay = getFirstDayOfMonth(date);
firstDay = firstDay === 0 ? 7 : firstDay;
const prevMonthDays = getPrevMonthLastDays(date, firstDay - 1).map(day => ({
const firstDayOfWeek = typeof this.firstDayOfWeek === 'number' ? this.firstDayOfWeek : 1;
const prevMonthDays = getPrevMonthLastDays(date, firstDay - firstDayOfWeek).map(day => ({
text: day,
type: 'prev'
}));
Expand All @@ -135,20 +138,22 @@ export default {
days = days.concat(nextMonthDays);
}
return this.toNestedArr(days);
}
},
},

data() {
const dayNames = getI18nSettings().dayNames;
return {
DAYS: dayNames.slice(1).concat(dayNames[0])
};
weekDays() {
const start = this.firstDayOfWeek;
if (typeof start !== 'number' || start === 0) {
return WEEK_DAYS.slice();
} else {
return WEEK_DAYS.slice(start).concat(WEEK_DAYS.slice(0, start));
}
}
},

render() {
const thead = this.hideHeader ? null : (<thead>
{
this.DAYS.map(day => <th key={day}>{ day }</th>)
this.weekDays.map(day => <th key={day}>{ day }</th>)
}
</thead>);
return (
Expand Down
12 changes: 12 additions & 0 deletions packages/calendar/src/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<date-table
:date="date"
:selected-day="realSelectedDay"
:first-day-of-week="realFirstDayOfWeek"
@pick="pickDay" />
</div>
<div
Expand Down Expand Up @@ -86,6 +87,10 @@ export default {
return true;
}
}
},
firstDayOfWeek: {
type: Number,
default: 1
}
},

Expand Down Expand Up @@ -238,6 +243,13 @@ export default {
return data;
}
return [];
},

realFirstDayOfWeek() {
if (this.firstDayOfWeek < 1 || this.firstDayOfWeek > 6) {
return 0;
}
return Math.floor(this.firstDayOfWeek);
}
},

Expand Down
19 changes: 19 additions & 0 deletions test/unit/specs/calendar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,24 @@ describe('Calendar', () => {
expect(/2019.*5/.test(titleEl.innerText)).to.be.true;
expect(cell.classList.contains('is-selected')).to.be.true;
});

it('firstDayOfWeek', async() => {
vm = createVue({
template: `
<el-calendar v-model="value" :first-day-of-week="0"></el-calendar>
`,
data() {
return {
value: new Date('2019-04-01')
};
}
}, true);
const head = vm.$el.querySelector('.el-calendar-table thead');
expect(head.firstElementChild.innerText).to.be.equal('日');
expect(head.lastElementChild.innerText).to.be.equal('六');
const firstRow = vm.$el.querySelector('.el-calendar-table__row');
expect(firstRow.firstElementChild.innerText).to.be.equal('31');
expect(firstRow.lastElementChild.innerText).to.be.equal('6');
});
});

3 changes: 3 additions & 0 deletions types/calendar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ export declare class ElCalendar extends ElementUIComponent {

/** Specify the display range of the calendar */
range: DateType[]

/** First day of week */
firstDayOfWeek: number
}