Commit 8e36a159 by Marcus Andersson Committed by GitHub

TimeZonePicker: added possibility to toggle if internal time zones should be…

TimeZonePicker: added possibility to toggle if internal time zones should be included or not. (#25934)

* made some small adjustments after feedback.

* made the flag optional.
parent a47fac72
...@@ -47,6 +47,7 @@ export const TimePickerFooter: FC<Props> = props => { ...@@ -47,6 +47,7 @@ export const TimePickerFooter: FC<Props> = props => {
<div className={cx(style.container, style.editContainer)}> <div className={cx(style.container, style.editContainer)}>
<div className={style.timeZoneContainer}> <div className={style.timeZoneContainer}>
<TimeZonePicker <TimeZonePicker
includeInternal={true}
onChange={timeZone => { onChange={timeZone => {
onToggleChangeTz(); onToggleChangeTz();
......
...@@ -21,6 +21,7 @@ export const basic = () => { ...@@ -21,6 +21,7 @@ export const basic = () => {
{(value, updateValue) => { {(value, updateValue) => {
return ( return (
<TimeZonePicker <TimeZonePicker
includeInternal={true}
value={value.value} value={value.value}
onChange={newValue => { onChange={newValue => {
if (!newValue) { if (!newValue) {
......
...@@ -15,16 +15,17 @@ import { TimeZoneGroup } from './TimeZonePicker/TimeZoneGroup'; ...@@ -15,16 +15,17 @@ import { TimeZoneGroup } from './TimeZonePicker/TimeZoneGroup';
import { formatUtcOffset } from './TimeZonePicker/TimeZoneOffset'; import { formatUtcOffset } from './TimeZonePicker/TimeZoneOffset';
export interface Props { export interface Props {
onChange: (timeZone: TimeZone | undefined) => void;
value?: TimeZone; value?: TimeZone;
width?: number; width?: number;
autoFocus?: boolean; autoFocus?: boolean;
onChange: (timeZone: TimeZone | undefined) => void;
onBlur?: () => void; onBlur?: () => void;
includeInternal?: boolean;
} }
export const TimeZonePicker: React.FC<Props> = props => { export const TimeZonePicker: React.FC<Props> = props => {
const { onChange, width, autoFocus = false, onBlur, value } = props; const { onChange, width, autoFocus = false, onBlur, value, includeInternal = false } = props;
const groupedTimeZones = useTimeZones(); const groupedTimeZones = useTimeZones(includeInternal);
const selected = useSelectedTimeZone(groupedTimeZones, value); const selected = useSelectedTimeZone(groupedTimeZones, value);
const filterBySearchIndex = useFilterBySearchIndex(); const filterBySearchIndex = useFilterBySearchIndex();
const TimeZoneOption = width && width <= 45 ? CompactTimeZoneOption : WideTimeZoneOption; const TimeZoneOption = width && width <= 45 ? CompactTimeZoneOption : WideTimeZoneOption;
...@@ -59,10 +60,10 @@ interface SelectableZoneGroup extends SelectableValue<string> { ...@@ -59,10 +60,10 @@ interface SelectableZoneGroup extends SelectableValue<string> {
options: SelectableZone[]; options: SelectableZone[];
} }
const useTimeZones = (): SelectableZoneGroup[] => { const useTimeZones = (includeInternal: boolean): SelectableZoneGroup[] => {
const now = Date.now(); const now = Date.now();
return getTimeZoneGroups(true).map((group: GroupedTimeZones) => { return getTimeZoneGroups(includeInternal).map((group: GroupedTimeZones) => {
const options = group.zones.reduce((options: SelectableZone[], zone) => { const options = group.zones.reduce((options: SelectableZone[], zone) => {
const info = getTimeZoneInfo(zone, now); const info = getTimeZoneInfo(zone, now);
...@@ -95,18 +96,20 @@ const useSelectedTimeZone = ( ...@@ -95,18 +96,20 @@ const useSelectedTimeZone = (
return undefined; return undefined;
} }
const tz = toLower(timeZone);
const group = groups.find(group => { const group = groups.find(group => {
if (!group.label) { if (!group.label) {
return isInternal(timeZone); return isInternal(tz);
} }
return timeZone.startsWith(group.label); return tz.startsWith(toLower(group.label));
}); });
return group?.options.find(option => { return group?.options.find(option => {
if (isEmpty(timeZone)) { if (isEmpty(tz)) {
return option.value === InternalTimeZones.default; return option.value === InternalTimeZones.default;
} }
return toLower(option.value) === timeZone; return toLower(option.value) === tz;
}); });
}, [groups, timeZone]); }, [groups, timeZone]);
}; };
......
...@@ -157,7 +157,7 @@ export class SharedPreferences extends PureComponent<Props, State> { ...@@ -157,7 +157,7 @@ export class SharedPreferences extends PureComponent<Props, State> {
</Field> </Field>
<Field label="Timezone" aria-label={selectors.components.TimeZonePicker.container}> <Field label="Timezone" aria-label={selectors.components.TimeZonePicker.container}>
<TimeZonePicker value={timezone} onChange={this.onTimeZoneChanged} /> <TimeZonePicker includeInternal={true} value={timezone} onChange={this.onTimeZoneChanged} />
</Field> </Field>
<div className="gf-form-button-row"> <div className="gf-form-button-row">
<Button variant="primary">Save</Button> <Button variant="primary">Save</Button>
......
...@@ -102,7 +102,12 @@ export class TimePickerSettings extends PureComponent<Props, State> { ...@@ -102,7 +102,12 @@ export class TimePickerSettings extends PureComponent<Props, State> {
<div className="gf-form-group"> <div className="gf-form-group">
<div className="gf-form" aria-label={selectors.components.TimeZonePicker.container}> <div className="gf-form" aria-label={selectors.components.TimeZonePicker.container}>
<label className="gf-form-label width-7">Timezone</label> <label className="gf-form-label width-7">Timezone</label>
<TimeZonePicker value={dashboard.timezone} onChange={this.onTimeZoneChange} width={40} /> <TimeZonePicker
includeInternal={true}
value={dashboard.timezone}
onChange={this.onTimeZoneChange}
width={40}
/>
</div> </div>
<div className="gf-form"> <div className="gf-form">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment