diff --git a/docs/docs/components/select.mdx b/docs/docs/components/select.mdx index f20763f..de50f98 100644 --- a/docs/docs/components/select.mdx +++ b/docs/docs/components/select.mdx @@ -40,6 +40,32 @@ const options = [ ]} /> +##### Select with Label + +```jsx +const options = [ + { label: "First", value: "first" }, + { label: "Second", value: "second", disabled: true }, + { label: "Third", value: "third" }, + { label: "Fourth", value: "fourth" } +]; + +
+ + ##### Select Rounded ```jsx @@ -307,6 +333,7 @@ const options = [ | Attributes | Values | Default Value | Optional ? | | :-------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------: | :--------------: | ---------: | | placeholder | `string` | `Pick one` | Yes | +| label | `string` | | Yes | | options | `Array of` **_[Options Object](#option-object)_** | | No | | value | **_[Options Object](#option-object)_**
when isMulti is `true` then value is
Array of **_[Options Object](#option-object)_** | | Yes | | isMulti | `boolean` | `false` | Yes | diff --git a/packages/react/src/components/Select/Pill/Pill.tsx b/packages/react/src/components/Select/Pill/Pill.tsx index 0b021f1..5d1e67d 100644 --- a/packages/react/src/components/Select/Pill/Pill.tsx +++ b/packages/react/src/components/Select/Pill/Pill.tsx @@ -10,7 +10,7 @@ type pillPropTypes = { export const Pill = ({ clearValue, value }: pillPropTypes) => { return ( - + {value} = ( { + label, + id, placeholder, options, value, @@ -64,7 +68,8 @@ const SelectComponent: ForwardRefRenderFunction< useDropdownPortal = false, closeDropdownPortalOnScroll = false, zIndex = "300", - useSerialSearch = false + useSerialSearch = false, + ...props }, ref ) => { @@ -171,7 +176,7 @@ const SelectComponent: ForwardRefRenderFunction< const closeOnScroll = () => { if (closeDropdownPortalOnScroll) { setIsDropped(false); - onDropDownClose && onDropDownClose(); + onDropDownClose?.(); } }; @@ -180,7 +185,7 @@ const SelectComponent: ForwardRefRenderFunction< const getComputedStyle = // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore - document.body && document.body.currentStyle + document?.body?.currentStyle ? function (elem: any) { return elem.currentStyle; } @@ -246,7 +251,7 @@ const SelectComponent: ForwardRefRenderFunction< closeOnOutsideClick(); } else { isDropped && setIsDropped(false); - onDropDownClose && onDropDownClose(); + onDropDownClose?.(); } }, isDropped @@ -260,13 +265,11 @@ const SelectComponent: ForwardRefRenderFunction< !optionsListRef.current?.contains(event.target as Node) ) { isDropped && setIsDropped(false); - onDropDownClose && onDropDownClose(); - } - } else { - if (!inputRef.current?.contains(event.target as Node)) { - isDropped && setIsDropped(false); - onDropDownClose && onDropDownClose(); + onDropDownClose?.(); } + } else if (!inputRef.current?.contains(event.target as Node)) { + isDropped && setIsDropped(false); + onDropDownClose?.(); } }); }; @@ -305,7 +308,7 @@ const SelectComponent: ForwardRefRenderFunction< setSearchText(""); const multiValue = Array.isArray(selectValue) ? [...selectValue] : []; multiValue.push(option); - onChange && onChange(multiValue, event); + onChange?.(multiValue, event); setSelectValue(multiValue); } else { if ( @@ -314,14 +317,14 @@ const SelectComponent: ForwardRefRenderFunction< option.value === selectValue?.value ) { setSelectValue(null); - onChange && onChange(null, event); + onChange?.(null, event); } else { setSelectValue(option); - onChange && onChange(option, event); + onChange?.(option, event); } setIsDropped(false); setInternalOptions(options); - onDropDownClose && onDropDownClose(); + onDropDownClose?.(); } }; @@ -331,12 +334,10 @@ const SelectComponent: ForwardRefRenderFunction< ): OptionsType[] => { return options?.filter((option) => { if (useSerialSearch) { - return ( - option.label - .trim() - .toLowerCase() - .indexOf(value.trim().toLowerCase()) === 0 - ); + return option.label + .trim() + .toLowerCase() + .startsWith(value.trim().toLowerCase()); } return option.label .trim() @@ -362,29 +363,9 @@ const SelectComponent: ForwardRefRenderFunction< : setInternalOptions(filteredOptions); }; - const handleIconClick = (event: MouseEvent) => { - event.stopPropagation(); - if (isClearable) { - if (isMulti) { - onChange && onChange([], event); - setSelectValue([]); - } else { - onChange && onChange(null, event); - setSelectValue(null); - } - } else { - setIsDropped(!isDropped); - isDropped - ? onDropDownClose && onDropDownClose() - : onDropDownOpen && onDropDownOpen(); - } - }; - const changeDrop = () => { setIsDropped(!isDropped); - isDropped - ? onDropDownClose && onDropDownClose() - : onDropDownOpen && onDropDownOpen(); + isDropped ? onDropDownClose?.() : onDropDownOpen?.(); }; const clearPill = ( @@ -393,7 +374,7 @@ const SelectComponent: ForwardRefRenderFunction< ) => { let tempArr = Array.isArray(selectValue) ? [...selectValue] : []; tempArr = tempArr.filter((arr) => arr.value !== clearValue); - onChange && onChange(tempArr, event); + onChange?.(tempArr, event); setSelectValue(tempArr); setIsDropped(true); }; @@ -533,18 +514,30 @@ const SelectComponent: ForwardRefRenderFunction< }); const selectInputStyles = selectInputRecipe({ - error: error ? true : false, + error: !!error, disabled: isDisabled }); const selectInputElement = selectInputElementRecipe({ - error: error ? true : false + error: !!error }); const inputStyles = inputRecipe({ - error: error ? true : false, + error: !!error, isMulti }); + const clearMultiValues = (event: MouseEvent) => { + event.stopPropagation(); + onChange?.([], event); + setSelectValue([]); + }; + + const clearValues = (event: MouseEvent) => { + event.stopPropagation(); + onChange?.(null, event); + setSelectValue(null); + }; + const renderDropDown = () => { return ( { + if (isClearable) { + if (Array.isArray(selectValue) && selectValue.length) { + return ( + + ); + } else if (!Array.isArray(selectValue) && selectValue?.value) { + return ( + + ); + } + } + return DropIcon || ; + }; + const renderDropDownList = () => { return Array.isArray(internalOptions) && internalOptions?.length !== 0 ? ( internalOptions.map((option, ind) => { @@ -653,7 +666,17 @@ const SelectComponent: ForwardRefRenderFunction< [selectVars.maxDropDownHeight]: maxDropDownHeight }) }} + {...props} > + {label && ( + + )} {!isLoading ? ( - - {DropIcon - ? !isClearable && DropIcon - : !isClearable && } - {isClearable && } + + {renderIcon()} ) : ( loadingOptions?.loader || diff --git a/packages/react/src/components/Select/select.css.ts b/packages/react/src/components/Select/select.css.ts index 7e7a142..337f754 100644 --- a/packages/react/src/components/Select/select.css.ts +++ b/packages/react/src/components/Select/select.css.ts @@ -190,3 +190,9 @@ export const loadingContentContainer = style({ padding: "10px 16px", cursor: "default" }); + +export const labelStyles = style({ + fontWeight: 500, + fontSize: "inherit", + marginBottom: "5px" +}); diff --git a/packages/react/src/components/Select/select.types.ts b/packages/react/src/components/Select/select.types.ts index 9bfe455..ddee551 100644 --- a/packages/react/src/components/Select/select.types.ts +++ b/packages/react/src/components/Select/select.types.ts @@ -1,6 +1,7 @@ import { KeyboardEvent, MouseEvent, MutableRefObject, ReactNode } from "react"; -type divType = Omit; +type divType = Omit; export type SelectPropsType = divType & { + label?: ReactNode; placeholder?: string; options: OptionsType[]; value?: SelectValueType;