File indexing completed on 2025-05-04 05:30:58
0001 import React, { useState } from 'react'; 0002 import Autosuggest from 'react-autosuggest'; 0003 0004 function renderSuggestion(suggestion) { 0005 return ( 0006 <div className={'suggestionsContainer'}> 0007 <div> 0008 {<img style={{ width: '50px', height: '50px' }} src={suggestion.image_small} ></img>} 0009 </div> 0010 <div className="description"> 0011 <> 0012 <span>{suggestion.title}</span> 0013 <span style={{ fontSize: '11px', color: '#ccc',lineHeight:'15px' }}>{' by ' + suggestion.username}</span> 0014 <span style={{ fontSize: '11px', 'color': '#ccc',lineHeight:'15px' }}>{suggestion.cat_title}</span> 0015 </> 0016 </div> 0017 </div> 0018 ); 0019 } 0020 0021 const renderInputComponent = inputProps => ( 0022 <div className="react-autosuggest__inputContainer" > 0023 <a onClick={inputProps.onSubmit}> 0024 <img className="react-autosuggest__icon" src={inputProps.baseUrlStore + "/theme/flatui/img/icon-search-input-2.png"} /> 0025 </a> 0026 <input {...inputProps} /> 0027 </div> 0028 ); 0029 0030 const SearchProductInput = (props) => { 0031 const [searchText, setSearchText] = useState(''); 0032 const [isShow, setIsShow] = useState(false); 0033 const [isLoading, setIsLoading] = useState(false); 0034 const [value, setValue] = useState(''); 0035 const [suggestions, setSuggestions] = useState([]); 0036 const [selected, setSelected] = useState({}); 0037 0038 const [projectCategoryId, setProjectCategoryId] = useState(props.product.project_category_id); 0039 0040 0041 const loadSuggestions = value => { 0042 const inputLength = value.length; 0043 if (inputLength < 3) return; 0044 setIsLoading(true); 0045 let url = props.baseUrlStore + '/json/searchp/p/' + value+'/c/'+projectCategoryId; 0046 if (props.store) { 0047 url += '/s/' + props.store 0048 } 0049 fetch(url, { 0050 mode: 'cors', 0051 credentials: 'include' 0052 }) 0053 .then(response => response.json()) 0054 .then(data => { 0055 setSuggestions(data); 0056 setIsLoading(false); 0057 }); 0058 0059 } 0060 0061 0062 const onSearchFormSubmit = e => { 0063 e.preventDefault(); 0064 e.stopPropagation(); 0065 } 0066 // const onKeyDown = event => { 0067 // switch (event.keyCode) { 0068 // case KeyCodes.ENTER: { 0069 // event.preventDefault(); 0070 // } 0071 // } 0072 // }; 0073 0074 const getSuggestionValue = suggestion => { 0075 setSelected(suggestion); 0076 //setProject_id(suggestion.project_id); 0077 props.setProjectId(suggestion.project_id); 0078 return suggestion.title; 0079 } 0080 0081 0082 const onHandleChange = (event, { newValue, method }) => { 0083 setValue(newValue); 0084 } 0085 0086 const shouldRenderSuggestions = value => { 0087 return value.trim().length > 2; 0088 } 0089 0090 const onSuggestionsFetchRequested = ({ value }) => { 0091 loadSuggestions(value); 0092 } 0093 0094 const onSuggestionsClearRequested = () => { 0095 setSuggestions([]); 0096 } 0097 0098 const onSuggestionSelected = (event, { suggestion, suggestionValue, suggestionIndex, sectionIndex, method }) => { 0099 setSelected(suggestion); 0100 //setProject_id(suggestion.project_id); 0101 props.setProjectId(suggestion.project_id); 0102 if (method === 'enter') { 0103 event.preventDefault(); 0104 } 0105 } 0106 0107 0108 const inputProps = { 0109 placeholder: "Search...", 0110 value, 0111 onChange: onHandleChange, 0112 onSubmit: onSearchFormSubmit, 0113 baseUrlStore: props.baseUrlStore, 0114 }; 0115 0116 return ( 0117 0118 <div className="autosuggest"> 0119 <div className="row"> 0120 <div className="col-lg-12"><h6>ID of product on pling:</h6> </div> 0121 <div className="col-lg-12"> 0122 <div style={{display:'flex'}}> 0123 <div> 0124 <input required name="project_id" id="project_id" 0125 value={props.project_id} 0126 style={{width:'100px',marginRight:'10px'}} 0127 onChange={props.handleInputProjectIdChange} 0128 ></input> 0129 </div> 0130 <div> 0131 <Autosuggest 0132 suggestions={suggestions} 0133 onSuggestionsFetchRequested={onSuggestionsFetchRequested} 0134 onSuggestionsClearRequested={onSuggestionsClearRequested} 0135 shouldRenderSuggestions={shouldRenderSuggestions} 0136 onSuggestionSelected={onSuggestionSelected} 0137 getSuggestionValue={getSuggestionValue} 0138 renderSuggestion={renderSuggestion} 0139 inputProps={inputProps} 0140 renderInputComponent={renderInputComponent} 0141 /> 0142 </div> 0143 </div> 0144 0145 </div> 0146 </div> 0147 0148 <div className="row"> 0149 <div className="col-lg-12"> 0150 {selected && selected.project_id && 0151 <div className='suggestionsContainer'> 0152 <div> 0153 <img style={{ width: '50px', height: '50px' }} src={selected.image_small} ></img> 0154 </div> 0155 <div className="description"> 0156 <> 0157 <span>{selected.title}</span> 0158 <span style={{ fontSize: '11px', color: '#ccc',lineHeight:'15px' }}>{' by ' + selected.username}</span> 0159 <span style={{ fontSize: '11px', color: '#ccc',lineHeight:'15px' }}>{selected.cat_title}</span> 0160 </> 0161 </div> 0162 </div> 0163 } 0164 </div> 0165 </div> 0166 0167 0168 0169 </div> 0170 0171 ) 0172 } 0173 export default SearchProductInput 0174