Bernardo_Schmitz

Untitled

Nov 7th, 2024
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import { useId } from "react";
  2. import SelectReact, { SingleValue, StylesConfig } from "react-select";
  3.  
  4. interface Option {
  5. value: any;
  6. label: string;
  7. }
  8.  
  9. interface Props<T> {
  10. options: Map<T, string>;
  11. valor: T | null;
  12. onChange: (valor: T) => void;
  13. disabled?: boolean;
  14. autoFocus?: boolean;
  15. isAdmin?: boolean;
  16. }
  17.  
  18. export function SelectOne<T>(p: Props<T>) {
  19. function onChange(option: SingleValue<Option>) {
  20. if (option) {
  21. p.onChange(option.value);
  22. }
  23. }
  24. const options: Option[] = [];
  25.  
  26. p.options.forEach((v, k) => {
  27. options.push({ value: k, label: v });
  28. });
  29.  
  30. const value = options.find((e) => e.value === p.valor);
  31. const styles: StylesConfig<Option> = {
  32. control: (base, state) => ({
  33. ...base,
  34. transition: "all 0.2s",
  35. border: state.isFocused ? "1px solid #80bdff" : "1px solid #ced4da",
  36. boxShadow: state.isFocused ? "0 0 0 0.25rem rgba(0,123,255,.25)" : "",
  37. "&:hover": {
  38. border: "default",
  39. },
  40. }),
  41. };
  42.  
  43. return (
  44. <SelectReact<Option>
  45. styles={styles}
  46. instanceId={useId()}
  47. className="w-100"
  48. value={value}
  49. onChange={onChange}
  50. placeholder="Selecione..."
  51. menuPlacement="auto"
  52. closeMenuOnSelect
  53. noOptionsMessage={({ inputValue }) => (
  54. <span>Nenhuma opção encontrada para o termo: {inputValue}</span>
  55. )}
  56. isSearchable
  57. options={options}
  58. isDisabled={p.disabled}
  59. autoFocus={p.autoFocus}
  60. />
  61. );
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment