|
| 1 | +import type { Meta, StoryObj } from '@storybook/react'; |
| 2 | +import React, { forwardRef, useState } from 'react'; |
| 3 | +import AddressSearch, { type Address, type Item } from '..'; |
| 4 | + |
| 5 | +const meta: Meta = { |
| 6 | + title: 'Custom Components', |
| 7 | + component: AddressSearch, |
| 8 | +}; |
| 9 | + |
| 10 | +export default meta; |
| 11 | + |
| 12 | +type Story = StoryObj<typeof AddressSearch>; |
| 13 | + |
| 14 | +const CustomInput = forwardRef<HTMLInputElement, React.ComponentProps<'input'>>( |
| 15 | + ({ value, onChange, ...rest }, ref) => { |
| 16 | + return ( |
| 17 | + <input |
| 18 | + ref={ref} |
| 19 | + value={value || ''} |
| 20 | + onChange={onChange} |
| 21 | + style={{ |
| 22 | + padding: '12px 16px', |
| 23 | + border: '2px solid #3b82f6', |
| 24 | + borderRadius: '8px', |
| 25 | + fontSize: '16px', |
| 26 | + width: '100%', |
| 27 | + outline: 'none', |
| 28 | + }} |
| 29 | + placeholder="Enter your address" |
| 30 | + {...rest} |
| 31 | + /> |
| 32 | + ); |
| 33 | + } |
| 34 | +); |
| 35 | + |
| 36 | +const CustomList = forwardRef<HTMLUListElement, React.ComponentProps<'ul'>>( |
| 37 | + (props, ref) => { |
| 38 | + return ( |
| 39 | + <ul |
| 40 | + ref={ref} |
| 41 | + {...props} |
| 42 | + style={{ |
| 43 | + ...props.style, |
| 44 | + backgroundColor: '#f8fafc', |
| 45 | + border: '2px solid #3b82f6', |
| 46 | + borderRadius: '8px', |
| 47 | + listStyle: 'none', |
| 48 | + margin: 0, |
| 49 | + padding: 0, |
| 50 | + }} |
| 51 | + /> |
| 52 | + ); |
| 53 | + } |
| 54 | +); |
| 55 | + |
| 56 | +const CustomListItem = forwardRef< |
| 57 | + HTMLLIElement, |
| 58 | + React.ComponentProps<'li'> & { suggestion: Item; value?: string } |
| 59 | +>((props, ref) => { |
| 60 | + const { |
| 61 | + suggestion, |
| 62 | + onClick, |
| 63 | + onKeyDown, |
| 64 | + onMouseEnter, |
| 65 | + onMouseLeave, |
| 66 | + style, |
| 67 | + ...rest |
| 68 | + } = props; |
| 69 | + return ( |
| 70 | + <li |
| 71 | + ref={ref} |
| 72 | + {...rest} |
| 73 | + onClick={onClick} |
| 74 | + onKeyDown={(e) => { |
| 75 | + if (e.key === 'Enter' || e.key === ' ') { |
| 76 | + e.preventDefault(); |
| 77 | + onClick?.(e as unknown as React.MouseEvent<HTMLLIElement>); |
| 78 | + } |
| 79 | + onKeyDown?.(e); |
| 80 | + }} |
| 81 | + style={{ |
| 82 | + padding: '12px 16px', |
| 83 | + borderBottom: '1px solid #e2e8f0', |
| 84 | + cursor: 'pointer', |
| 85 | + backgroundColor: 'white', |
| 86 | + ...style, |
| 87 | + }} |
| 88 | + onMouseEnter={(e) => { |
| 89 | + e.currentTarget.style.backgroundColor = '#eff6ff'; |
| 90 | + onMouseEnter?.(e); |
| 91 | + }} |
| 92 | + onMouseLeave={(e) => { |
| 93 | + e.currentTarget.style.backgroundColor = 'white'; |
| 94 | + onMouseLeave?.(e); |
| 95 | + }} |
| 96 | + > |
| 97 | + {suggestion.Text} {suggestion.Description} |
| 98 | + </li> |
| 99 | + ); |
| 100 | +}); |
| 101 | + |
| 102 | +const Template = (props: React.ComponentProps<typeof AddressSearch>) => { |
| 103 | + const [result, setResult] = useState<Address>(); |
| 104 | + |
| 105 | + return ( |
| 106 | + <> |
| 107 | + <AddressSearch {...props} onSelect={setResult} /> |
| 108 | + <pre>{JSON.stringify(result, null, 2)}</pre> |
| 109 | + </> |
| 110 | + ); |
| 111 | +}; |
| 112 | + |
| 113 | +export const CustomComponents: Story = { |
| 114 | + args: { |
| 115 | + // @ts-expect-error: env does exist |
| 116 | + // We need to prefix with STORYBOOK, otherwise Storybook will ignore the variable |
| 117 | + apiKey: import.meta.env.STORYBOOK_API_KEY ?? '', |
| 118 | + countries: ['US', 'GB'], |
| 119 | + locale: 'en_US', |
| 120 | + inline: true, |
| 121 | + components: { |
| 122 | + Input: CustomInput, |
| 123 | + List: CustomList, |
| 124 | + ListItem: CustomListItem, |
| 125 | + }, |
| 126 | + }, |
| 127 | + render: Template, |
| 128 | +}; |
0 commit comments