Shopping cart(购物车)
购物车卡片列出商品、小计、税费与总额,并提供继续添加和购买操作。
loading
安装
bash
npm install @assistant-ui/generative-ui zod1
用法
将下方规格传给渲染器。提供 dispatch 可接收按钮或表单的动作载荷。演示只在本地显示操作反馈。
tsx
import { defaultGenerativeUILibrary, renderGenerativeUI } from "@assistant-ui/generative-ui/react";
renderGenerativeUI(spec, defaultGenerativeUILibrary, {
status: "done",
dispatch: (action) => {
// 将用户操作交给应用。
console.log(action);
},
});1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
规格
tsx
const spec = {
"$type": "Card",
"title": "购物车",
"children": [
{
"$type": "ListView",
"$key": "items",
"children": [
{
"$type": "ListViewItem",
"$key": "item-1",
"children": {
"$type": "Row",
"justify": "between",
"children": [
{
"$type": "Text",
"$key": "name",
"value": "无线耳机"
},
{
"$type": "Caption",
"$key": "price",
"value": "$79.00"
}
]
}
},
{
"$type": "ListViewItem",
"$key": "item-2",
"children": {
"$type": "Row",
"justify": "between",
"children": [
{
"$type": "Text",
"$key": "name",
"value": "USB-C 数据线(2 米)"
},
{
"$type": "Caption",
"$key": "price",
"value": "$12.00"
}
]
}
},
{
"$type": "ListViewItem",
"$key": "item-3",
"children": {
"$type": "Row",
"justify": "between",
"children": [
{
"$type": "Text",
"$key": "name",
"value": "手机保护壳"
},
{
"$type": "Caption",
"$key": "price",
"value": "$18.00"
}
]
}
}
]
},
{
"$type": "Divider",
"$key": "divider"
},
{
"$type": "Row",
"$key": "totals",
"justify": "between",
"children": [
{
"$type": "Fact",
"$key": "subtotal",
"label": "小计",
"value": "$109.00"
},
{
"$type": "Fact",
"$key": "tax",
"label": "税费",
"value": "$9.81"
},
{
"$type": "Fact",
"$key": "total",
"label": "总计",
"value": "$118.81"
}
]
},
{
"$type": "Row",
"$key": "actions",
"justify": "end",
"children": [
{
"$type": "Button",
"$key": "add",
"label": "加入购物车",
"buttonStyle": "outline",
"$action": {
"type": "add_to_cart"
}
},
{
"$type": "Button",
"$key": "purchase",
"label": "购买",
"buttonStyle": "primary",
"$action": {
"type": "purchase_cart"
}
}
]
}
]
}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
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