-
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.
feat: Add no relevant option to analogue.
- Loading branch information
1 parent
1751138
commit e61bf22
Showing
5 changed files
with
81 additions
and
41 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
67 changes: 67 additions & 0 deletions
67
src/features/AddModel/ModelMetadata/AnalogueSelect/AnalogueSelect.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,67 @@ | ||
/* eslint-disable max-lines-per-function */ | ||
/* eslint-disable react/prop-types */ | ||
|
||
import { Autocomplete, AutocompleteChanges } from '@equinor/eds-core-react'; | ||
import { AnalogueList, AnalogueModelDetail } from '../../../../api/generated'; | ||
|
||
export const AnalogueSelect = ({ | ||
errors, | ||
data, | ||
analogue, | ||
metadata, | ||
setMetadata, | ||
}: { | ||
errors: string | undefined; | ||
data: AnalogueList[]; | ||
analogue: AnalogueList[] | undefined; | ||
metadata: AnalogueModelDetail; | ||
setMetadata: (metadata: AnalogueModelDetail) => void; | ||
}) => { | ||
const emptyOption: AnalogueList[] = [ | ||
{ | ||
analogueId: 'Analogue', | ||
name: 'NoRelevant', | ||
description: 'Not relevant', | ||
}, | ||
]; | ||
|
||
const selectedOptions = | ||
metadata.analogues.length > 0 ? metadata.analogues : emptyOption; | ||
|
||
const optionList: AnalogueList[] = | ||
data.length > 0 ? emptyOption.concat(data) : emptyOption; | ||
|
||
const intersection = optionList.filter( | ||
(a) => | ||
selectedOptions && | ||
selectedOptions.some((b) => JSON.stringify(a) === JSON.stringify(b)), | ||
); | ||
|
||
function handleAddMetadata(e: AutocompleteChanges<AnalogueList>) { | ||
const removeNotSelected = e.selectedItems.filter( | ||
(i) => i.analogueId !== 'Analogue', | ||
); | ||
const newList = [...removeNotSelected]; | ||
|
||
setMetadata({ | ||
...metadata, | ||
analogues: [...newList], | ||
}); | ||
} | ||
|
||
return ( | ||
<Autocomplete | ||
variant={errors ? 'error' : undefined} | ||
label="Analogue" | ||
options={optionList} | ||
optionLabel={(option) => option.name} | ||
multiple | ||
initialSelectedOptions={ | ||
intersection.length === 0 ? emptyOption : intersection | ||
} | ||
onOptionsChange={(e: AutocompleteChanges<AnalogueList>) => | ||
handleAddMetadata(e) | ||
} | ||
/> | ||
); | ||
}; |
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