| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { memo } from 'react' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-08 21:56:09 +08:00
										 |  |  | type VariableMenuItemProps = { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   title: string | 
					
						
							|  |  |  |   icon?: JSX.Element | 
					
						
							|  |  |  |   extraElement?: JSX.Element | 
					
						
							|  |  |  |   isSelected: boolean | 
					
						
							|  |  |  |   queryString: string | null | 
					
						
							| 
									
										
										
										
											2024-07-08 21:56:09 +08:00
										 |  |  |   onClick: () => void | 
					
						
							|  |  |  |   onMouseEnter: () => void | 
					
						
							|  |  |  |   setRefElement?: (element: HTMLDivElement) => void | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | export const VariableMenuItem = memo(({ | 
					
						
							| 
									
										
										
										
											2024-07-08 21:56:09 +08:00
										 |  |  |   title, | 
					
						
							|  |  |  |   icon, | 
					
						
							|  |  |  |   extraElement, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   isSelected, | 
					
						
							| 
									
										
										
										
											2024-07-08 21:56:09 +08:00
										 |  |  |   queryString, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   onClick, | 
					
						
							|  |  |  |   onMouseEnter, | 
					
						
							| 
									
										
										
										
											2024-07-08 21:56:09 +08:00
										 |  |  |   setRefElement, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | }: VariableMenuItemProps) => { | 
					
						
							|  |  |  |   let before = title | 
					
						
							|  |  |  |   let middle = '' | 
					
						
							|  |  |  |   let after = '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (queryString) { | 
					
						
							|  |  |  |     const regex = new RegExp(queryString, 'i') | 
					
						
							| 
									
										
										
										
											2024-07-08 21:56:09 +08:00
										 |  |  |     const match = regex.exec(title) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (match) { | 
					
						
							|  |  |  |       before = title.substring(0, match.index) | 
					
						
							|  |  |  |       middle = match[0] | 
					
						
							|  |  |  |       after = title.substring(match.index + match[0].length) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <div | 
					
						
							|  |  |  |       className={`
 | 
					
						
							|  |  |  |         flex items-center px-3 h-6 rounded-md hover:bg-primary-50 cursor-pointer | 
					
						
							|  |  |  |         ${isSelected && 'bg-primary-50'} | 
					
						
							|  |  |  |       `}
 | 
					
						
							|  |  |  |       tabIndex={-1} | 
					
						
							| 
									
										
										
										
											2024-07-08 21:56:09 +08:00
										 |  |  |       ref={setRefElement} | 
					
						
							|  |  |  |       onMouseEnter={onMouseEnter} | 
					
						
							|  |  |  |       onClick={onClick}> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       <div className='mr-2'> | 
					
						
							| 
									
										
										
										
											2024-07-08 21:56:09 +08:00
										 |  |  |         {icon} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2024-07-08 21:56:09 +08:00
										 |  |  |       <div className='grow text-[13px] text-gray-900 truncate' title={title}> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         {before} | 
					
						
							|  |  |  |         <span className='text-[#2970FF]'>{middle}</span> | 
					
						
							|  |  |  |         {after} | 
					
						
							|  |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2024-07-08 21:56:09 +08:00
										 |  |  |       {extraElement} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | VariableMenuItem.displayName = 'VariableMenuItem' |