Skip to content

Commit cafd921

Browse files
authored
Merge pull request #154 from objectstack-ai/copilot/add-examples-to-ui-folder
2 parents 49c8a79 + b459d62 commit cafd921

26 files changed

+6294
-0
lines changed

examples/ui/README.md

Lines changed: 347 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,347 @@
1+
# ObjectStack UI Protocol Examples
2+
3+
This directory contains comprehensive examples demonstrating the **UI Protocol** of ObjectStack - the presentation layer that defines how users interact with data.
4+
5+
## 📚 What's Inside
6+
7+
This package contains three sub-packages demonstrating different aspects of the UI Protocol:
8+
9+
### 1. **Metadata Examples** (`metadata-examples/`)
10+
TypeScript/JSON configurations defining UI structure - the "what" of the UI:
11+
- **view.examples.ts** - Grid, Kanban, Calendar, Gantt views
12+
- **page.examples.ts** - Record, Home, App pages
13+
- **dashboard.examples.ts** - Analytics dashboards with widgets
14+
- **action.examples.ts** - User interactions and workflows
15+
- **app.examples.ts** - Complete application structures
16+
- **theme.examples.ts** - Visual styling and theming
17+
18+
### 2. **Custom Components** (`custom-components/`)
19+
React implementations showing how to build custom UI components:
20+
- CustomButton - Flexible button with variants and theming
21+
- CustomDataGrid - Advanced grid with sorting, filtering, pagination
22+
- Component registration system
23+
24+
### 3. **React Renderer** (`react-renderer/`)
25+
Shows how to render UI metadata using React - the "how" of the UI:
26+
- PageRenderer - Renders Page metadata
27+
- ComponentRenderer - Renders individual components
28+
- Template expression resolver for data binding
29+
- Complete integration examples
30+
31+
## 🎯 Key Concepts
32+
33+
### Data-Driven UI
34+
All UI components in ObjectStack are **metadata-driven** - they are defined as JSON/TypeScript configurations rather than hardcoded implementations. This enables:
35+
36+
- **Zero-Code Customization**: Modify UIs without changing source code
37+
- **Version Control**: Track UI changes like any other code
38+
- **Dynamic Generation**: Build UIs programmatically
39+
- **Multi-tenant Isolation**: Different UIs for different customers
40+
41+
### Separation of Concerns
42+
43+
The UI Protocol is completely decoupled from:
44+
- **Data Protocol**: Business objects and logic
45+
- **System Protocol**: Runtime configuration
46+
- **Implementation**: Can be rendered by any frontend (React, Vue, Angular)
47+
48+
### Best Practices Alignment
49+
50+
ObjectStack UI Protocol draws inspiration from industry leaders:
51+
52+
| Feature | Salesforce | ServiceNow | ObjectStack |
53+
|---------|-----------|-----------|-------------|
54+
| List Views | List Views | Lists | `ListView` (grid, kanban, calendar) |
55+
| Record Pages | Lightning Pages | Forms | `Page` (regions + components) |
56+
| Dashboards | Dashboards | Performance Analytics | `Dashboard` (widgets) |
57+
| Actions | Quick Actions | UI Actions | `Action` (modal, flow, script) |
58+
| Apps | Lightning Apps | Applications | `App` (navigation + branding) |
59+
60+
## 🚀 Usage Examples
61+
62+
### Basic Grid View
63+
```typescript
64+
import { ListView } from '@objectstack/spec/ui';
65+
66+
const taskGridView: ListView = {
67+
type: 'grid',
68+
columns: ['subject', 'status', 'priority', 'due_date'],
69+
filter: [{ field: 'status', operator: '$ne', value: 'completed' }],
70+
sort: [{ field: 'due_date', order: 'asc' }],
71+
};
72+
```
73+
74+
### Kanban Board
75+
```typescript
76+
const opportunityKanban: ListView = {
77+
type: 'kanban',
78+
columns: ['name', 'amount', 'close_date'],
79+
kanban: {
80+
groupByField: 'stage',
81+
summarizeField: 'amount',
82+
columns: ['name', 'amount', 'account_name'],
83+
},
84+
};
85+
```
86+
87+
### Interactive Dashboard
88+
```typescript
89+
import { Dashboard } from '@objectstack/spec/ui';
90+
91+
const salesDashboard: Dashboard = {
92+
name: 'sales_overview',
93+
label: 'Sales Overview',
94+
widgets: [
95+
{
96+
title: 'Total Revenue',
97+
type: 'metric',
98+
object: 'opportunity',
99+
valueField: 'amount',
100+
aggregate: 'sum',
101+
layout: { x: 0, y: 0, w: 3, h: 2 },
102+
},
103+
// ... more widgets
104+
],
105+
};
106+
```
107+
108+
### Custom Action
109+
```typescript
110+
import { Action } from '@objectstack/spec/ui';
111+
112+
const convertLeadAction: Action = {
113+
name: 'convert_lead',
114+
label: 'Convert Lead',
115+
type: 'flow',
116+
target: 'lead_conversion_flow',
117+
locations: ['record_header', 'list_item'],
118+
visible: 'status = "qualified"',
119+
};
120+
```
121+
122+
## 🔗 Integration with Data Protocol
123+
124+
UI components reference objects and fields defined in the Data Protocol:
125+
126+
```typescript
127+
// Data Protocol defines the object
128+
import { ObjectSchema, Field } from '@objectstack/spec/data';
129+
130+
const Task = ObjectSchema.create({
131+
name: 'task',
132+
fields: {
133+
subject: Field.text({ required: true }),
134+
status: Field.select({ options: [...] }),
135+
priority: Field.number({ min: 1, max: 5 }),
136+
},
137+
});
138+
139+
// UI Protocol defines how to display it
140+
const taskListView: ListView = {
141+
type: 'grid',
142+
data: { provider: 'object', object: 'task' },
143+
columns: ['subject', 'status', 'priority'],
144+
};
145+
```
146+
147+
## 📖 Learning Path
148+
149+
1. **Start Simple**: Review `view.examples.ts` for basic list and form views
150+
2. **Add Interactivity**: Explore `action.examples.ts` for user interactions
151+
3. **Build Analytics**: Study `dashboard.examples.ts` for reporting
152+
4. **Compose Layouts**: Check `page.examples.ts` for advanced layouts
153+
5. **Complete Apps**: See `app.examples.ts` for full application structure
154+
155+
## 🎨 Visual Customization
156+
157+
### Theme Variables
158+
```typescript
159+
const customTheme = {
160+
colors: {
161+
primary: '#4169E1',
162+
secondary: '#9370DB',
163+
success: '#00AA00',
164+
danger: '#FF0000',
165+
},
166+
fonts: {
167+
heading: 'Inter, sans-serif',
168+
body: 'Roboto, sans-serif',
169+
},
170+
};
171+
```
172+
173+
### App Branding
174+
```typescript
175+
const salesApp = {
176+
name: 'sales_crm',
177+
branding: {
178+
primaryColor: '#4169E1',
179+
logo: '/assets/sales-logo.png',
180+
favicon: '/assets/sales-favicon.ico',
181+
},
182+
};
183+
```
184+
185+
## 🔍 Advanced Features
186+
187+
### Dynamic Data Sources
188+
```typescript
189+
// Use custom API instead of ObjectStack metadata
190+
const customListView: ListView = {
191+
type: 'grid',
192+
data: {
193+
provider: 'api',
194+
read: {
195+
url: '/api/external/data',
196+
method: 'GET',
197+
headers: { 'X-API-Key': '{api_key}' },
198+
},
199+
},
200+
columns: ['id', 'name', 'value'],
201+
};
202+
```
203+
204+
### Conditional Visibility
205+
```typescript
206+
const adminAction: Action = {
207+
name: 'delete_all',
208+
label: 'Delete All',
209+
type: 'script',
210+
visible: 'user.role = "admin" AND user.department = "engineering"',
211+
locations: ['list_toolbar'],
212+
};
213+
```
214+
215+
### Multi-level Navigation
216+
```typescript
217+
navigation: [
218+
{
219+
id: 'sales',
220+
type: 'group',
221+
label: 'Sales',
222+
children: [
223+
{ id: 'leads', type: 'object', objectName: 'lead' },
224+
{ id: 'accounts', type: 'object', objectName: 'account' },
225+
{
226+
id: 'reports',
227+
type: 'group',
228+
label: 'Reports',
229+
children: [
230+
{ id: 'sales_report', type: 'dashboard', dashboardName: 'sales_dashboard' },
231+
],
232+
},
233+
],
234+
},
235+
]
236+
```
237+
238+
## 📝 Directory Structure
239+
240+
```
241+
examples/ui/
242+
├── README.md # This file
243+
├── metadata-examples/ # UI metadata configurations
244+
│ ├── README.md
245+
│ ├── package.json
246+
│ ├── tsconfig.json
247+
│ └── src/
248+
│ ├── view.examples.ts # List, Form, Kanban, Calendar views
249+
│ ├── page.examples.ts # Record, Home, App pages
250+
│ ├── dashboard.examples.ts # Widgets and analytics
251+
│ ├── action.examples.ts # Buttons and interactions
252+
│ ├── app.examples.ts # Application structure
253+
│ └── theme.examples.ts # Visual styling
254+
├── custom-components/ # React component implementations
255+
│ ├── README.md
256+
│ ├── package.json
257+
│ ├── tsconfig.json
258+
│ └── src/
259+
│ ├── components/
260+
│ │ ├── CustomButton.tsx
261+
│ │ └── CustomDataGrid.tsx
262+
│ └── registry.ts # Component registration
263+
└── react-renderer/ # React renderer for metadata
264+
├── README.md
265+
├── package.json
266+
├── tsconfig.json
267+
└── src/
268+
├── renderers/
269+
│ ├── PageRenderer.tsx
270+
│ └── ComponentRenderer.tsx
271+
├── utils/
272+
│ └── templateResolver.ts
273+
└── examples/
274+
└── SimpleApp.tsx
275+
```
276+
277+
## 🚀 Quick Start
278+
279+
### 1. View Metadata Examples
280+
281+
Explore the TypeScript metadata configurations:
282+
283+
```bash
284+
cd metadata-examples
285+
npm install
286+
npm run build
287+
# View the examples in src/
288+
```
289+
290+
### 2. Custom Components
291+
292+
See how to implement custom React components:
293+
294+
```bash
295+
cd custom-components
296+
npm install
297+
npm run dev
298+
```
299+
300+
### 3. React Renderer
301+
302+
See how to render metadata with React:
303+
304+
```bash
305+
cd react-renderer
306+
npm install
307+
npm run dev
308+
```
309+
310+
311+
## 🤝 Related Examples
312+
313+
- **`examples/crm`**: Full CRM application using these UI patterns
314+
- **`examples/todo`**: Simple Todo app demonstrating basic UI
315+
- **`examples/modern-fields`**: Modern field types and validation
316+
317+
## 📚 References
318+
319+
- [UI Protocol Specification](../../packages/spec/src/ui/)
320+
- [Data Protocol Specification](../../packages/spec/src/data/)
321+
- [ObjectStack Architecture](../../ARCHITECTURE.md)
322+
- [Salesforce Lightning Design System](https://www.lightningdesignsystem.com/)
323+
- [ServiceNow UI Builder](https://docs.servicenow.com/bundle/washington-application-development/page/administer/ui-builder/concept/ui-builder.html)
324+
325+
## 🛠️ Building Examples
326+
327+
```bash
328+
# Install dependencies
329+
pnpm install
330+
331+
# Build this example
332+
cd examples/ui
333+
pnpm build
334+
335+
# Build all examples
336+
pnpm -r build
337+
```
338+
339+
## 💡 Contributing
340+
341+
These examples are designed to be comprehensive learning resources. When adding new examples:
342+
343+
1. **Follow naming conventions**: Use `camelCase` for configuration properties
344+
2. **Add comments**: Explain WHY, not just WHAT
345+
3. **Show variations**: Demonstrate multiple approaches
346+
4. **Keep it real**: Use realistic business scenarios
347+
5. **Reference standards**: Link to Salesforce/ServiceNow equivalents when applicable

0 commit comments

Comments
 (0)