Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ describe('ScheduleItemField', () => {
expect(mockField.setValue).toHaveBeenCalledWith('5000000s');
});

it(`uses the "units" prop when it's passed`, async () => {
const mockField = useFormFieldMock<string>({ value: '7d' });
const wrapper = mount(
<TestProviders>
<ScheduleItemField
field={mockField}
units={['m', 'h', 'd']}
dataTestSubj="schedule-item"
idAria="idAria"
/>
</TestProviders>
);

expect(wrapper.find('[data-test-subj="interval"]').last().prop('value')).toEqual(7);
expect(wrapper.find('[data-test-subj="timeType"]').last().prop('value')).toEqual('d');

wrapper
.find('[data-test-subj="interval"]')
.last()
.simulate('change', { target: { value: '8' } });

expect(mockField.setValue).toHaveBeenCalledWith('8d');
});

it.each([
[-10, -5],
[-5, 0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface ScheduleItemProps {
isDisabled?: boolean;
minValue?: number;
maxValue?: number;
timeTypes?: string[];
units?: string[];
fullWidth?: boolean;
}

Expand Down Expand Up @@ -74,10 +74,10 @@ export function ScheduleItemField({
idAria,
minValue = Number.MIN_SAFE_INTEGER,
maxValue = Number.MAX_SAFE_INTEGER,
timeTypes = DEFAULT_TIME_DURATION_UNITS,
units = DEFAULT_TIME_DURATION_UNITS,
fullWidth = false,
}: ScheduleItemProps): JSX.Element {
const [timeType, setTimeType] = useState(timeTypes[0]);
const [timeType, setTimeType] = useState(units[0]);
const [timeVal, setTimeVal] = useState<number>(0);
const { isInvalid, errorMessage } = getFieldValidityAndErrorMessage(field);
const { value, setValue } = field;
Expand All @@ -92,7 +92,7 @@ export function ScheduleItemField({

const onChangeTimeVal = useCallback<NonNullable<EuiFieldNumberProps['onChange']>>(
(e) => {
const number = parseInt(e.target.value, 10);
const number = e.target.value === '' ? minValue : parseInt(e.target.value, 10);

if (Number.isNaN(number)) {
return;
Expand All @@ -112,7 +112,7 @@ export function ScheduleItemField({
}

const isNegative = value.startsWith('-');
const durationRegexp = new RegExp(`^\\-?(\\d+)(${timeTypes.join('|')})$`);
const durationRegexp = new RegExp(`^\\-?(\\d+)(${units.join('|')})$`);
const durationMatchArray = value.match(durationRegexp);

if (!durationMatchArray) {
Expand All @@ -124,7 +124,7 @@ export function ScheduleItemField({

setTimeVal(time);
setTimeType(unit);
}, [timeType, timeTypes, timeVal, value]);
}, [timeType, units, timeVal, value]);

const label = useMemo(
() => (
Expand Down Expand Up @@ -154,7 +154,7 @@ export function ScheduleItemField({
append={
<MyEuiSelect
fullWidth
options={timeTypeOptions.filter((type) => timeTypes.includes(type.value))}
options={timeTypeOptions.filter((type) => units.includes(type.value))}
value={timeType}
onChange={onChangeTimeType}
disabled={isDisabled}
Expand Down