import { css } from '@emotion/css'; import React from 'react'; import { useController, useFormContext } from 'react-hook-form'; import { dateTime, GrafanaTheme } from '@grafana/data'; import { Field, TimeRangeInput, useStyles } from '@grafana/ui'; import { SilenceFormFields } from '../../types/silence-form'; export const SilencePeriod = () => { const { control, getValues } = useFormContext(); const styles = useStyles(getStyles); const { field: { onChange: onChangeStartsAt, value: startsAt }, fieldState: { invalid: startsAtInvalid }, } = useController({ name: 'startsAt', control, rules: { validate: (value) => getValues().endsAt > value, }, }); const { field: { onChange: onChangeEndsAt, value: endsAt }, fieldState: { invalid: endsAtInvalid }, } = useController({ name: 'endsAt', control, rules: { validate: (value) => getValues().startsAt < value, }, }); const { field: { onChange: onChangeTimeZone, value: timeZone }, } = useController({ name: 'timeZone', control, }); const invalid = startsAtInvalid || endsAtInvalid; const from = dateTime(startsAt); const to = dateTime(endsAt); return ( { onChangeStartsAt(dateTime(newValue.from)); onChangeEndsAt(dateTime(newValue.to)); }} onChangeTimeZone={(newValue) => onChangeTimeZone(newValue)} hideTimeZone={false} hideQuickRanges={true} placeholder={'Select time range'} /> ); }; const getStyles = (theme: GrafanaTheme) => ({ timeRange: css` width: 400px; `, });