autoComplete.stories.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React from 'react';
  2. import AutoComplete from 'app/components/autoComplete';
  3. const items = [
  4. {
  5. name: 'Apple',
  6. },
  7. {
  8. name: 'Pineapple',
  9. },
  10. {
  11. name: 'Orange',
  12. },
  13. ];
  14. export default {
  15. title: 'Forms/AutoComplete',
  16. parameters: {
  17. controls: {hideNoControlsWarning: true},
  18. },
  19. };
  20. export const Input = () => (
  21. <AutoComplete itemToString={item => item.name}>
  22. {({
  23. getRootProps,
  24. getInputProps,
  25. getMenuProps,
  26. getItemProps,
  27. inputValue,
  28. highlightedIndex,
  29. isOpen,
  30. }) => {
  31. return (
  32. <div {...getRootProps({style: {position: 'relative'}})}>
  33. <input {...getInputProps({})} />
  34. {isOpen && (
  35. <div
  36. {...getMenuProps({
  37. style: {
  38. boxShadow:
  39. '0 1px 4px 1px rgba(47,40,55,0.08), 0 4px 16px 0 rgba(47,40,55,0.12)',
  40. position: 'absolute',
  41. backgroundColor: 'white',
  42. padding: '0',
  43. },
  44. })}
  45. >
  46. <div>
  47. {items
  48. .filter(
  49. item => 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. Input.parameters = {
  79. docs: {
  80. description: {
  81. story: 'Autocomplete on an input',
  82. },
  83. },
  84. };