Skip to content

Commit

Permalink
Merge pull request #507 from cornell-dti/dka34/select
Browse files Browse the repository at this point in the history
Select Component - dropdown with fuzzy search and multi-select capability
  • Loading branch information
Atikpui007 authored Oct 5, 2024
2 parents 6b2861f + d097aa5 commit 5eb347e
Show file tree
Hide file tree
Showing 10 changed files with 602 additions and 189 deletions.
217 changes: 217 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"react-router-dom": "^6.26.2",
"react-router-hash-link": "^2.4.3",
"react-scripts": "^5.0.1",
"react-select": "^5.8.1",
"reactjs-popup": "^2.0.6"
},
"scripts": {
Expand Down Expand Up @@ -71,4 +72,4 @@
"eslint-plugin-react-hooks": "^4.6.0",
"prettier": "2.8.8"
}
}
}
66 changes: 66 additions & 0 deletions frontend/src/components/FormElements/FormElements.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import React, { SelectHTMLAttributes } from 'react';

Check warning on line 1 in frontend/src/components/FormElements/FormElements.tsx

View workflow job for this annotation

GitHub Actions / check

'SelectHTMLAttributes' is defined but never used
import cn from 'classnames';
import styles from './formelements.module.css';
import Select, { ActionMeta, Props as SelectProps } from 'react-select';
import {
Control,
RegisterOptions,
useController,
Path,
FieldValues,
} from 'react-hook-form';

type LabelType = React.DetailedHTMLProps<
React.LabelHTMLAttributes<HTMLLabelElement>,
Expand Down Expand Up @@ -73,3 +81,61 @@ export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
);
}
);

type Option = {
id: string;
name: string;
};

type SelectOption = {
value: string;
label: string;
};

type SelectComponentProps<TFieldValues extends FieldValues> = SelectProps & {
control: Control<TFieldValues>;
name: Path<TFieldValues>;
datalist: Option[];
className?: string;
rules?: RegisterOptions<TFieldValues>;
};

export const SelectComponent = <TFieldValues extends FieldValues>({
control,
name,
datalist,
className,
rules,
...rest
}: SelectComponentProps<TFieldValues>) => {
const {
field: { onChange, value, ref, ...inputProps },
} = useController<TFieldValues>({
name,
control,
rules,
});

const transformedOptions = datalist.map((data) => ({
value: data.id,
label: data.name,
}));

const selectedOption = transformedOptions.find(
(option) => option.value === value
);

return (
<Select
{...inputProps}
options={transformedOptions}
className={cn(styles.customSelect, className)}
onChange={(newValue: unknown) => {
const option = newValue as SelectOption | null;
onChange(option?.value);
}}
value={selectedOption}
{...rest}
/>
);
};
Loading

0 comments on commit 5eb347e

Please sign in to comment.