import React, { useEffect } from 'react'; import { useSelector, useDispatch } from 'react-redux'; import { FormGroup, TextArea } from '@patternfly/react-core'; import PropTypes from 'prop-types'; import SearchBar from 'foremanReact/components/SearchBar'; import { getControllerSearchProps } from 'foremanReact/constants'; import { TRIGGERS } from 'foremanReact/components/AutoComplete/AutoCompleteConstants'; import { getResults } from 'foremanReact/components/AutoComplete/AutoCompleteActions'; import { helpLabel } from './FormHelpers'; import { SelectField } from './SelectField'; import { ResourceSelectAPI } from './ResourceSelect'; import { DateTimePicker } from '../form/DateTimePicker'; import { noop } from '../../../helpers'; const TemplateSearchField = ({ name, controller, url, labelText, required, defaultValue, setValue, values, }) => { const searchQuery = useSelector( state => state.autocomplete?.[name]?.searchQuery ); const dispatch = useDispatch(); useEffect(() => { setValue({ ...values, [name]: searchQuery }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [searchQuery]); const id = name.replace(/ /g, '-'); const props = getControllerSearchProps(controller.replace('/', '_'), name); const setSearch = newSearchQuery => { dispatch( getResults({ url, searchQuery: newSearchQuery, controller, trigger: TRIGGERS.INPUT_CHANGE, id: name, }) ); }; return ( setSearch(search)} /> ); }; export const formatter = (input, values, setValue) => { const isSelectType = !!input?.options; const inputType = input.value_type; const isTextType = inputType === 'plain' || !inputType; // null defaults to plain const { name, required, hidden_value: hidden, resource_type: resourceType, value_type: valueType, } = input; const labelText = input.description; const value = values[name]; const id = name.replace(/ /g, '-'); if (valueType === 'resource') { return ( setValue({ ...values, [name]: newValue })} /> ); } if (isSelectType) { const options = input.options.split(/\r?\n/).map(option => option.trim()); return ( setValue({ ...values, [name]: newValue })} /> ); } if (isTextType) { return (