autoComplete.stories.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import AutoComplete from 'sentry/components/autoComplete';
  2. const items = [
  3. {
  4. name: 'Apple',
  5. },
  6. {
  7. name: 'Pineapple',
  8. },
  9. {
  10. name: 'Orange',
  11. },
  12. ];
  13. export default {
  14. title: 'Components/Forms/Auto Complete',
  15. parameters: {
  16. controls: {hideNoControlsWarning: true},
  17. },
  18. };
  19. export const Input = () => (
  20. <AutoComplete itemToString={item => item.name}>
  21. {({
  22. getRootProps,
  23. getInputProps,
  24. getMenuProps,
  25. getItemProps,
  26. inputValue,
  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 => item.name.toLowerCase().indexOf(inputValue.toLowerCase()) > -1
  49. )
  50. .map((item, index) => (
  51. <div
  52. key={item.name}
  53. {...getItemProps({
  54. item,
  55. index,
  56. style: {
  57. cursor: 'pointer',
  58. padding: '6px 12px',
  59. backgroundColor:
  60. index === highlightedIndex
  61. ? 'rgba(0, 0, 0, 0.02)'
  62. : undefined,
  63. },
  64. })}
  65. >
  66. {item.name}
  67. </div>
  68. ))}
  69. </div>
  70. </div>
  71. )}
  72. </div>
  73. );
  74. }}
  75. </AutoComplete>
  76. );
  77. Input.parameters = {
  78. docs: {
  79. description: {
  80. story: 'Autocomplete on an input',
  81. },
  82. },
  83. };