All files / src/stories Autocomplete.stories.tsx

0% Statements 0/42
100% Branches 0/0
0% Functions 0/14
0% Lines 0/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                                                                                                                                                                                                                                                                                                   
import { action } from '@storybook/addon-actions';
import { Story } from '@storybook/react';
import { Box, useTheme } from '@mui/material';
import React, { useCallback } from 'react';
import Autocomplete, { ValueType, Props as AutocompleteProps } from '../components/Autocomplete/Autocomplete';
 
export default {
  title: 'Autocomplete',
  component: Autocomplete,
};
 
const Template: Story<AutocompleteProps> = (args) => {
  const theme = useTheme();
  const [selected, setSelected] = React.useState<ValueType>();
  const handleChange = (value: ValueType) => {
    action('onChange')(value);
    setSelected(value);
  };
 
  return (
    <Box sx={{ marginTop: '30px' }}>
      <Autocomplete
        {...args}
        selected={selected}
        onChange={handleChange}
        sx={{
          backgroundColor: theme.palette.TwClrBaseWhite,
          width: '300px',
        }}
      />
    </Box>
  );
};
 
const AsyncTemplate: Story<AutocompleteProps> = (args) => {
  const theme = useTheme();
 
  const [open, setOpen] = React.useState<boolean>(false);
  const [loading, setLoading] = React.useState<boolean>(false);
  const [selected, setSelected] = React.useState<ValueType>();
  const [options, setOptions] = React.useState<ValueType[]>([]);
 
  const sleep = (duration: number): Promise<void> => {
    return new Promise<void>((resolve) => {
      setTimeout(() => {
        resolve();
      }, duration);
    });
  };
 
  const load = useCallback(async () => {
    setLoading(true);
    await sleep(1e3);
    setLoading(false);
    setOptions([...args.options]);
  }, [setLoading, setOptions]);
 
  const handleOpen = useCallback(async () => {
    setOpen(true);
    await load();
  }, [setOpen, load]);
 
  const handleClose = () => {
    setOpen(false);
    setOptions([]);
  };
 
  const handleChange = (value: ValueType) => {
    action('onChange')(value);
    setSelected(value);
  };
 
  return (
    <Box sx={{ marginTop: '30px' }}>
      <Autocomplete
        {...args}
        open={open}
        onOpen={() => void handleOpen()}
        onClose={handleClose}
        selected={selected}
        loading={loading}
        options={options}
        onChange={handleChange}
        sx={{
          backgroundColor: theme.palette.TwClrBaseWhite,
          width: '300px',
        }}
      />
    </Box>
  );
};
 
export const Default = Template.bind({});
 
export const Complex = Template.bind({});
 
export const AsyncDefault = AsyncTemplate.bind({});
 
 
Default.args = {
  id: '1',
  label: 'Test',
  options: ['Test 1', 'Test 2', 'Hello'],
  onChange: () => true,
  selected: undefined,
  freeSolo: true,
  errorText: '',
};
 
Complex.args = {
  id: '2',
  label: 'Complex Objects',
  placeholder: 'Pick a value',
  options: [
    {
      label: 'hello',
      value: 1,
    },
    {
      label: 'world',
      value: 2,
    },
    {
      label: 'yoyo',
      value: 3,
    },
    {
      label: 'ma',
      value: 4,
    },
  ],
  onChange: () => true,
  selected: undefined,
  hideClearIcon: true,
  errorText: '',
  isEqual: (option: any, value: any) => option.value === value.value,
  tooltipTitle: 'Hello world!',
};
 
AsyncDefault.args = {
  id: '3',
  label: 'Test',
  options: ['Test 1', 'Test 2', 'Hello'],
  freeSolo: true,
  errorText: '',
};