import { css } from '@emotion/css'; import React, { FC } from 'react'; import { useFieldArray, useFormContext } from 'react-hook-form'; import { GrafanaTheme2 } from '@grafana/data'; import { Field, InlineFieldRow, InlineField, Input, Button, IconButton, useStyles2 } from '@grafana/ui'; import { MuteTimingFields } from '../../types/mute-timing-form'; interface Props { intervalIndex: number; } export const MuteTimingTimeRange: FC = ({ intervalIndex }) => { const styles = useStyles2(getStyles); const { register, formState } = useFormContext(); const { fields: timeRanges, append: addTimeRange, remove: removeTimeRange, } = useFieldArray({ name: `time_intervals.${intervalIndex}.times`, }); const validateTime = (timeString: string) => { if (!timeString) { return true; } const [hour, minutes] = timeString.split(':').map((x) => parseInt(x, 10)); const isHourValid = hour >= 0 && hour < 25; const isMinuteValid = minutes > -1 && minutes < 60; const isTimeValid = hour === 24 ? minutes === 0 : isHourValid && isMinuteValid; return isTimeValid || 'Time is invalid'; }; const formErrors = formState.errors.time_intervals?.[intervalIndex]; const timeRangeInvalid = formErrors?.times?.some((value) => value?.start_time || value?.end_time) ?? false; return (
<> {timeRanges.map((timeRange, index) => { return (
{ e.preventDefault(); removeTimeRange(index); }} />
); })}
); }; const getStyles = (theme: GrafanaTheme2) => ({ field: css` margin-bottom: 0; `, timeRange: css` margin-bottom: ${theme.spacing(1)}; `, timeRangeInput: css` width: 120px; `, deleteTimeRange: css` margin: ${theme.spacing(1)} 0 0 ${theme.spacing(0.5)}; `, addTimeRange: css` margin-bottom: ${theme.spacing(2)}; `, });