All files / src/stories Select.stories.tsx

0% Statements 0/40
0% Branches 0/6
0% Functions 0/12
0% Lines 0/37

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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194                                                                                                                                                                                                                                                                                                                                                                                                   
import { Story } from '@storybook/react';
import React from 'react';
import { Box, Typography } from '@mui/material';
import Select, { SelectProps } from '../components/Select/Select';
import SelectT, { SelectTProps } from '../components/Select/SelectT';
 
export default {
  title: 'Select',
  component: Select,
};
 
const Template: Story<SelectProps> = (args) => {
  // tslint:disable-next-line:no-unused-vars
  const [value, setValue] = React.useState('');
  const handleChange = (str: string) => {
    setValue(str);
  };
 
  return <Select {...args} onChange={handleChange} selectedValue={value} />;
};
 
const EditableTemplate: Story<SelectProps> = (args) => {
  // tslint:disable-next-line:no-unused-vars
  const [value, setValue] = React.useState('');
  const [options, setOptions] = React.useState(args.options);
  const handleChange = (str: string) => {
    setValue(str);
    setOptions((args.options ?? []).filter((opt) => !!opt.match(str)));
  };
 
  // the Box hierarchy below is mostly to enable scrolling and test fixedMenu with scrolled content
  return (
    <Box border='1px solid black' height='400px' width='500px' marginTop='50px' overflow='auto'>
      <Box height='1000px'>
        <Box width='200px' margin='0 auto'>
          <Select {...args} onChange={handleChange} selectedValue={value} options={options} />
          <input />
        </Box>
      </Box>
    </Box>
  );
};
 
export const Default = Template.bind({});
 
Default.args = {
  disabled: false,
  errorText: '',
  fixedMenu: true,
  helperText: 'Help text.',
  label: 'Field Label',
  options: ['test 1', 'test 2', 'test 3'],
  placeholder: 'Placeholder...',
  readonly: false,
  selectedValue: 'test 2',
  warningText: '',
};
 
export const Editable = EditableTemplate.bind({});
 
const editableValues = () => {
  const values = [];
  for (let i = 0; i < 300; i++) {
    values.push(`test ${i + 1}`);
  }
 
  return values;
};
 
Editable.args = {
  disabled: false,
  editable: true,
  errorText: '',
  fixedMenu: true,
  helperText: 'Help text.',
  label: 'Field Label',
  options: editableValues(),
  placeholder: 'Placeholder...',
  readonly: false,
  selectedValue: 'test 2',
  warningText: '',
};
 
type Value = {
  name?: string;
  age?: number;
};
 
const TemplateSelectT: Story<SelectTProps<Value>> = (args) => {
  // tslint:disable-next-line:no-unused-vars
  const [value, setValue] = React.useState<Value>({});
  const handleChange = (val: Value) => {
    setValue(val);
  };
 
  const isEqual = (A: Value, B: Value) => {
    return A.name === B.name && A.age === B.age;
  };
 
  const renderOption = (option: Value) => {
    return (
      <div>
        <Typography component='p' sx={{ fontWeight: 'bold', fontSize: '12px', display: 'inline-block' }}>
          {option.name}
        </Typography>
        &nbsp;
        <Typography component='p' sx={{ fontStyle: 'italic', fontSize: '10px', display: 'inline-block' }}>
          ({option.age})
        </Typography>
      </div>
    );
  };
 
  const toT = (input: string) => ({ name: input });
 
  const displayLabel = (option: any) => {
    return (option as Value)?.name || '';
  };
 
  return (
    <SelectT
      {...args}
      onChange={handleChange}
      isEqual={isEqual}
      renderOption={renderOption}
      toT={toT}
      displayLabel={displayLabel}
      selectedValue={value}
    />
  );
};
 
export const ComplexSelect = TemplateSelectT.bind({});
 
ComplexSelect.args = {
  label: 'Field Label',
  disabled: false,
  helperText: 'Help text.',
  placeholder: 'Placeholder...',
  errorText: '',
  warningText: '',
  readonly: false,
  options: [
    {
      name: 'Dora',
      age: 5,
    },
    {
      name: 'Eeyore',
      age: 8,
    },
    {
      name: 'Yoda',
      age: 500,
    },
  ],
  selectedValue: {
    name: 'Eeyore',
    age: 8,
  },
  tooltipTitle: 'Hello world!',
};
 
export const Styled = Template.bind({});
 
Styled.args = {
  disabled: false,
  errorText: '',
  fixedMenu: true,
  helperText: '',
  label: '',
  options: ['test 1', 'test 2', 'test 3'],
  placeholder: 'Placeholder...',
  readonly: false,
  selectedValue: 'test 2',
  warningText: '',
  selectStyles: {
    arrow: {
      height: '32px',
    },
    input: {
      fontSize: '24px',
      fontWeight: '600',
      lineHeight: '32px',
    },
    inputContainer: {
      border: 0,
    },
    optionsContainer: {
      border: 0,
    },
  },
};