sveltia / sveltia-cms

Alternative to Netlify/Decap CMS. Fast, lightweight, Git-based headless CMS. Modern UX, first-class i18n support, open source & free. Made with Svelte.
MIT License
862 stars 43 forks source link

Suggestion: hidden list field with current item index #172

Closed josineto closed 3 months ago

josineto commented 3 months ago

The idea is: in a List widget with fields (e.g. not "simple string list"), one could add a hidden field that holds the index of the current item in that list, like:

list:
- fruit: apple
  index: 0 # <---
- fruit: banana
  index: 1 # <---

It could be a hidden widget with default: '{{index}}', or a compute widget with value: '{{index}}', or even something else...

kyoshino commented 3 months ago

I could add support for the index, but indexes can be easily retrieved in the framework, right? In Svelte, that’s the each block’s second callback argument, and in Hugo, range $index, $value := $slice

josineto commented 3 months ago

indexes can be easily retrieved in the framework

Not always: in Hugo, it's idiomatic to filter a collection right before looping into it (a range (where <collection> <filter> expression). In that case, the indexes are not relative to the original collection, but to the filtered one, so an index field would be useful.

kyoshino commented 3 months ago

Okay, let’s see how the hidden index field would work.

kyoshino commented 3 months ago

The fix shipped with v0.35.0 🚢

You can now use widget: compute + value: '{{index}}'.

josineto commented 3 months ago

Thank you!