|
| 1 | +import type { Meta, StoryObj } from '@storybook/react'; |
| 2 | +import { fn } from '@storybook/test'; |
| 3 | +import { http, HttpResponse } from 'msw'; |
| 4 | + |
| 5 | +import { AutocompleteField } from '@concepta/react-material-ui'; |
| 6 | + |
| 7 | +const meta = { |
| 8 | + component: AutocompleteField, |
| 9 | + tags: ['autodocs'], |
| 10 | + argTypes: {}, |
| 11 | + args: { |
| 12 | + label: 'Autocomplete Field', |
| 13 | + }, |
| 14 | + parameters: { |
| 15 | + msw: { |
| 16 | + handlers: [ |
| 17 | + http.get('/food', ({ request }) => { |
| 18 | + const url = new URL(request.url); |
| 19 | + const sort = url.searchParams.get('sort'); |
| 20 | + const sortField = sort?.split(',')[0]; |
| 21 | + const sortOrder = sort?.split(',')[1]; |
| 22 | + const filters = url.searchParams.get('filters[]'); |
| 23 | + const type = filters?.split('||$eq||')[1]; |
| 24 | + |
| 25 | + const foodArray = [ |
| 26 | + { id: 1, name: 'Carrot', type: 'fruit' }, |
| 27 | + { id: 2, name: 'Cesar Salad', type: 'healthy' }, |
| 28 | + { id: 3, name: 'Apple', type: 'fruit' }, |
| 29 | + { id: 4, name: 'Pizza', type: 'junk' }, |
| 30 | + { id: 5, name: 'Banana', type: 'fruit' }, |
| 31 | + { id: 6, name: 'Hamburguer', type: 'junk' }, |
| 32 | + { id: 7, name: 'Sardines', type: 'healthy' }, |
| 33 | + ]; |
| 34 | + |
| 35 | + return HttpResponse.json( |
| 36 | + foodArray |
| 37 | + .sort((a, b) => { |
| 38 | + if (!sortField || !a[sortField] || !b[sortField]) return 0; |
| 39 | + |
| 40 | + if (sortField === 'id') { |
| 41 | + return sortOrder === 'ASC' ? a.id - b.id : b.id - a.id; |
| 42 | + } |
| 43 | + |
| 44 | + if (sortOrder === 'ASC') { |
| 45 | + return a[sortField]?.localeCompare(b[sortField]); |
| 46 | + } |
| 47 | + |
| 48 | + if (sortOrder === 'DESC') { |
| 49 | + return b[sortField]?.localeCompare(a[sortField]); |
| 50 | + } |
| 51 | + |
| 52 | + return 0; |
| 53 | + }) |
| 54 | + .filter((food) => { |
| 55 | + return type ? food.type === type : true; |
| 56 | + }), |
| 57 | + ); |
| 58 | + }), |
| 59 | + ], |
| 60 | + }, |
| 61 | + }, |
| 62 | +} satisfies Meta<typeof AutocompleteField>; |
| 63 | + |
| 64 | +export default meta; |
| 65 | + |
| 66 | +type Story = StoryObj<typeof meta>; |
| 67 | + |
| 68 | +export const Default: Story = { |
| 69 | + args: { |
| 70 | + options: [ |
| 71 | + { value: '1', label: 'Option 1' }, |
| 72 | + { value: '2', label: 'Option 2' }, |
| 73 | + { value: '3', label: 'Option 3' }, |
| 74 | + ], |
| 75 | + label: 'Autocomplete Field', |
| 76 | + isLoading: false, |
| 77 | + onChange: fn(), |
| 78 | + }, |
| 79 | +}; |
| 80 | + |
| 81 | +export const WithOptions: Story = { |
| 82 | + args: { |
| 83 | + options: [ |
| 84 | + { value: '1', label: 'Option 1' }, |
| 85 | + { value: '2', label: 'Option 2' }, |
| 86 | + { value: '3', label: 'Option 3' }, |
| 87 | + ], |
| 88 | + }, |
| 89 | +}; |
| 90 | + |
| 91 | +export const Loading: Story = { |
| 92 | + args: { |
| 93 | + isLoading: true, |
| 94 | + }, |
| 95 | +}; |
| 96 | + |
| 97 | +/** |
| 98 | + * If you provide a "resource" prop, the component will fetch the data from the provided path. |
| 99 | + * |
| 100 | + * The "resourceLabel" and "resourceValue" props are used to define the label and value of the options. |
| 101 | + */ |
| 102 | +export const ResourceData: Story = { |
| 103 | + args: { |
| 104 | + label: 'Food', |
| 105 | + resource: 'food', |
| 106 | + resourceLabel: 'name', |
| 107 | + resourceValue: 'id', |
| 108 | + }, |
| 109 | +}; |
| 110 | + |
| 111 | +/** |
| 112 | + * You can also provide filters and sort options to the resource. |
| 113 | + */ |
| 114 | +export const ResourceDataWithFilters: Story = { |
| 115 | + args: { |
| 116 | + label: 'Junk food', |
| 117 | + resource: 'food', |
| 118 | + resourceLabel: 'name', |
| 119 | + resourceValue: 'id', |
| 120 | + filters: { 'type||$eq||': 'junk' }, |
| 121 | + }, |
| 122 | +}; |
| 123 | + |
| 124 | +export const ResourceDataWithSort: Story = { |
| 125 | + args: { |
| 126 | + label: 'Sorted by name', |
| 127 | + resource: 'food', |
| 128 | + resourceLabel: 'name', |
| 129 | + resourceValue: 'id', |
| 130 | + sort: 'name,ASC', |
| 131 | + }, |
| 132 | +}; |
0 commit comments