-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
372 additions
and
556 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import AsyncSelect from 'react-select/async'; | ||
|
||
const TS_BASE = 'https://service.tib.eu/ts4tib/api/select'; | ||
const TS_PARAM = [ | ||
'rows=20', | ||
'collection=nfdi4chem', | ||
'obsoletes=false', | ||
'local=false', | ||
'fieldList=id,iri,label,short_form,obo_id,ontology_name,ontology_prefix,description,type', | ||
].join('&'); | ||
|
||
const constructOption = data => { | ||
if (data) { | ||
const desc = data.description?.join('') || ''; | ||
return { | ||
value: data.id, | ||
label: ( | ||
<> | ||
{data.label} | ||
<span> | ||
<span className="gu-ontology-select-code-short badge"> | ||
{data.short_form} | ||
</span> | ||
| ||
<span className="gu-ontology-select-code-prefix badge"> | ||
{data.ontology_prefix} | ||
</span> | ||
{desc ? ( | ||
<> | ||
<br /> | ||
<span style={{ fontSize: '11px' }}>{desc}</span> | ||
</> | ||
) : null} | ||
</span> | ||
</> | ||
), | ||
}; | ||
} | ||
return data; | ||
}; | ||
|
||
const constructOptions = data => { | ||
if (!data) { | ||
return []; | ||
} | ||
return data.map(d => { | ||
const option = Object.assign(constructOption(d), { data: d }); | ||
return option; | ||
}); | ||
}; | ||
|
||
const OntologySelect = props => { | ||
const { fnSelected, defaultValue } = props; | ||
const [data, setData] = useState(null); | ||
|
||
const fetchData = async inputValue => { | ||
try { | ||
if (!inputValue.trim() || inputValue.length < 2) { | ||
return []; | ||
} | ||
|
||
const response = await fetch(`${TS_BASE}?q=${inputValue}&${TS_PARAM}`); | ||
if (!response.ok) { | ||
throw new Error('Network request failed'); | ||
} | ||
const result = await response.json(); | ||
return constructOptions(result?.response?.docs); | ||
} catch (error) { | ||
console.error('Error fetching data:', error); | ||
return []; | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
fetchData('').then(setData); | ||
}, []); | ||
|
||
return ( | ||
<AsyncSelect | ||
backspaceRemoves | ||
isClearable | ||
value={defaultValue} | ||
valueKey="value" | ||
labelKey="label" | ||
loadOptions={(inputValue, callback) => { | ||
fetchData(inputValue).then(options => callback(options)); | ||
}} | ||
onChange={selected => { | ||
fnSelected(selected); | ||
}} | ||
styles={{ | ||
container: baseStyles => { | ||
return { | ||
...baseStyles, | ||
width: '100%', | ||
}; | ||
}, | ||
control: base => { | ||
return { | ||
...base, | ||
height: '36px', | ||
minHeight: '36px', | ||
minWidth: '200px', | ||
border: '1px solid #ccc', | ||
}; | ||
}, | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export default OntologySelect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.