|
| 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 { WidgetConfigPanel } from './WidgetConfigPanel'; |
| 12 | +import { DashboardWithConfig } from './DashboardWithConfig'; |
| 13 | +import type { DashboardSchema } from '@object-ui/types'; |
| 14 | + |
| 15 | +// ─── WidgetConfigPanel Stories ────────────────────────────────────────────── |
| 16 | + |
| 17 | +const widgetMeta = { |
| 18 | + title: 'Plugins/DashboardConfigPanel', |
| 19 | + parameters: { |
| 20 | + layout: 'padded', |
| 21 | + }, |
| 22 | + tags: ['autodocs'], |
| 23 | +} satisfies Meta; |
| 24 | + |
| 25 | +export default widgetMeta; |
| 26 | +type Story = StoryObj<typeof widgetMeta>; |
| 27 | + |
| 28 | +// --- WidgetConfigPanel --- |
| 29 | + |
| 30 | +const widgetConfig = { |
| 31 | + title: 'Revenue Chart', |
| 32 | + description: 'Monthly revenue by region', |
| 33 | + type: 'bar', |
| 34 | + object: 'orders', |
| 35 | + categoryField: 'region', |
| 36 | + valueField: 'amount', |
| 37 | + aggregate: 'sum', |
| 38 | + colorVariant: 'blue', |
| 39 | + actionUrl: '', |
| 40 | + layoutW: 2, |
| 41 | + layoutH: 1, |
| 42 | +}; |
| 43 | + |
| 44 | +function WidgetConfigStory() { |
| 45 | + const [config, setConfig] = useState(widgetConfig); |
| 46 | + return ( |
| 47 | + <div style={{ position: 'relative', height: 600, width: 320, border: '1px solid #e5e7eb', borderRadius: 8, overflow: 'hidden' }}> |
| 48 | + <WidgetConfigPanel |
| 49 | + open={true} |
| 50 | + onClose={() => alert('Close clicked')} |
| 51 | + config={config} |
| 52 | + onSave={(newConfig) => { |
| 53 | + setConfig(newConfig as typeof widgetConfig); |
| 54 | + alert('Saved: ' + JSON.stringify(newConfig, null, 2)); |
| 55 | + }} |
| 56 | + /> |
| 57 | + </div> |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +export const WidgetConfig: Story = { |
| 62 | + render: () => <WidgetConfigStory />, |
| 63 | +}; |
| 64 | + |
| 65 | +// --- DashboardWithConfig --- |
| 66 | + |
| 67 | +const dashboardSchema: DashboardSchema = { |
| 68 | + type: 'dashboard', |
| 69 | + title: 'Sales Dashboard', |
| 70 | + columns: 3, |
| 71 | + gap: 4, |
| 72 | + widgets: [ |
| 73 | + { |
| 74 | + id: 'mc-1', |
| 75 | + component: { |
| 76 | + type: 'metric-card', |
| 77 | + title: 'Total Revenue', |
| 78 | + value: '$128,430', |
| 79 | + icon: 'DollarSign', |
| 80 | + trend: 'up', |
| 81 | + trendValue: '+14.2%', |
| 82 | + }, |
| 83 | + layout: { x: 0, y: 0, w: 1, h: 1 }, |
| 84 | + }, |
| 85 | + { |
| 86 | + id: 'mc-2', |
| 87 | + component: { |
| 88 | + type: 'metric-card', |
| 89 | + title: 'Active Users', |
| 90 | + value: '3,842', |
| 91 | + icon: 'Users', |
| 92 | + trend: 'up', |
| 93 | + trendValue: '+8.1%', |
| 94 | + }, |
| 95 | + layout: { x: 1, y: 0, w: 1, h: 1 }, |
| 96 | + }, |
| 97 | + { |
| 98 | + id: 'mc-3', |
| 99 | + component: { |
| 100 | + type: 'metric-card', |
| 101 | + title: 'Churn Rate', |
| 102 | + value: '1.8%', |
| 103 | + icon: 'TrendingDown', |
| 104 | + trend: 'down', |
| 105 | + trendValue: '-0.3%', |
| 106 | + }, |
| 107 | + layout: { x: 2, y: 0, w: 1, h: 1 }, |
| 108 | + }, |
| 109 | + ], |
| 110 | + header: { |
| 111 | + showTitle: true, |
| 112 | + showDescription: true, |
| 113 | + }, |
| 114 | +}; |
| 115 | + |
| 116 | +const dashboardConfig = { |
| 117 | + columns: 3, |
| 118 | + gap: 4, |
| 119 | + rowHeight: '120', |
| 120 | + refreshInterval: '0', |
| 121 | + title: 'Sales Dashboard', |
| 122 | + showDescription: true, |
| 123 | + theme: 'auto', |
| 124 | +}; |
| 125 | + |
| 126 | +function DashboardWithConfigStory() { |
| 127 | + const [config, setConfig] = useState(dashboardConfig); |
| 128 | + return ( |
| 129 | + <div style={{ height: 600, width: '100%', border: '1px solid #e5e7eb', borderRadius: 8, overflow: 'hidden' }}> |
| 130 | + <DashboardWithConfig |
| 131 | + schema={dashboardSchema} |
| 132 | + config={config} |
| 133 | + onConfigSave={(newConfig) => { |
| 134 | + setConfig(newConfig as typeof dashboardConfig); |
| 135 | + }} |
| 136 | + defaultConfigOpen={true} |
| 137 | + /> |
| 138 | + </div> |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +export const DashboardWithConfigPanel: Story = { |
| 143 | + render: () => <DashboardWithConfigStory />, |
| 144 | +}; |
| 145 | + |
| 146 | +function DashboardWithConfigClosedStory() { |
| 147 | + const [config, setConfig] = useState(dashboardConfig); |
| 148 | + return ( |
| 149 | + <div style={{ height: 600, width: '100%', border: '1px solid #e5e7eb', borderRadius: 8, overflow: 'hidden' }}> |
| 150 | + <DashboardWithConfig |
| 151 | + schema={dashboardSchema} |
| 152 | + config={config} |
| 153 | + onConfigSave={(newConfig) => { |
| 154 | + setConfig(newConfig as typeof dashboardConfig); |
| 155 | + }} |
| 156 | + defaultConfigOpen={false} |
| 157 | + /> |
| 158 | + </div> |
| 159 | + ); |
| 160 | +} |
| 161 | + |
| 162 | +export const DashboardWithConfigClosed: Story = { |
| 163 | + render: () => <DashboardWithConfigClosedStory />, |
| 164 | +}; |
0 commit comments