-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1352 from phac-nml/display-id-for-project-drop-downs
Displaying identifiers for project drop downs
- Loading branch information
Showing
6 changed files
with
117 additions
and
46 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
83 changes: 83 additions & 0 deletions
83
src/main/webapp/resources/js/components/selects/SearchByNameAndIdSelect.tsx
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,83 @@ | ||
import React from "react"; | ||
import { Select, SelectProps, Tag, Typography } from "antd"; | ||
import { LabeledValue } from "antd/lib/select"; | ||
import { IridaBase } from "../../types/irida"; | ||
|
||
export type SelectListItem = Pick<IridaBase, "id" | "name">; | ||
export interface SelectListProps extends SelectProps { | ||
selectList: SelectListItem[]; | ||
} | ||
|
||
/** | ||
* React component for displaying a drop-down menu. | ||
* @param selectList - list that is to be displayed | ||
* @param className - class name of the select list | ||
* @param onChange - function that is called when select option has changed | ||
* @param defaultValue - identifier of the select list item that is to be displayed by default | ||
* @constructor | ||
*/ | ||
export function SearchByNameAndIdSelect({ | ||
selectList, | ||
onChange, | ||
className, | ||
defaultValue = null, | ||
}: SelectListProps): JSX.Element { | ||
const [options, setOptions] = React.useState<LabeledValue[]>(() => | ||
formatOptions(selectList) | ||
); | ||
|
||
function formatOptions(values: SelectListItem[]) { | ||
if (!values) return []; | ||
return values.map((selectListItem) => ({ | ||
label: ( | ||
<div | ||
style={{ | ||
display: "flex", | ||
justifyContent: "space-between", | ||
alignItems: "center", | ||
width: "100%", | ||
}} | ||
> | ||
<Typography.Text ellipsis={{ tooltip: true }}> | ||
{selectListItem.name} | ||
</Typography.Text> | ||
<Tag> | ||
{i18n("SearchByNameAndIdSelect.label.id", selectListItem.id)} | ||
</Tag> | ||
</div> | ||
), | ||
value: selectListItem.id, | ||
})); | ||
} | ||
|
||
React.useEffect(() => { | ||
setOptions(formatOptions(selectList)); | ||
}, [selectList]); | ||
|
||
const handleSearch = (value: string) => { | ||
const lowerValue = value.toLowerCase(); | ||
|
||
const available = selectList.filter( | ||
(selectItem: { name: string; id: { toString: () => string } }) => | ||
selectItem.name.toLowerCase().includes(lowerValue) || | ||
selectItem.id.toString() === value | ||
); | ||
const formatted = formatOptions(available); | ||
setOptions(formatted); | ||
}; | ||
|
||
return ( | ||
<Select | ||
autoFocus | ||
showSearch | ||
size="large" | ||
style={{ width: `100%` }} | ||
options={options} | ||
className={className} | ||
filterOption={false} | ||
onSearch={handleSearch} | ||
onChange={onChange} | ||
defaultValue={defaultValue} | ||
/> | ||
); | ||
} |
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