Sha256: b5e0adb2a53dd09d24878757c4b000d6a07a3dfd9604004bd714621f5acca0d3
Contents?: true
Size: 1.8 KB
Versions: 40
Compression:
Stored size: 1.8 KB
Contents
import React, { useState } from 'react'; import PropTypes from 'prop-types'; import { SearchAutocomplete } from 'foremanReact/components/SearchBar/SearchAutocomplete'; import { useAPI } from 'foremanReact/common/hooks/API/APIHooks'; import { STATUS } from 'foremanReact/constants'; import { noop } from 'foremanReact/common/helpers'; const SearchText = ({ data: { autocomplete: { url, apiParams } = { url: '' }, disabled, }, initialQuery, onSearchChange, name, }) => { const [search, setSearch] = useState(initialQuery || ''); const getAPIparams = input => ({ ...apiParams(input) }); const { response, status, setAPIOptions } = useAPI('get', url, { params: getAPIparams(search), }); const onChange = (newValue) => { onSearchChange(newValue); setSearch(newValue); setAPIOptions({ params: { ...getAPIparams(newValue) } }); }; const error = status === STATUS.ERROR || response?.[0]?.error ? response?.[0]?.error || response.message : null; let results = []; if (Array.isArray(response) && !error) { results = response.map(item => ({ label: item, category: '' })); } return ( <div className="foreman-search-text"> <SearchAutocomplete results={results} onSearchChange={onChange} value={search} disabled={disabled} error={error} name={name} /> </div> ); }; SearchText.propTypes = { data: PropTypes.shape({ autocomplete: PropTypes.shape({ url: PropTypes.string.isRequired, apiParams: PropTypes.func, }).isRequired, disabled: PropTypes.bool, }).isRequired, initialQuery: PropTypes.string, onSearchChange: PropTypes.func, name: PropTypes.string, }; SearchText.defaultProps = { initialQuery: '', onSearchChange: noop, name: null, }; export default SearchText;
Version data entries
40 entries across 40 versions & 1 rubygems