@@ -5,23 +5,25 @@ import { action } from '@/utils/action';
55// CSF format story
66export const selectedItem = ( ) => {
77 const medicineList = [
8- { label : 'Aspirin' , value : 'Aspirin' , group : 'Painkillers' } ,
9- { label : 'Paracetamol' , value : 'Paracetamol' , group : 'Painkillers' } ,
10- { label : 'Lisinopril' , value : 'Lisinopril' , group : 'Hypertension' } ,
11- { label : 'Simvastatin' , value : 'Simvastatin' , group : 'Antibiotics' } ,
12- { label : 'Amoxicillin' , value : 'Amoxicillin' , group : 'Antibiotics' } ,
13- { label : 'Ciprofloxacin' , value : 'Ciprofloxacin' , group : 'Antibiotics' } ,
14- { label : 'Omeprazole' , value : 'Omeprazole' , group : 'Painkillers' } ,
15- { label : 'Diazepam' , value : 'Diazepam' , group : 'Antibiotics' } ,
16- { label : 'Levothyroxine' , value : 'Levothyroxine' , group : 'Antibiotics' } ,
17- { label : 'Ibuprofen' , value : 'Ibuprofen' , group : 'Painkillers' } ,
18- { label : 'Prednisone' , value : 'Prednisone' , group : 'Painkillers' } ,
19- { label : 'Metoprolol' , value : 'Metoprolol' , group : 'Hypertension' } ,
8+ { id : 'aspirin' , label : 'Aspirin' , value : 'Aspirin' , group : 'Painkillers' } ,
9+ { id : 'paracetamol' , label : 'Paracetamol' , value : 'Paracetamol' , group : 'Painkillers' } ,
10+ { id : 'lisinopril' , label : 'Lisinopril' , value : 'Lisinopril' , group : 'Hypertension' } ,
11+ { id : 'simvastatin' , label : 'Simvastatin' , value : 'Simvastatin' , group : 'Antibiotics' } ,
12+ { id : 'amoxicillin' , label : 'Amoxicillin' , value : 'Amoxicillin' , group : 'Antibiotics' } ,
13+ { id : 'ciprofloxacin' , label : 'Ciprofloxacin' , value : 'Ciprofloxacin' , group : 'Antibiotics' } ,
14+ { id : 'omeprazole' , label : 'Omeprazole' , value : 'Omeprazole' , group : 'Painkillers' } ,
15+ { id : 'diazepam' , label : 'Diazepam' , value : 'Diazepam' , group : 'Antibiotics' } ,
16+ { id : 'levothyroxine' , label : 'Levothyroxine' , value : 'Levothyroxine' , group : 'Antibiotics' } ,
17+ { id : 'ibuprofen' , label : 'Ibuprofen' , value : 'Ibuprofen' , group : 'Painkillers' } ,
18+ { id : 'prednisone' , label : 'Prednisone' , value : 'Prednisone' , group : 'Painkillers' } ,
19+ { id : 'metoprolol' , label : 'Metoprolol' , value : 'Metoprolol' , group : 'Hypertension' } ,
2020 ] ;
2121
2222 const [ selectedOptions , setSelectedOptions ] = React . useState ( [ ] ) ;
2323 const selectRef = React . useRef ( null ) ;
2424
25+ const getOptionIdentifier = ( option ) => option ?. id ?? option ?. value ;
26+
2527 const handleSelect = ( selectedOption ) => {
2628 action ( 'selectedOption' , selectedOption ) ;
2729 selectRef . current . setFocusFirstItem ( ) ;
@@ -33,7 +35,10 @@ export const selectedItem = () => {
3335 } ;
3436
3537 const groupedMedicine = medicineList . reduce ( ( acc , item ) => {
36- const groupKey = selectedOptions . find ( ( opt ) => opt . value === item . value ) ? 'Selected Items' : item . group ;
38+ const itemIdentifier = getOptionIdentifier ( item ) ;
39+ const groupKey = selectedOptions . find ( ( opt ) => getOptionIdentifier ( opt ) === itemIdentifier )
40+ ? 'Selected Items'
41+ : item . group ;
3742 if ( ! acc [ groupKey ] ) {
3843 acc [ groupKey ] = [ ] ;
3944 }
@@ -56,7 +61,7 @@ export const selectedItem = () => {
5661 Selected Items
5762 </ Text >
5863 { selectedOptions . map ( ( option ) => (
59- < Select . Option key = { option . value } option = { option } >
64+ < Select . Option key = { getOptionIdentifier ( option ) } option = { option } >
6065 { option . label }
6166 </ Select . Option >
6267 ) ) }
@@ -71,7 +76,10 @@ export const selectedItem = () => {
7176 { group }
7277 </ Text >
7378 { groupedMedicine [ group ] . map ( ( item ) => (
74- < Select . Option key = { item . value } option = { { label : item . label , value : item . value } } >
79+ < Select . Option
80+ key = { getOptionIdentifier ( item ) }
81+ option = { { label : item . label , value : item . value , id : item . id } }
82+ >
7583 { item . label }
7684 </ Select . Option >
7785 ) ) }
@@ -85,18 +93,18 @@ export const selectedItem = () => {
8593
8694const customCode = `() => {
8795 const medicineList = [
88- { label: 'Aspirin', value: 'Aspirin', group: 'Painkillers' },
89- { label: 'Paracetamol', value: 'Paracetamol', group: 'Painkillers' },
90- { label: 'Lisinopril', value: 'Lisinopril', group: 'Hypertension' },
91- { label: 'Simvastatin', value: 'Simvastatin', group: 'Antibiotics' },
92- { label: 'Amoxicillin', value: 'Amoxicillin', group: 'Antibiotics' },
93- { label: 'Ciprofloxacin', value: 'Ciprofloxacin', group: 'Antibiotics' },
94- { label: 'Omeprazole', value: 'Omeprazole', group: 'Painkillers' },
95- { label: 'Diazepam', value: 'Diazepam', group: 'Antibiotics' },
96- { label: 'Levothyroxine', value: 'Levothyroxine', group: 'Antibiotics' },
97- { label: 'Ibuprofen', value: 'Ibuprofen', group: 'Painkillers' },
98- { label: 'Prednisone', value: 'Prednisone', group: 'Painkillers' },
99- { label: 'Metoprolol', value: 'Metoprolol', group: 'Hypertension' },
96+ { id: 'aspirin', label: 'Aspirin', value: 'Aspirin', group: 'Painkillers' },
97+ { id: 'paracetamol', label: 'Paracetamol', value: 'Paracetamol', group: 'Painkillers' },
98+ { id: 'lisinopril', label: 'Lisinopril', value: 'Lisinopril', group: 'Hypertension' },
99+ { id: 'simvastatin', label: 'Simvastatin', value: 'Simvastatin', group: 'Antibiotics' },
100+ { id: 'amoxicillin', label: 'Amoxicillin', value: 'Amoxicillin', group: 'Antibiotics' },
101+ { id: 'ciprofloxacin', label: 'Ciprofloxacin', value: 'Ciprofloxacin', group: 'Antibiotics' },
102+ { id: 'omeprazole', label: 'Omeprazole', value: 'Omeprazole', group: 'Painkillers' },
103+ { id: 'diazepam', label: 'Diazepam', value: 'Diazepam', group: 'Antibiotics' },
104+ { id: 'levothyroxine', label: 'Levothyroxine', value: 'Levothyroxine', group: 'Antibiotics' },
105+ { id: 'ibuprofen', label: 'Ibuprofen', value: 'Ibuprofen', group: 'Painkillers' },
106+ { id: 'prednisone', label: 'Prednisone', value: 'Prednisone', group: 'Painkillers' },
107+ { id: 'metoprolol', label: 'Metoprolol', value: 'Metoprolol', group: 'Hypertension' },
100108 ];
101109
102110 const [selectedOptions, setSelectedOptions] = React.useState([]);
@@ -112,8 +120,13 @@ const customCode = `() => {
112120 setSelectedOptions([]);
113121 };
114122
123+ const getOptionIdentifier = (option) => option?.id ?? option?.value;
124+
115125 const groupedMedicine = medicineList.reduce((acc, item) => {
116- const groupKey = selectedOptions.find((opt) => opt.value === item.value) ? 'Selected Items' : item.group;
126+ const itemIdentifier = getOptionIdentifier(item);
127+ const groupKey = selectedOptions.find((opt) => getOptionIdentifier(opt) === itemIdentifier)
128+ ? 'Selected Items'
129+ : item.group;
117130 if (!acc[groupKey]) {
118131 acc[groupKey] = [];
119132 }
@@ -136,7 +149,7 @@ const customCode = `() => {
136149 Selected Items
137150 </Text>
138151 {selectedOptions.map((option) => (
139- <Select.Option key={option.value } option={option}>
152+ <Select.Option key={getOptionIdentifier( option) } option={option}>
140153 {option.label}
141154 </Select.Option>
142155 ))}
@@ -155,7 +168,10 @@ const customCode = `() => {
155168 {group}
156169 </Text>
157170 {groupedMedicine[group].map((item) => (
158- <Select.Option key={item.value} option={{ label: item.label, value: item.value }}>
171+ <Select.Option
172+ key={getOptionIdentifier(item)}
173+ option={{ label: item.label, value: item.value, id: item.id }}
174+ >
159175 {item.label}
160176 </Select.Option>
161177 ))}
0 commit comments