Skip to content

Commit 6eff909

Browse files
committed
fix(docs): reflect data edits in playground demo (#825)
The demo's Data editor silently failed to update on delete in two ways: - parsedData was a plain `let`, so the `bookCount` computed never recomputed; the "N items" count stayed stuck even on a valid delete. Made it a ref so the count and results update reactively. - onDataChange swallowed JSON.parse errors in an empty catch, so a mid-edit invalid state (commonly a trailing comma when deleting the last array element) updated nothing with no feedback. Added a dataError flag and a visible "Invalid JSON" badge in the Data header.
1 parent a84f9cd commit 6eff909

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

docs/.vitepress/theme/components/Playground.vue

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
<div class="playground-data">
5757
<div class="panel-header">
5858
<span class="panel-title">Data</span>
59-
<span class="panel-meta">{{ bookCount }} items</span>
59+
<span class="panel-meta" :class="{ 'panel-meta-error': dataError }">
60+
{{ dataError ? 'Invalid JSON' : `${bookCount} items` }}
61+
</span>
6062
</div>
6163
<textarea
6264
class="data-editor"
@@ -141,7 +143,8 @@ const searchPattern = ref('')
141143
const jsonData = ref(JSON.stringify(books, null, 2))
142144
const results = ref<any[]>([])
143145
const searchTime = ref<string | null>(null)
144-
let parsedData = [...books]
146+
const parsedData = ref<any[]>([...books])
147+
const dataError = ref(false)
145148
146149
const options = reactive({
147150
threshold: 0.6,
@@ -152,7 +155,7 @@ const options = reactive({
152155
useTokenSearch: false
153156
})
154157
155-
const bookCount = computed(() => parsedData.length)
158+
const bookCount = computed(() => parsedData.value.length)
156159
157160
const generatedCode = computed(() => {
158161
const opts: string[] = []
@@ -169,10 +172,13 @@ const generatedCode = computed(() => {
169172
170173
function onDataChange() {
171174
try {
172-
parsedData = JSON.parse(jsonData.value)
175+
parsedData.value = JSON.parse(jsonData.value)
176+
dataError.value = false
173177
doSearch()
174178
} catch {
175-
// invalid JSON, skip
179+
// Keep the last valid data, but flag that the editor holds invalid JSON
180+
// so deletions mid-edit (e.g. a trailing comma) don't fail silently.
181+
dataError.value = true
176182
}
177183
}
178184
@@ -189,7 +195,7 @@ function doSearch() {
189195
keys: ['title', 'author.firstName', 'author.lastName']
190196
}
191197
192-
const fuse = new Fuse(parsedData, fuseOptions)
198+
const fuse = new Fuse(parsedData.value, fuseOptions)
193199
const start = performance.now()
194200
results.value = fuse.search(searchPattern.value)
195201
searchTime.value = (performance.now() - start).toFixed(1)
@@ -326,6 +332,10 @@ function escapeHtml(str: string): string {
326332
font-family: var(--font-family-code);
327333
}
328334
335+
.panel-meta-error {
336+
color: var(--c-danger, #e53e3e);
337+
}
338+
329339
.data-editor {
330340
width: 100%;
331341
height: 300px;

0 commit comments

Comments
 (0)