little wrapper around the time selector (#20881)
this makes it easier to use
This commit is contained in:
parent
c771890da8
commit
4b3733de2e
3 changed files with 26 additions and 20 deletions
|
@ -9,7 +9,6 @@ import {
|
||||||
ToolbarItem,
|
ToolbarItem,
|
||||||
} from "@patternfly/react-core";
|
} from "@patternfly/react-core";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { Controller, useFormContext } from "react-hook-form";
|
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { HelpItem } from "ui-shared";
|
import { HelpItem } from "ui-shared";
|
||||||
|
|
||||||
|
@ -22,7 +21,7 @@ import {
|
||||||
Action,
|
Action,
|
||||||
KeycloakDataTable,
|
KeycloakDataTable,
|
||||||
} from "../../components/table-toolbar/KeycloakDataTable";
|
} from "../../components/table-toolbar/KeycloakDataTable";
|
||||||
import { TimeSelector } from "../../components/time-selector/TimeSelector";
|
import { TimeSelectorForm } from "../../components/time-selector/TimeSelectorForm";
|
||||||
import useFormatDate, { FORMAT_DATE_AND_TIME } from "../../utils/useFormatDate";
|
import useFormatDate, { FORMAT_DATE_AND_TIME } from "../../utils/useFormatDate";
|
||||||
import { AddHostDialog } from ".././advanced/AddHostDialog";
|
import { AddHostDialog } from ".././advanced/AddHostDialog";
|
||||||
import { AdvancedProps, parseResult } from "../AdvancedTab";
|
import { AdvancedProps, parseResult } from "../AdvancedTab";
|
||||||
|
@ -37,7 +36,6 @@ export const ClusteringPanel = ({
|
||||||
client: { id, registeredNodes, access },
|
client: { id, registeredNodes, access },
|
||||||
}: AdvancedProps) => {
|
}: AdvancedProps) => {
|
||||||
const { t } = useTranslation("clients");
|
const { t } = useTranslation("clients");
|
||||||
const { control } = useFormContext();
|
|
||||||
const { addAlert, addError } = useAlerts();
|
const { addAlert, addError } = useAlerts();
|
||||||
const formatDate = useFormatDate();
|
const formatDate = useFormatDate();
|
||||||
|
|
||||||
|
@ -101,14 +99,7 @@ export const ClusteringPanel = ({
|
||||||
>
|
>
|
||||||
<Split hasGutter>
|
<Split hasGutter>
|
||||||
<SplitItem>
|
<SplitItem>
|
||||||
<Controller
|
<TimeSelectorForm name="nodeReRegistrationTimeout" />
|
||||||
name="nodeReRegistrationTimeout"
|
|
||||||
defaultValue=""
|
|
||||||
control={control}
|
|
||||||
render={({ field }) => (
|
|
||||||
<TimeSelector value={field.value} onChange={field.onChange} />
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
</SplitItem>
|
</SplitItem>
|
||||||
<SplitItem>
|
<SplitItem>
|
||||||
<Button variant={ButtonVariant.secondary} onClick={() => save()}>
|
<Button variant={ButtonVariant.secondary} onClick={() => save()}>
|
||||||
|
|
|
@ -22,15 +22,15 @@ const allTimes: TimeUnit[] = [
|
||||||
{ unit: "day", label: "times.days", multiplier: 86400 },
|
{ unit: "day", label: "times.days", multiplier: 86400 },
|
||||||
];
|
];
|
||||||
|
|
||||||
export type TimeSelectorProps = TextInputProps &
|
export type TimeSelectorProps = Omit<TextInputProps, "onChange"> &
|
||||||
Pick<DropdownProps, "menuAppendTo"> & {
|
Pick<DropdownProps, "menuAppendTo"> & {
|
||||||
value: number;
|
value?: number;
|
||||||
units?: Unit[];
|
units?: Unit[];
|
||||||
onChange: (time: number | string) => void;
|
onChange?: (time: number | string) => void;
|
||||||
className?: string;
|
className?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getTimeUnit = (value: number) =>
|
export const getTimeUnit = (value: number | undefined = 0) =>
|
||||||
allTimes.reduce(
|
allTimes.reduce(
|
||||||
(v, time) =>
|
(v, time) =>
|
||||||
value % time.multiplier === 0 && v.multiplier < time.multiplier
|
value % time.multiplier === 0 && v.multiplier < time.multiplier
|
||||||
|
@ -73,7 +73,10 @@ export const TimeSelector = ({
|
||||||
const filteredUnits = units.map(
|
const filteredUnits = units.map(
|
||||||
(unit) => allTimes.find((time) => time.unit === unit)!
|
(unit) => allTimes.find((time) => time.unit === unit)!
|
||||||
);
|
);
|
||||||
if (!filteredUnits.every((u) => u.multiplier === multiplier)) {
|
if (
|
||||||
|
!filteredUnits.every((u) => u.multiplier === multiplier) &&
|
||||||
|
filteredUnits[0] !== allTimes[0]
|
||||||
|
) {
|
||||||
filteredUnits.unshift(allTimes[0]);
|
filteredUnits.unshift(allTimes[0]);
|
||||||
}
|
}
|
||||||
return filteredUnits;
|
return filteredUnits;
|
||||||
|
@ -86,20 +89,20 @@ export const TimeSelector = ({
|
||||||
setMultiplier(multiplier);
|
setMultiplier(multiplier);
|
||||||
setTimeValue(value / multiplier);
|
setTimeValue(value / multiplier);
|
||||||
} else {
|
} else {
|
||||||
setTimeValue(value);
|
setTimeValue(value || "");
|
||||||
setMultiplier(defaultMultiplier);
|
setMultiplier(defaultMultiplier);
|
||||||
}
|
}
|
||||||
}, [value]);
|
}, [value, defaultMultiplier]);
|
||||||
|
|
||||||
const updateTimeout = (
|
const updateTimeout = (
|
||||||
timeout: "" | number,
|
timeout: "" | number,
|
||||||
times: number | undefined = multiplier
|
times: number | undefined = multiplier
|
||||||
) => {
|
) => {
|
||||||
if (timeout !== "") {
|
if (timeout !== "") {
|
||||||
onChange(timeout * (times || 1));
|
onChange?.(timeout * (times || 1));
|
||||||
setTimeValue(timeout);
|
setTimeValue(timeout);
|
||||||
} else {
|
} else {
|
||||||
onChange("");
|
onChange?.("");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
import { useController } from "react-hook-form";
|
||||||
|
import { TimeSelector, TimeSelectorProps } from "./TimeSelector";
|
||||||
|
|
||||||
|
type TimeSelectorFormProps = TimeSelectorProps & {
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const TimeSelectorForm = (props: TimeSelectorFormProps) => {
|
||||||
|
const { field } = useController(props);
|
||||||
|
|
||||||
|
return <TimeSelector {...props} {...field} />;
|
||||||
|
};
|
Loading…
Reference in a new issue