mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Use firstday of the week for range shortcuts in date pickers (#14210)
This commit is contained in:
parent
16848d03ae
commit
4c8e863c0e
@ -11,16 +11,20 @@ export const weekdays = [
|
|||||||
"saturday",
|
"saturday",
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
export const firstWeekdayIndex = (locale: FrontendLocaleData): number => {
|
type WeekdayIndex = 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
||||||
|
|
||||||
|
export const firstWeekdayIndex = (locale: FrontendLocaleData): WeekdayIndex => {
|
||||||
if (locale.first_weekday === FirstWeekday.language) {
|
if (locale.first_weekday === FirstWeekday.language) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if ("weekInfo" in Intl.Locale.prototype) {
|
if ("weekInfo" in Intl.Locale.prototype) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
return new Intl.Locale(locale.language).weekInfo.firstDay % 7;
|
return new Intl.Locale(locale.language).weekInfo.firstDay % 7;
|
||||||
}
|
}
|
||||||
return getWeekStartByLocale(locale.language) % 7;
|
return (getWeekStartByLocale(locale.language) % 7) as WeekdayIndex;
|
||||||
}
|
}
|
||||||
return weekdays.indexOf(locale.first_weekday);
|
return weekdays.includes(locale.first_weekday)
|
||||||
|
? (weekdays.indexOf(locale.first_weekday) as WeekdayIndex)
|
||||||
|
: 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const firstWeekday = (locale: FrontendLocaleData) => {
|
export const firstWeekday = (locale: FrontendLocaleData) => {
|
||||||
|
@ -16,6 +16,7 @@ import {
|
|||||||
} from "home-assistant-js-websocket/dist/types";
|
} from "home-assistant-js-websocket/dist/types";
|
||||||
import { css, html, LitElement, PropertyValues } from "lit";
|
import { css, html, LitElement, PropertyValues } from "lit";
|
||||||
import { property, state } from "lit/decorators";
|
import { property, state } from "lit/decorators";
|
||||||
|
import { firstWeekdayIndex } from "../../common/datetime/first_weekday";
|
||||||
import { LocalStorage } from "../../common/decorators/local-storage";
|
import { LocalStorage } from "../../common/decorators/local-storage";
|
||||||
import { ensureArray } from "../../common/ensure-array";
|
import { ensureArray } from "../../common/ensure-array";
|
||||||
import { navigate } from "../../common/navigate";
|
import { navigate } from "../../common/navigate";
|
||||||
@ -179,8 +180,9 @@ class HaPanelHistory extends SubscribeMixin(LitElement) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
const weekStart = startOfWeek(today);
|
const weekStartsOn = firstWeekdayIndex(this.hass.locale);
|
||||||
const weekEnd = endOfWeek(today);
|
const weekStart = startOfWeek(today, { weekStartsOn });
|
||||||
|
const weekEnd = endOfWeek(today, { weekStartsOn });
|
||||||
|
|
||||||
this._ranges = {
|
this._ranges = {
|
||||||
[this.hass.localize("ui.components.date-range-picker.ranges.today")]: [
|
[this.hass.localize("ui.components.date-range-picker.ranges.today")]: [
|
||||||
|
@ -12,6 +12,7 @@ import {
|
|||||||
} from "date-fns/esm";
|
} from "date-fns/esm";
|
||||||
import { css, html, LitElement, PropertyValues } from "lit";
|
import { css, html, LitElement, PropertyValues } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
|
import { firstWeekdayIndex } from "../../common/datetime/first_weekday";
|
||||||
import { navigate } from "../../common/navigate";
|
import { navigate } from "../../common/navigate";
|
||||||
import {
|
import {
|
||||||
createSearchParam,
|
createSearchParam,
|
||||||
@ -108,8 +109,9 @@ export class HaPanelLogbook extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
const weekStart = startOfWeek(today);
|
const weekStartsOn = firstWeekdayIndex(this.hass.locale);
|
||||||
const weekEnd = endOfWeek(today);
|
const weekStart = startOfWeek(today, { weekStartsOn });
|
||||||
|
const weekEnd = endOfWeek(today, { weekStartsOn });
|
||||||
|
|
||||||
this._ranges = {
|
this._ranges = {
|
||||||
[this.hass.localize("ui.components.date-range-picker.ranges.today")]: [
|
[this.hass.localize("ui.components.date-range-picker.ranges.today")]: [
|
||||||
|
@ -36,6 +36,7 @@ import { EnergyData, getEnergyDataCollection } from "../../../data/energy";
|
|||||||
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
|
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
|
||||||
import { HomeAssistant, ToggleButton } from "../../../types";
|
import { HomeAssistant, ToggleButton } from "../../../types";
|
||||||
import { computeRTLDirection } from "../../../common/util/compute_rtl";
|
import { computeRTLDirection } from "../../../common/util/compute_rtl";
|
||||||
|
import { firstWeekdayIndex } from "../../../common/datetime/first_weekday";
|
||||||
|
|
||||||
@customElement("hui-energy-period-selector")
|
@customElement("hui-energy-period-selector")
|
||||||
export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
|
export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
|
||||||
@ -179,11 +180,13 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
|
|||||||
? today
|
? today
|
||||||
: this._startDate;
|
: this._startDate;
|
||||||
|
|
||||||
|
const weekStartsOn = firstWeekdayIndex(this.hass.locale);
|
||||||
|
|
||||||
this._setDate(
|
this._setDate(
|
||||||
this._period === "day"
|
this._period === "day"
|
||||||
? startOfDay(start)
|
? startOfDay(start)
|
||||||
: this._period === "week"
|
: this._period === "week"
|
||||||
? startOfWeek(start, { weekStartsOn: 1 })
|
? startOfWeek(start, { weekStartsOn })
|
||||||
: this._period === "month"
|
: this._period === "month"
|
||||||
? startOfMonth(start)
|
? startOfMonth(start)
|
||||||
: startOfYear(start)
|
: startOfYear(start)
|
||||||
@ -191,11 +194,13 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _pickToday() {
|
private _pickToday() {
|
||||||
|
const weekStartsOn = firstWeekdayIndex(this.hass.locale);
|
||||||
|
|
||||||
this._setDate(
|
this._setDate(
|
||||||
this._period === "day"
|
this._period === "day"
|
||||||
? startOfToday()
|
? startOfToday()
|
||||||
: this._period === "week"
|
: this._period === "week"
|
||||||
? startOfWeek(new Date(), { weekStartsOn: 1 })
|
? startOfWeek(new Date(), { weekStartsOn })
|
||||||
: this._period === "month"
|
: this._period === "month"
|
||||||
? startOfMonth(new Date())
|
? startOfMonth(new Date())
|
||||||
: startOfYear(new Date())
|
: startOfYear(new Date())
|
||||||
@ -227,11 +232,13 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _setDate(startDate: Date) {
|
private _setDate(startDate: Date) {
|
||||||
|
const weekStartsOn = firstWeekdayIndex(this.hass.locale);
|
||||||
|
|
||||||
const endDate =
|
const endDate =
|
||||||
this._period === "day"
|
this._period === "day"
|
||||||
? endOfDay(startDate)
|
? endOfDay(startDate)
|
||||||
: this._period === "week"
|
: this._period === "week"
|
||||||
? endOfWeek(startDate, { weekStartsOn: 1 })
|
? endOfWeek(startDate, { weekStartsOn })
|
||||||
: this._period === "month"
|
: this._period === "month"
|
||||||
? endOfMonth(startDate)
|
? endOfMonth(startDate)
|
||||||
: endOfYear(startDate);
|
: endOfYear(startDate);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user