Reputation: 394
I create an async select with react-select
.I can show value get from API. but when user select one of them, how to find which one is selected.
this is loadOptions
method:
const loadOptions = (selectedOption, callback) => {
let xml = `my xml data`;
axios.post('test.com', xml, { headers: { 'Content-Type': 'text/xml;charset=UTF-8' } }).then(function (response) {
//console.log(response)
var options = {
attributeNamePrefix: "@_",
attrNodeName: "attr", //default is 'false'
textNodeName: "#text",
ignoreAttributes: true,
ignoreNameSpace: false,
allowBooleanAttributes: false,
parseNodeValue: true,
parseAttributeValue: false,
trimValues: true,
cdataTagName: "__cdata", //default is 'false'
cdataPositionChar: "\\c",
localeRange: "", //To support non english character in tag/attribute values.
parseTrueNumberOnly: false,
attrValueProcessor: a => he.decode(a, { isAttributeValue: true }),//default is a=>a
tagValueProcessor: a => he.decode(a) //default is a=>a
};
// Intermediate obj
var tObj = parser.getTraversalObj(response.data, options);
var jsonObj = parser.convertToJson(tObj, options);
if (jsonObj["soap:Envelope"]["soap:Body"].GetAllCategoriesResponse.GetAllCategoriesResult["diffgr:diffgram"].DocumentElement != null) {
var jsonDropDownDetails = jsonObj["soap:Envelope"]["soap:Body"].GetAllCategoriesResponse.GetAllCategoriesResult["diffgr:diffgram"].DocumentElement.CATEGORY
jsonDropDownDetails.map(item => {
const data = { value: item.CATEGORYNAME, label: item.CATEGORYNAME, index: item.CATEGORYID }
setDropDownOptions(dropDownOptions.push(data))
})
callback(dropDownOptions)
}
}).catch(function (error) {
console.log("erorr in DropDown : " + error)
})
};
and this is my handelChange
method:
const handleChange = selectedOption => {
console.log(selectedOption)
};
and this is my AsyncSelect:
<AsyncSelect
styles={customStyles}
cacheOptions
loadOptions={loadOptions}
defaultOptions
onInputChange={handleChange}
isRtl={true}
isSearchable={false}
classNamePrefix='myDropDown'
/>
how to callback which value is selected?
Upvotes: 0
Views: 239
Reputation: 869
Change onInputChange
to onChange
without changing your handler, selectedOption
now should work
onChange={handleChange}
Upvotes: 1