[Fleet] Fix host input with empty value (#101178)

This commit is contained in:
Nicolas Chaulet 2021-06-02 13:44:04 -04:00 committed by GitHub
parent e607b58590
commit 66553681c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View file

@ -66,3 +66,12 @@ test('it should allow to update existing host with multiple hosts', async () =>
fireEvent.change(inputEl, { target: { value: 'http://newhost.com' } });
expect(mockOnChange).toHaveBeenCalledWith(['http://newhost.com', 'http://host2.com']);
});
test('it should render an input if there is not hosts', async () => {
const { utils, mockOnChange } = renderInput([]);
const inputEl = await utils.findByDisplayValue('');
expect(inputEl).toBeDefined();
fireEvent.change(inputEl, { target: { value: 'http://newhost.com' } });
expect(mockOnChange).toHaveBeenCalledWith(['http://newhost.com']);
});

View file

@ -132,7 +132,7 @@ const SortableTextField: FunctionComponent<SortableTextFieldProps> = React.memo(
export const HostsInput: FunctionComponent<Props> = ({
id,
value,
value: valueFromProps,
onChange,
helpText,
label,
@ -140,6 +140,10 @@ export const HostsInput: FunctionComponent<Props> = ({
errors,
}) => {
const [autoFocus, setAutoFocus] = useState(false);
const value = useMemo(() => {
return valueFromProps.length ? valueFromProps : [''];
}, [valueFromProps]);
const rows = useMemo(
() =>
value.map((host, idx) => ({