Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Select & Autocomplete components #624

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/components/Autocomplete/Autocomplete.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import React, { useState } from "react";
bexsoft marked this conversation as resolved.
Show resolved Hide resolved
import { useState } from "react";
import { Meta, Story } from "@storybook/react";

import Autocomplete from "./Autocomplete";
Expand All @@ -27,6 +27,7 @@ import DownloadIcon from "../Icons/DownloadIcon";
import UploadIcon from "../Icons/UploadIcon";
import UsersIcon from "../Icons/UsersIcon";
import { SelectorType } from "../../global/global.types";
import TestIcon from "../../utils/TestIcon";

export default {
title: "MDS/Forms/Autocomplete",
Expand Down Expand Up @@ -205,3 +206,23 @@ NoDropArrow.args = {
],
displayDropArrow: false,
};

export const IndicatorInLabel = Template.bind({});
IndicatorInLabel.args = {
options: [
{
label: "Option with Indicator",
value: "value1",
indicator: <TestIcon style={{ fill: "#080" }} />,
},
{
label: "Option 2",
value: "value2",
},
{
label: "Option 3",
value: "value3",
},
{ label: "No Extra Value", value: "value4", icon: <UsersIcon /> },
],
};
45 changes: 40 additions & 5 deletions src/components/Autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import React, { FC, Fragment, useEffect, useState } from "react";
import React, { FC, Fragment, useEffect, useRef, useState } from "react";
import styled from "styled-components";
import get from "lodash/get";
import { AutocompleteProps } from "./Autocomplete.types";
Expand Down Expand Up @@ -47,6 +47,7 @@ const AutocompleteBase = styled.input(({ theme }) => {
borderRadius: 3,
outline: "none",
transitionDuration: "0.1s",
transitionProperty: "border",
backgroundColor: get(theme, "inputBox.backgroundColor", "#fff"),
userAutocomplete: "none",
"&:placeholder": {
Expand Down Expand Up @@ -74,6 +75,9 @@ const AutocompleteBase = styled.input(({ theme }) => {
borderColor: get(theme, "inputBox.disabledBorder", "#494A4D"),
},
},
"&.withIcon": {
paddingLeft: 38,
},
};
});

Expand All @@ -82,6 +86,7 @@ const InputContainer = styled.div<InputContainerProps>(
display: "flex",
flexGrow: 1,
width: "100%",
height: 38,
position: "relative",
"& .AutocompleteContainer": {
width: "100%",
Expand Down Expand Up @@ -112,6 +117,18 @@ const InputContainer = styled.div<InputContainerProps>(
"& .inputLabel": {
marginBottom: error ? 18 : 0,
},
"& .iconOption": {
display: "flex",
alignItems: "center",
justifyContent: "center",
position: "absolute",
marginLeft: 15,
height: 38,
"& svg": {
width: 16,
height: 16,
},
},
...sx,
}),
);
Expand All @@ -136,23 +153,29 @@ const Autocomplete: FC<AutocompleteProps> = ({
}) => {
const [isOpen, setIsOpen] = useState<boolean>(false);
const [searchBoxVal, setSearchBoxVal] = useState<string>("");
const [valueSelected, setValueSelected] = useState<number | null>(null);
const [filterVal, setFilterVal] = useState<string>("");
const [anchorEl, setAnchorEl] = React.useState<
(EventTarget & HTMLDivElement) | null
>(null);

useEffect(() => {
if (value !== "") {
const option = options.find((option) => option.value === value);
const index = options.findIndex((option) => option.value === value);

setSearchBoxVal(option?.label || "");
setValueSelected(index);
setSearchBoxVal(options[index]?.label || "");
}
}, []);

const filteredOptions = options.filter((item) =>
item.label.toLowerCase().includes(filterVal.toLowerCase()),
);

const optionWithIcon =
valueSelected !== null &&
(options[valueSelected]?.icon || options[valueSelected]?.indicator);

return (
<InputContainer
sx={sx}
Expand Down Expand Up @@ -195,6 +218,13 @@ const Autocomplete: FC<AutocompleteProps> = ({
}
}}
>
{optionWithIcon && (
<Box className={"iconOption"}>
{options[valueSelected]?.indicator
? options[valueSelected]?.indicator
: options[valueSelected].icon}
</Box>
)}
<AutocompleteBase
disabled={disabled}
id={id}
Expand All @@ -205,6 +235,7 @@ const Autocomplete: FC<AutocompleteProps> = ({
setFilterVal(e.target.value);
}}
placeholder={placeholder}
className={`${optionWithIcon ? "withIcon" : ""}`}
/>
{displayDropArrow && (
<Box className={"overlayArrow"}>
Expand All @@ -213,12 +244,16 @@ const Autocomplete: FC<AutocompleteProps> = ({
)}

<DropdownSelector
id={`${id}-options-Autocompleteor`}
id={`${id}-options-Autocomplete`}
options={filteredOptions}
selectedOption={value}
onSelect={(nValue, extraValue, label) => {
onSelect={(nValue, extraValue, label, id) => {
console.log("Selected", id);
setSearchBoxVal(label || "");
setFilterVal("");
if (id !== undefined) {
setValueSelected(id);
}
onChange(nValue, extraValue);
}}
hideTriggerAction={() => {
Expand Down
8 changes: 7 additions & 1 deletion src/components/DropdownSelector/DropdownSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ const DropdownSelector: FC<DropdownSelectorProps> = ({
const option = options[indexHover];

if (!option.disabled) {
onSelect(option.value, option.extraValue || null, option.label);
onSelect(
option.value,
option.extraValue || null,
option.label,
indexHover,
);
}

hideTriggerAction();
Expand Down Expand Up @@ -229,6 +234,7 @@ const DropdownSelector: FC<DropdownSelectorProps> = ({
>
{option.icon}
{option.label}
{option.indicator}
</li>
);
})}
Expand Down
7 changes: 6 additions & 1 deletion src/components/DropdownSelector/DropdownSelector.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export interface DropdownSelectorProps {
id: string;
options: SelectorType[];
selectedOption?: string;
onSelect: (value: string, extraValue?: any, label?: string) => void;
onSelect: (
value: string,
extraValue?: any,
label?: string,
selectedIndex?: number,
) => void;
hideTriggerAction: () => void;
open: boolean;
anchorEl?: (EventTarget & HTMLElement) | null;
Expand Down
30 changes: 29 additions & 1 deletion src/components/Select/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import React, { useState } from "react";
import { Fragment, useState } from "react";
import { Meta, Story } from "@storybook/react";

import Select from "./Select";
Expand All @@ -27,6 +27,7 @@ import DownloadIcon from "../Icons/DownloadIcon";
import UploadIcon from "../Icons/UploadIcon";
import UsersIcon from "../Icons/UsersIcon";
import { SelectorType } from "../../global/global.types";
import TestIcon from "../../utils/TestIcon";

export default {
title: "MDS/Forms/Select",
Expand Down Expand Up @@ -186,3 +187,30 @@ WithPlaceholder.args = {
{ label: "No Extra Value", value: "value4", icon: <UsersIcon /> },
],
};

export const IndicatorInLabel = Template.bind({});
IndicatorInLabel.args = {
placeholder: "This Select has placeholder",
options: [
{
label: "Option with indicator",
value: "value1",
icon: <DownloadIcon />,
extraValue: { anotherVar: "test1" },
indicator: <TestIcon style={{ fill: "#080" }} />,
},
{
label: "Option 2",
value: "value2",
icon: <UploadIcon />,
extraValue: { anotherVar: "test2" },
},
{
label: "Option 3",
value: "value3",
icon: <UsersIcon />,
extraValue: { anotherVar: "test3" },
},
{ label: "No Extra Value", value: "value4", icon: <UsersIcon /> },
],
};
9 changes: 8 additions & 1 deletion src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const SelectBase = styled.div(({ theme }) => {
transitionDuration: "0.1s",
backgroundColor: get(theme, "inputBox.backgroundColor", "#fff"),
userSelect: "none",
gap: 8,
"&:placeholder": {
color: "#858585",
opacity: 1,
Expand All @@ -74,6 +75,10 @@ const SelectBase = styled.div(({ theme }) => {
borderColor: get(theme, "inputBox.disabledBorder", "#494A4D"),
},
},
"& svg": {
width: 16,
height: 16,
},
};
});

Expand Down Expand Up @@ -181,15 +186,17 @@ const Select: FC<SelectProps> = ({
>
<SelectBase className={disabled ? "disabled" : ""}>
<Fragment>
{fixedLabel !== "" ? (
{fixedLabel && fixedLabel !== "" ? (
fixedLabel
) : (
<Fragment>
{selectedLabel?.icon}
{selectedLabel?.label || (
<i style={{ opacity: 0.6 }}>
{placeholder !== "" ? placeholder : ""}
</i>
)}
{selectedLabel?.indicator}
</Fragment>
)}
</Fragment>
Expand Down
1 change: 1 addition & 0 deletions src/global/global.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ export interface SelectorType {
label: string;
value: string;
icon?: React.ReactNode;
indicator?: React.ReactNode;
extraValue?: any;
disabled?: boolean;
}
Expand Down