Skip to content

Commit d619120

Browse files
authored
[docs] Add the Working with enumerations section to the custom components page. (#273)
1 parent 04046c0 commit d619120

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

.changeset/soft-clubs-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"docs2": minor
3+
---
4+
5+
Add the `Working with enumerations` section to the custom components page

apps/docs2/src/content/docs/guides/custom-components.mdx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,52 @@ previous category, have the suffix `Attributes`.
277277
</TabItem>
278278
</Tabs>
279279

280+
## Working with enumerations
281+
282+
To implement widgets that use enumerations (`options: EnumOption<SchemaValue>[]`),
283+
use the functions from the `@sjsf/form/options.svelte` module.
284+
285+
```ts
286+
import { idMapper, singleOption } from "@sjsf/form/options.svelte";
287+
288+
const mapped = singleOption({
289+
mapper: () => idMapper(options),
290+
value: () => value,
291+
update: (v) => (value = v),
292+
});
293+
```
294+
295+
Now, `mapped.value` will correspond to the `option.id` of the current option,
296+
and when it changes, value will be assigned the corresponding `option.value`:
297+
298+
```svelte
299+
<select bind:value={mapped.value} {...attributes}>
300+
<option value={UNDEFINED_ID}>{placeholder}</option>
301+
{#each options as option (option.id)}
302+
<option value={option.id} disabled={option.disabled}>
303+
{option.label}
304+
</option>
305+
{/each}
306+
</select>
307+
```
308+
309+
You can also use the `multipleOptions` function to implement multi-select behavior.
310+
In this case, `mapped.value` will be an array of `option.id`:
311+
312+
```svelte
313+
<script lang="ts">
314+
import { idMapper, singleOption } from "@sjsf/form/options.svelte";
315+
316+
const mapped = multipleOptions({
317+
mapper: () => idMapper(options),
318+
value: () => value,
319+
update: (v) => (value = v),
320+
})
321+
</script>
322+
323+
<select multiple bind:value={mapped.value} {...attributes}>
324+
...
325+
```
280326

281327
## Retrieving theme components
282328

0 commit comments

Comments
 (0)