autoComplete.stories.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React from 'react';
  2. import {storiesOf} from '@storybook/react';
  3. import {withInfo} from '@storybook/addon-info';
  4. import AutoComplete from 'app/components/autoComplete';
  5. const items = [
  6. {
  7. name: 'Apple',
  8. },
  9. {
  10. name: 'Pineapple',
  11. },
  12. {
  13. name: 'Orange',
  14. },
  15. ];
  16. storiesOf('UI|AutoComplete', module).add(
  17. 'Input',
  18. withInfo('Autocomplete on an input')(() => (
  19. <AutoComplete itemToString={item => item.name}>
  20. {({
  21. getRootProps,
  22. getInputProps,
  23. getMenuProps,
  24. getItemProps,
  25. inputValue,
  26. selectedItem,
  27. highlightedIndex,
  28. isOpen,
  29. }) => {
  30. return (
  31. <div {...getRootProps({style: {position: 'relative'}})}>
  32. <input {...getInputProps({})} />
  33. {isOpen && (
  34. <div
  35. {...getMenuProps({
  36. style: {
  37. boxShadow:
  38. '0 1px 4px 1px rgba(47,40,55,0.08), 0 4px 16px 0 rgba(47,40,55,0.12)',
  39. position: 'absolute',
  40. backgroundColor: 'white',
  41. padding: '0',
  42. },
  43. })}
  44. >
  45. <div>
  46. {items
  47. .filter(
  48. item =>
  49. item.name.toLowerCase().indexOf(inputValue.toLowerCase()) > -1
  50. )
  51. .map((item, index) => (
  52. <div
  53. key={item.name}
  54. {...getItemProps({
  55. item,
  56. index,
  57. style: {
  58. cursor: 'pointer',
  59. padding: '6px 12px',
  60. backgroundColor:
  61. index === highlightedIndex
  62. ? 'rgba(0, 0, 0, 0.02)'
  63. : undefined,
  64. },
  65. })}
  66. >
  67. {item.name}
  68. </div>
  69. ))}
  70. </div>
  71. </div>
  72. )}
  73. </div>
  74. );
  75. }}
  76. </AutoComplete>
  77. ))
  78. );