| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  | import { CheckIcon, XIcon } from "@heroicons/react/outline"; | 
					
						
							|  |  |  | import React, { useEffect, useState } from "react"; | 
					
						
							|  |  |  | import { MultiValue } from "react-select"; | 
					
						
							| 
									
										
										
										
											2021-09-22 19:52:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 22:20:10 +00:00
										 |  |  | import { useLocale } from "@lib/hooks/useLocale"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import Avatar from "@components/ui/Avatar"; | 
					
						
							|  |  |  | import Select from "@components/ui/form/Select"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 21:33:18 +00:00
										 |  |  | type CheckedSelectValue = { | 
					
						
							|  |  |  |   avatar: string; | 
					
						
							|  |  |  |   label: string; | 
					
						
							|  |  |  |   value: string; | 
					
						
							| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  |   disabled?: boolean; | 
					
						
							| 
									
										
										
										
											2021-09-29 21:33:18 +00:00
										 |  |  | }[]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  | export type CheckedSelectProps = { | 
					
						
							| 
									
										
										
										
											2021-09-29 21:33:18 +00:00
										 |  |  |   defaultValue?: CheckedSelectValue; | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  |   placeholder?: string; | 
					
						
							|  |  |  |   name?: string; | 
					
						
							| 
									
										
										
										
											2021-09-29 21:33:18 +00:00
										 |  |  |   options: CheckedSelectValue; | 
					
						
							|  |  |  |   onChange: (options: CheckedSelectValue) => void; | 
					
						
							|  |  |  |   disabled: boolean; | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  | export const CheckedSelect = (props: CheckedSelectProps) => { | 
					
						
							| 
									
										
										
										
											2021-09-29 21:33:18 +00:00
										 |  |  |   const [selectedOptions, setSelectedOptions] = useState<CheckedSelectValue>(props.defaultValue || []); | 
					
						
							| 
									
										
										
										
											2021-10-15 10:53:42 +00:00
										 |  |  |   const { t } = useLocale(); | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     props.onChange(selectedOptions); | 
					
						
							|  |  |  |   }, [selectedOptions]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const options = props.options.map((option) => ({ | 
					
						
							|  |  |  |     ...option, | 
					
						
							|  |  |  |     disabled: !!selectedOptions.find((selectedOption) => selectedOption.value === option.value), | 
					
						
							|  |  |  |   })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 21:33:18 +00:00
										 |  |  |   const removeOption = (value: string) => | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  |     setSelectedOptions(selectedOptions.filter((option) => option.value !== value)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  |   const changeHandler = (selections: MultiValue<CheckedSelectValue[number]>) => | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  |     selections.forEach((selected) => { | 
					
						
							|  |  |  |       if (selectedOptions.find((option) => option.value === selected.value)) { | 
					
						
							|  |  |  |         removeOption(selected.value); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       setSelectedOptions(selectedOptions.concat(selected)); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <> | 
					
						
							| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  |       <Select<CheckedSelectValue[number], true> | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  |         styles={{ | 
					
						
							|  |  |  |           option: (styles, { isDisabled }) => ({ | 
					
						
							|  |  |  |             ...styles, | 
					
						
							|  |  |  |             backgroundColor: isDisabled ? "#F5F5F5" : "inherit", | 
					
						
							|  |  |  |           }), | 
					
						
							|  |  |  |         }} | 
					
						
							|  |  |  |         name={props.name} | 
					
						
							| 
									
										
										
										
											2021-10-15 10:53:42 +00:00
										 |  |  |         placeholder={props.placeholder || t("select")} | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  |         isSearchable={false} | 
					
						
							| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  |         formatOptionLabel={({ label, avatar, disabled }) => ( | 
					
						
							|  |  |  |           <div className="flex"> | 
					
						
							|  |  |  |             <Avatar className="mr-3 h-6 w-6 rounded-full" alt={label} imageSrc={avatar} /> | 
					
						
							|  |  |  |             {label} | 
					
						
							|  |  |  |             {disabled && ( | 
					
						
							|  |  |  |               <div className="flex-grow"> | 
					
						
							|  |  |  |                 <CheckIcon className="float-right h-6 w-6 text-neutral-500" /> | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  |             )} | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         )} | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  |         options={options} | 
					
						
							|  |  |  |         isMulti | 
					
						
							| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  |         // value={props.placeholder || t("select")}
 | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  |         onChange={changeHandler} | 
					
						
							|  |  |  |       /> | 
					
						
							|  |  |  |       {selectedOptions.map((option) => ( | 
					
						
							| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  |         <div key={option.value} className="border-1 border p-2 font-medium"> | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  |           <Avatar | 
					
						
							| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  |             className="inline h-6 w-6 rounded-full ltr:mr-2 rtl:ml-2" | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  |             imageSrc={option.avatar} | 
					
						
							| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  |             alt={option.label} | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  |           /> | 
					
						
							|  |  |  |           {option.label} | 
					
						
							|  |  |  |           <XIcon | 
					
						
							|  |  |  |             onClick={() => changeHandler([option])} | 
					
						
							| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  |             className="float-right mt-0.5 h-5 w-5 cursor-pointer text-neutral-500" | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  |           /> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       ))} | 
					
						
							|  |  |  |     </> | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-09-14 08:45:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | CheckedSelect.displayName = "CheckedSelect"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default CheckedSelect; |