|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import React, { useState } from 'react'; |
| 10 | +import type { Meta, StoryObj } from '@storybook/react'; |
| 11 | +import { ConfigPanelRenderer } from './custom/config-panel-renderer'; |
| 12 | +import { ConfigFieldRenderer } from './custom/config-field-renderer'; |
| 13 | +import { useConfigDraft } from './hooks/use-config-draft'; |
| 14 | +import type { ConfigPanelSchema, ConfigField } from './types/config-panel'; |
| 15 | + |
| 16 | +// ─── ConfigPanelRenderer Stories ───────────────────────────────────────────── |
| 17 | + |
| 18 | +const panelMeta = { |
| 19 | + title: 'Config/ConfigPanelRenderer', |
| 20 | + component: ConfigPanelRenderer, |
| 21 | + parameters: { |
| 22 | + layout: 'padded', |
| 23 | + }, |
| 24 | + tags: ['autodocs'], |
| 25 | +} satisfies Meta<typeof ConfigPanelRenderer>; |
| 26 | + |
| 27 | +export default panelMeta; |
| 28 | +type Story = StoryObj<typeof panelMeta>; |
| 29 | + |
| 30 | +// Basic schema example |
| 31 | +const basicSchema: ConfigPanelSchema = { |
| 32 | + breadcrumb: ['Settings', 'General'], |
| 33 | + sections: [ |
| 34 | + { |
| 35 | + key: 'basic', |
| 36 | + title: 'Basic', |
| 37 | + fields: [ |
| 38 | + { key: 'title', label: 'Title', type: 'input', placeholder: 'Enter title' }, |
| 39 | + { key: 'enabled', label: 'Enabled', type: 'switch', defaultValue: true }, |
| 40 | + { |
| 41 | + key: 'mode', |
| 42 | + label: 'Mode', |
| 43 | + type: 'select', |
| 44 | + options: [ |
| 45 | + { value: 'auto', label: 'Auto' }, |
| 46 | + { value: 'manual', label: 'Manual' }, |
| 47 | + { value: 'scheduled', label: 'Scheduled' }, |
| 48 | + ], |
| 49 | + defaultValue: 'auto', |
| 50 | + }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + { |
| 54 | + key: 'appearance', |
| 55 | + title: 'Appearance', |
| 56 | + collapsible: true, |
| 57 | + fields: [ |
| 58 | + { key: 'theme', label: 'Theme', type: 'select', options: [ |
| 59 | + { value: 'light', label: 'Light' }, |
| 60 | + { value: 'dark', label: 'Dark' }, |
| 61 | + { value: 'system', label: 'System' }, |
| 62 | + ], defaultValue: 'system' }, |
| 63 | + { key: 'accentColor', label: 'Accent color', type: 'color', defaultValue: '#3b82f6' }, |
| 64 | + { key: 'fontSize', label: 'Font size', type: 'slider', min: 10, max: 24, step: 1, defaultValue: 14 }, |
| 65 | + ], |
| 66 | + }, |
| 67 | + { |
| 68 | + key: 'advanced', |
| 69 | + title: 'Advanced', |
| 70 | + collapsible: true, |
| 71 | + defaultCollapsed: true, |
| 72 | + hint: 'These settings are for power users.', |
| 73 | + fields: [ |
| 74 | + { key: 'debug', label: 'Debug mode', type: 'checkbox' }, |
| 75 | + { key: 'apiEndpoint', label: 'API endpoint', type: 'input', placeholder: 'https://...' }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + ], |
| 79 | +}; |
| 80 | + |
| 81 | +function BasicPanelStory() { |
| 82 | + const source = { title: 'My App', enabled: true, mode: 'auto', theme: 'system', accentColor: '#3b82f6', fontSize: 14, debug: false, apiEndpoint: '' }; |
| 83 | + const { draft, isDirty, updateField, discard } = useConfigDraft(source); |
| 84 | + return ( |
| 85 | + <div style={{ position: 'relative', height: 600, width: 320, border: '1px solid #e5e7eb', borderRadius: 8, overflow: 'hidden' }}> |
| 86 | + <ConfigPanelRenderer |
| 87 | + open={true} |
| 88 | + onClose={() => alert('Close clicked')} |
| 89 | + schema={basicSchema} |
| 90 | + draft={draft} |
| 91 | + isDirty={isDirty} |
| 92 | + onFieldChange={updateField} |
| 93 | + onSave={() => alert('Saved: ' + JSON.stringify(draft, null, 2))} |
| 94 | + onDiscard={discard} |
| 95 | + /> |
| 96 | + </div> |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +export const Basic: Story = { |
| 101 | + render: () => <BasicPanelStory />, |
| 102 | +}; |
| 103 | + |
| 104 | +// Dashboard-like schema |
| 105 | +const dashboardSchema: ConfigPanelSchema = { |
| 106 | + breadcrumb: ['Dashboard', 'Layout'], |
| 107 | + sections: [ |
| 108 | + { |
| 109 | + key: 'layout', |
| 110 | + title: 'Layout', |
| 111 | + fields: [ |
| 112 | + { key: 'columns', label: 'Columns', type: 'slider', min: 1, max: 12, step: 1, defaultValue: 3 }, |
| 113 | + { key: 'gap', label: 'Gap', type: 'slider', min: 0, max: 16, step: 1, defaultValue: 4 }, |
| 114 | + { key: 'rowHeight', label: 'Row height', type: 'input', defaultValue: '120', placeholder: 'px' }, |
| 115 | + ], |
| 116 | + }, |
| 117 | + { |
| 118 | + key: 'data', |
| 119 | + title: 'Data', |
| 120 | + collapsible: true, |
| 121 | + fields: [ |
| 122 | + { key: 'refreshInterval', label: 'Refresh interval', type: 'select', options: [ |
| 123 | + { value: '0', label: 'Manual' }, |
| 124 | + { value: '30', label: '30s' }, |
| 125 | + { value: '60', label: '1 min' }, |
| 126 | + { value: '300', label: '5 min' }, |
| 127 | + ], defaultValue: '0' }, |
| 128 | + { key: 'autoRefresh', label: 'Auto-refresh', type: 'switch', defaultValue: false }, |
| 129 | + ], |
| 130 | + }, |
| 131 | + { |
| 132 | + key: 'appearance', |
| 133 | + title: 'Appearance', |
| 134 | + collapsible: true, |
| 135 | + defaultCollapsed: true, |
| 136 | + fields: [ |
| 137 | + { key: 'showTitle', label: 'Show title', type: 'switch', defaultValue: true }, |
| 138 | + { key: 'showDescription', label: 'Show description', type: 'switch', defaultValue: true }, |
| 139 | + { key: 'theme', label: 'Theme', type: 'select', options: [ |
| 140 | + { value: 'light', label: 'Light' }, |
| 141 | + { value: 'dark', label: 'Dark' }, |
| 142 | + { value: 'auto', label: 'Auto' }, |
| 143 | + ], defaultValue: 'auto' }, |
| 144 | + ], |
| 145 | + }, |
| 146 | + ], |
| 147 | +}; |
| 148 | + |
| 149 | +function DashboardPanelStory() { |
| 150 | + const source = { columns: 3, gap: 4, rowHeight: '120', refreshInterval: '0', autoRefresh: false, showTitle: true, showDescription: true, theme: 'auto' }; |
| 151 | + const { draft, isDirty, updateField, discard } = useConfigDraft(source); |
| 152 | + return ( |
| 153 | + <div style={{ position: 'relative', height: 600, width: 320, border: '1px solid #e5e7eb', borderRadius: 8, overflow: 'hidden' }}> |
| 154 | + <ConfigPanelRenderer |
| 155 | + open={true} |
| 156 | + onClose={() => alert('Close')} |
| 157 | + schema={dashboardSchema} |
| 158 | + draft={draft} |
| 159 | + isDirty={isDirty} |
| 160 | + onFieldChange={updateField} |
| 161 | + onSave={() => alert('Saved!')} |
| 162 | + onDiscard={discard} |
| 163 | + /> |
| 164 | + </div> |
| 165 | + ); |
| 166 | +} |
| 167 | + |
| 168 | +export const DashboardConfig: Story = { |
| 169 | + render: () => <DashboardPanelStory />, |
| 170 | +}; |
| 171 | + |
| 172 | +// Custom field escape hatch |
| 173 | +const customSchema: ConfigPanelSchema = { |
| 174 | + breadcrumb: ['View', 'Config'], |
| 175 | + sections: [ |
| 176 | + { |
| 177 | + key: 'main', |
| 178 | + title: 'General', |
| 179 | + fields: [ |
| 180 | + { key: 'name', label: 'View name', type: 'input' }, |
| 181 | + { |
| 182 | + key: 'rowHeight', |
| 183 | + label: 'Row height', |
| 184 | + type: 'custom', |
| 185 | + render: (value, onChange) => ( |
| 186 | + <div style={{ display: 'flex', gap: 4, padding: '4px 0' }}> |
| 187 | + {['compact', 'medium', 'tall'].map((h) => ( |
| 188 | + <button |
| 189 | + key={h} |
| 190 | + onClick={() => onChange(h)} |
| 191 | + style={{ |
| 192 | + padding: '2px 8px', |
| 193 | + fontSize: 11, |
| 194 | + borderRadius: 4, |
| 195 | + border: value === h ? '2px solid #3b82f6' : '1px solid #d1d5db', |
| 196 | + background: value === h ? '#eff6ff' : 'transparent', |
| 197 | + cursor: 'pointer', |
| 198 | + }} |
| 199 | + > |
| 200 | + {h} |
| 201 | + </button> |
| 202 | + ))} |
| 203 | + </div> |
| 204 | + ), |
| 205 | + }, |
| 206 | + ], |
| 207 | + }, |
| 208 | + ], |
| 209 | +}; |
| 210 | + |
| 211 | +function CustomFieldStory() { |
| 212 | + const source = { name: 'My Grid View', rowHeight: 'medium' }; |
| 213 | + const { draft, isDirty, updateField, discard } = useConfigDraft(source); |
| 214 | + return ( |
| 215 | + <div style={{ position: 'relative', height: 400, width: 320, border: '1px solid #e5e7eb', borderRadius: 8, overflow: 'hidden' }}> |
| 216 | + <ConfigPanelRenderer |
| 217 | + open={true} |
| 218 | + onClose={() => {}} |
| 219 | + schema={customSchema} |
| 220 | + draft={draft} |
| 221 | + isDirty={isDirty} |
| 222 | + onFieldChange={updateField} |
| 223 | + onSave={() => alert('Saved!')} |
| 224 | + onDiscard={discard} |
| 225 | + /> |
| 226 | + </div> |
| 227 | + ); |
| 228 | +} |
| 229 | + |
| 230 | +export const CustomFieldEscapeHatch: Story = { |
| 231 | + render: () => <CustomFieldStory />, |
| 232 | +}; |
0 commit comments