| 
									
										
										
										
											2020-03-27 04:48:46 +08:00
										 |  |  | import React, { useMemo } from 'react'; | 
					
						
							| 
									
										
										
										
											2020-03-29 06:11:50 +08:00
										 |  |  | import { set as lodashSet, get as lodashGet } from 'lodash'; | 
					
						
							| 
									
										
										
										
											2020-03-27 04:48:46 +08:00
										 |  |  | import { PanelPlugin } from '@grafana/data'; | 
					
						
							|  |  |  | import { Forms } from '@grafana/ui'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface PanelOptionsEditorProps<TOptions> { | 
					
						
							|  |  |  |   plugin: PanelPlugin; | 
					
						
							|  |  |  |   options: TOptions; | 
					
						
							|  |  |  |   onChange: (options: TOptions) => void; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const PanelOptionsEditor: React.FC<PanelOptionsEditorProps<any>> = ({ plugin, options, onChange }) => { | 
					
						
							|  |  |  |   const optionEditors = useMemo(() => plugin.optionEditors, [plugin]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const onOptionChange = (key: string, value: any) => { | 
					
						
							| 
									
										
										
										
											2020-03-29 06:11:50 +08:00
										 |  |  |     const newOptions = lodashSet({ ...options }, key, value); | 
					
						
							|  |  |  |     onChange(newOptions); | 
					
						
							| 
									
										
										
										
											2020-03-27 04:48:46 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <> | 
					
						
							|  |  |  |       {optionEditors.list().map(e => { | 
					
						
							|  |  |  |         return ( | 
					
						
							| 
									
										
										
										
											2020-03-29 06:11:50 +08:00
										 |  |  |           <Forms.Field label={e.name} description={e.description} key={e.id}> | 
					
						
							|  |  |  |             <e.editor value={lodashGet(options, e.id)} onChange={value => onOptionChange(e.id, value)} item={e} /> | 
					
						
							| 
									
										
										
										
											2020-03-27 04:48:46 +08:00
										 |  |  |           </Forms.Field> | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       })} | 
					
						
							|  |  |  |     </> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | }; |