All files / blong-browser/src/components/Editor/stories CascadedTables.stories.tsx

0% Statements 0/165
0% Branches 0/1
0% Functions 0/1
0% Lines 0/165

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166                                                                                                                                                                                                                                                                                                                                           
/**
 * Three cascaded table widgets: selecting a person filters documents by personId,
 * selecting a document filters attachments by documentId.
 * Uses `widget.parent` + `widget.master` for cascaded filtering and `autoSelect: true`
 * to automatically select the first row when the parent selection changes.
 */
import type {Meta} from '@storybook/react-vite';
import type {StoryFn} from '../Editor.stories.js';
import {Editor} from '../Editor.js';

const meta: Meta<typeof Editor> = {title: 'Editor/CascadedTables', component: Editor};
export default meta;

const cascadedValue = {
    person: [
        {personId: 1, fullName: 'John Doe', birthDate: '2000-01-08', nationalId: '777777777'},
        {personId: 2, fullName: 'Jane Doe', birthDate: '2002-03-18', nationalId: '888888888'},
    ],
    document: [
        {
            documentId: 1,
            personId: 1,
            documentType: 'Passport',
            issueDate: '2020-01-01',
            expiryDate: '2025-01-01',
        },
        {
            documentId: 2,
            personId: 1,
            documentType: 'Driving License',
            issueDate: '2020-01-02',
            expiryDate: '2025-01-02',
        },
        {
            documentId: 3,
            personId: 1,
            documentType: 'Marriage Certificate',
            issueDate: '2020-01-03',
            expiryDate: '2025-01-03',
        },
        {
            documentId: 4,
            personId: 2,
            documentType: 'Passport',
            issueDate: '2020-01-04',
            expiryDate: '2025-01-04',
        },
        {
            documentId: 5,
            personId: 2,
            documentType: 'Driving License',
            issueDate: '2020-01-05',
            expiryDate: '2025-01-05',
        },
    ],
    attachment: [
        {
            attachmentId: 1,
            documentId: 1,
            pageNumber: 1,
            contentType: 'image/jpeg',
            sizeBytes: 12345,
        },
        {
            attachmentId: 2,
            documentId: 1,
            pageNumber: 2,
            contentType: 'image/jpeg',
            sizeBytes: 23456,
        },
        {attachmentId: 3, documentId: 2, pageNumber: 1, contentType: 'image/png', sizeBytes: 33333},
        {attachmentId: 4, documentId: 2, pageNumber: 2, contentType: 'image/png', sizeBytes: 22222},
    ],
};

export const CascadedTables: StoryFn = () => (
    <Editor
        schema={{
            properties: {
                person: {
                    title: '',
                    type: 'array',
                    widget: {
                        type: 'table',
                        selectionMode: 'single',
                        columns: ['fullName', 'nationalId', 'birthDate'],
                    },
                    items: {
                        properties: {
                            personId: {title: 'ID', readOnly: true},
                            fullName: {title: 'Full Name'},
                            nationalId: {title: 'National ID'},
                            birthDate: {title: 'Birth Date'},
                        },
                    },
                },
                document: {
                    title: '',
                    type: 'array',
                    widget: {
                        type: 'table',
                        selectionMode: 'single',
                        columns: ['documentType', 'issueDate', 'expiryDate'],
                        parent: 'person',
                        master: {personId: 'personId'},
                        autoSelect: true,
                    },
                    items: {
                        properties: {
                            documentId: {title: 'ID', readOnly: true},
                            personId: {title: 'Person ID', readOnly: true},
                            documentType: {title: 'Document Type'},
                            issueDate: {title: 'Issue Date'},
                            expiryDate: {title: 'Expiry Date'},
                        },
                    },
                },
                attachment: {
                    title: '',
                    type: 'array',
                    widget: {
                        type: 'table',
                        selectionMode: 'single',
                        columns: ['pageNumber', 'contentType', 'sizeBytes'],
                        parent: 'document',
                        master: {documentId: 'documentId'},
                        autoSelect: true,
                    },
                    items: {
                        properties: {
                            attachmentId: {title: 'ID', readOnly: true},
                            documentId: {title: 'Document ID', readOnly: true},
                            pageNumber: {title: 'Page'},
                            contentType: {title: 'Content Type'},
                            sizeBytes: {title: 'Size (bytes)'},
                        },
                    },
                },
            },
        }}
        cards={{
            person: {label: 'Person', className: 'col-12 md:col-4', widgets: ['person']},
            document: {label: 'Document', className: 'col-12 md:col-4', widgets: ['document']},
            attachment: {
                label: 'Attachment',
                className: 'col-12 md:col-4',
                widgets: ['attachment'],
            },
        }}
        value={cascadedValue}
        editable
        editMode
        layout="edit"
        layouts={{edit: ['person', 'document', 'attachment']}}
    />
);

CascadedTables.play = async ({canvas, userEvent}) => {
    const johnRow = await canvas.findByText('John Doe');
    await userEvent.click(johnRow);
    await new Promise(resolve => setTimeout(resolve, 200));
    const drivingRow = canvas.getByText('Driving License');
    await userEvent.click(drivingRow);
    await new Promise(resolve => setTimeout(resolve, 200));
};