little wrapper around the time selector (#20881)

this makes it easier to use
This commit is contained in:
Erik Jan de Wit 2023-06-12 12:53:29 +02:00 committed by GitHub
parent c771890da8
commit 4b3733de2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 20 deletions

View file

@ -9,7 +9,6 @@ import {
ToolbarItem,
} from "@patternfly/react-core";
import { useState } from "react";
import { Controller, useFormContext } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { HelpItem } from "ui-shared";
@ -22,7 +21,7 @@ import {
Action,
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 { AddHostDialog } from ".././advanced/AddHostDialog";
import { AdvancedProps, parseResult } from "../AdvancedTab";
@ -37,7 +36,6 @@ export const ClusteringPanel = ({
client: { id, registeredNodes, access },
}: AdvancedProps) => {
const { t } = useTranslation("clients");
const { control } = useFormContext();
const { addAlert, addError } = useAlerts();
const formatDate = useFormatDate();
@ -101,14 +99,7 @@ export const ClusteringPanel = ({
>
<Split hasGutter>
<SplitItem>
<Controller
name="nodeReRegistrationTimeout"
defaultValue=""
control={control}
render={({ field }) => (
<TimeSelector value={field.value} onChange={field.onChange} />
)}
/>
<TimeSelectorForm name="nodeReRegistrationTimeout" />
</SplitItem>
<SplitItem>
<Button variant={ButtonVariant.secondary} onClick={() => save()}>

View file

@ -22,15 +22,15 @@ const allTimes: TimeUnit[] = [
{ unit: "day", label: "times.days", multiplier: 86400 },
];
export type TimeSelectorProps = TextInputProps &
export type TimeSelectorProps = Omit<TextInputProps, "onChange"> &
Pick<DropdownProps, "menuAppendTo"> & {
value: number;
value?: number;
units?: Unit[];
onChange: (time: number | string) => void;
onChange?: (time: number | string) => void;
className?: string;
};
export const getTimeUnit = (value: number) =>
export const getTimeUnit = (value: number | undefined = 0) =>
allTimes.reduce(
(v, time) =>
value % time.multiplier === 0 && v.multiplier < time.multiplier
@ -73,7 +73,10 @@ export const TimeSelector = ({
const filteredUnits = units.map(
(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]);
}
return filteredUnits;
@ -86,20 +89,20 @@ export const TimeSelector = ({
setMultiplier(multiplier);
setTimeValue(value / multiplier);
} else {
setTimeValue(value);
setTimeValue(value || "");
setMultiplier(defaultMultiplier);
}
}, [value]);
}, [value, defaultMultiplier]);
const updateTimeout = (
timeout: "" | number,
times: number | undefined = multiplier
) => {
if (timeout !== "") {
onChange(timeout * (times || 1));
onChange?.(timeout * (times || 1));
setTimeValue(timeout);
} else {
onChange("");
onChange?.("");
}
};

View file

@ -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} />;
};