Skip to content

Commit

Permalink
docs(BFormSelect): Refactor (bootstrap-vue-next#2460)
Browse files Browse the repository at this point in the history
* docs(BFormSelect): Refactor examples

* docs: Normalize options docs between checkbox, radio and select

* docs: Factor out options property documentation

* docs: remnove unused code fragments
  • Loading branch information
dwgray authored Dec 16, 2024
1 parent e865bb4 commit 5930a10
Show file tree
Hide file tree
Showing 21 changed files with 264 additions and 536 deletions.
2 changes: 0 additions & 2 deletions apps/docs/src/data/components/formSelect.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export default {
component: 'BFormSelectOption',
styleSpec: {kind: StyleKind.Tag, value: 'option'},
sourcePath: '/BFormSelect/BFormSelectOption.vue',
emits: [],
props: {
'': {
value: {
Expand Down Expand Up @@ -154,7 +153,6 @@ export default {
),
} satisfies Record<keyof BvnComponentProps['BFormSelectOptionGroup'], PropertyReference>,
},
emits: [],
slots: [
{
name: 'first',
Expand Down
46 changes: 46 additions & 0 deletions apps/docs/src/docs/components/_options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Options

## Options Property

`options` can be an array of strings or objects, or a key-value object. Available fields:

- **`value`** The selected value which will be set on `v-model`
- **`disabled`** Disables item for selection
- **`text`** Display text

`value` can be a string, number, or simple object. Avoid using complex types in values.

::: info NOTE
The BootstrapVue field `html` on the `options` object has been deprecated. See our
[Migration Guide](/docs/migration-guide/#v-html) for details.
:::

### Options as an array

<<< FRAGMENT ./demo/OptionsArray.ts#snippet{ts}

If an array entry is a string, it will be used for both the generated `value` and `text` fields.

You can mix using strings and [objects](#options-as-an-array-of-objects) in the array.

Internally, BootstrapVueNext will convert the above array to the following array (the
[array of objects](#options-as-an-array-of-objects)) format:

<<< FRAGMENT ./demo/OptionsObjectArray.ts#snippet{ts}

### Options as an array of objects

<<< FRAGMENT ./demo/OptionsObjectArrayRaw.ts#snippet{ts}

If `value` is missing, then `text` will be used as both the `value` and `text` fields.

Internally, BootstrapVueNext will convert the above array to the following array (the
[array of objects](#options-as-an-array-of-objects)) format:

<<< FRAGMENT ./demo/OptionsObjectArrayNormalized.ts#snippet{ts}

### Changing the option field names

If you want to customize the field property names (for example using `name` field for display
`text`) you can easily change them by setting the `text-field`, `value-field`, and
`disabled-field` props to a string that contains the property name you would like to use:
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
const options = [
{text: 'Item 1', value: 'first'},
{text: 'Item 2', value: 'second'},
{text: 'Item 3', value: 'third', disabled: true},
{text: 'Item 4'},
{
label: 'Grouped options',
options: [{text: 'Item< 3', value: 'third', disabled: true}, {text: 'Item 4'}],
},
{text: 'Item 5', value: {foo: 'bar', baz: true}},
]
// #endregion snippet
4 changes: 0 additions & 4 deletions apps/docs/src/docs/components/demo/RadioArray.ts

This file was deleted.

11 changes: 0 additions & 11 deletions apps/docs/src/docs/components/demo/RadioObjectArray.ts

This file was deleted.

10 changes: 0 additions & 10 deletions apps/docs/src/docs/components/demo/RadioObjectArrayNormalized.ts

This file was deleted.

27 changes: 27 additions & 0 deletions apps/docs/src/docs/components/demo/SelectCustomFields.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<template>
<BFormSelect
v-model="selected"
:options="exFieldNamesOptions"
class="mb-3"
value-field="item"
text-field="name"
disabled-field="notEnabled"
/>

<div class="mt-3">
Selected: <strong>{{ selected }}</strong>
</div>
</template>

<script setup lang="ts">
import {ref} from 'vue'
const exFieldNamesOptions = [
{item: 'A', name: 'Option A'},
{item: 'B', name: 'Option B'},
{item: 'D', name: 'Option C', notEnabled: true},
{item: {d: 1}, name: 'Option D'},
]
const selected = ref('A')
</script>
23 changes: 23 additions & 0 deletions apps/docs/src/docs/components/demo/SelectMultiValue.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<template>
<BFormSelect v-model="selected" :options="exMultiOptions" multiple :select-size="4" />

<div class="mt-3">
Selected: <strong>{{ selected }}</strong>
</div>
</template>

<script setup lang="ts">
import {ref} from 'vue'
const exMultiOptions = [
{value: 'a', text: 'This is First option'},
{value: 'b', text: 'Default Selected Option'},
{value: 'c', text: 'This is another option'},
{value: 'd', text: 'This one is disabled', disabled: true},
{value: 'e', text: 'This is option e'},
{value: 'f', text: 'This is option f'},
{value: 'g', text: 'This is option g'},
]
const selected = ref(['b'])
</script>
26 changes: 26 additions & 0 deletions apps/docs/src/docs/components/demo/SelectOptionsGroup.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<template>
<BFormSelect v-model="selected" :options="ex1GroupOptions" />

<div class="mt-3">
Selected: <strong>{{ selected }}</strong>
</div>
</template>

<script setup lang="ts">
import {ref} from 'vue'
const ex1GroupOptions = [
{value: null, text: 'Please select an option'},
{value: 'a', text: 'This is First option'},
{value: 'b', text: 'Selected Option', disabled: true},
{
label: 'Grouped options',
options: [
{value: {C: '3PO'}, text: 'Option with object value'},
{value: {R: '2D2'}, text: 'Another option with object value'},
],
},
]
const selected = ref()
</script>
21 changes: 21 additions & 0 deletions apps/docs/src/docs/components/demo/SelectOptionsManual.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<template>
<BFormSelect v-model="selected">
<BFormSelectOption :value="null">Please select an option</BFormSelectOption>
<BFormSelectOption value="a">Option A</BFormSelectOption>
<BFormSelectOption value="b" disabled>Option B (disabled)</BFormSelectOption>
<BFormSelectOptionGroup label="Grouped options">
<BFormSelectOption :value="{C: '3PO'}">Option with object value</BFormSelectOption>
<BFormSelectOption :value="{R: '2D2'}">Another option with object value</BFormSelectOption>
</BFormSelectOptionGroup>
</BFormSelect>

<div class="mt-3">
Selected: <strong>{{ selected }}</strong>
</div>
</template>

<script setup lang="ts">
import {ref} from 'vue'
const selected = ref(null)
</script>
27 changes: 27 additions & 0 deletions apps/docs/src/docs/components/demo/SelectOptionsMixed.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<template>
<BFormSelect v-model="selected" :options="exFirstSlotOptions" class="mb-3">
<!-- This slot appears above the options from 'options' prop -->
<template #first>
<BFormSelectOption :value="null" disabled>-- Please select an option --</BFormSelectOption>
</template>

<!-- These options will appear after the ones from 'options' prop -->
<BFormSelectOption value="C">Option C</BFormSelectOption>
<BFormSelectOption value="D">Option D</BFormSelectOption>
</BFormSelect>

<div class="mt-3">
Selected: <strong>{{ selected }}</strong>
</div>
</template>

<script setup lang="ts">
import {ref} from 'vue'
const exFirstSlotOptions = [
{value: 'A', text: 'Option A (from options prop)'},
{value: 'B', text: 'Option B (from options prop)'},
]
const selected = ref(null)
</script>
24 changes: 24 additions & 0 deletions apps/docs/src/docs/components/demo/SelectOverview.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
```vue
<template>
<BFormSelect v-model="selected" :options="ex1Options" />

<BFormSelect v-model="selected" :options="ex1Options" size="sm" class="mt-3" />

<div class="mt-3">
Selected: <strong>{{ selected }}</strong>
</div>
</template>

<script setup lang="ts">
import {ref} from 'vue'
const ex1Options = [
{value: null, text: 'Please select an option'},
{value: 'a', text: 'This is First option'},
{value: 'b', text: 'Selected Option'},
{value: {C: '3PO'}, text: 'This is an option with object value'},
{value: 'd', text: 'This one is disabled', disabled: true},
]
const selected = ref(null)
</script>
21 changes: 21 additions & 0 deletions apps/docs/src/docs/components/demo/SelectSingleValue.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<template>
<BFormSelect v-model="selected" :options="ex1Options" />

<div class="mt-3">
Selected: <strong>{{ selected }}</strong>
</div>
</template>

<script setup lang="ts">
import {ref} from 'vue'
const ex1Options = [
{value: null, text: 'Please select an option'},
{value: 'a', text: 'This is First option'},
{value: 'b', text: 'Selected Option'},
{value: {C: '3PO'}, text: 'This is an option with object value'},
{value: 'd', text: 'This one is disabled', disabled: true},
]
const selected = ref()
</script>
21 changes: 21 additions & 0 deletions apps/docs/src/docs/components/demo/SelectSizing.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<template>
<BFormSelect v-model="selected" :options="ex1Options" :select-size="4" />

<div class="mt-3">
Selected: <strong>{{ selected }}</strong>
</div>
</template>

<script setup lang="ts">
import {ref} from 'vue'
const ex1Options = [
{value: null, text: 'Please select an option'},
{value: 'a', text: 'This is First option'},
{value: 'b', text: 'Selected Option'},
{value: {C: '3PO'}, text: 'This is an option with object value'},
{value: 'd', text: 'This one is disabled', disabled: true},
]
const selected = ref()
</script>
43 changes: 2 additions & 41 deletions apps/docs/src/docs/components/form-checkbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,9 @@ For cross browser consistency, `BFormCheckboxGroup` and `BFormCheckbox` use Boot

<<< DEMO ./demo/CheckboxExample2.vue

## Checkbox group options array
## Options property

`options` can be an array of strings or objects. Available fields:

- `value` The selected value which will be set on `v-model`
- `disabled` Disables item for selection
- `text` Display text

`value` can be a string, number, or simple object. Avoid using complex types in values.

<<< FRAGMENT ./demo/CheckboxArray.ts#snippet{ts}

If an array entry is a string, it will be used for both the generated `value` and `text` fields.

You can mix using strings and [objects](#options-as-an-array-of-objects) in the array.

Internally, BootstrapVueNext will convert the above array to the following array (the
[array of objects](#options-as-an-array-of-objects)) format:

<<< FRAGMENT ./demo/CheckboxObjectArray.ts#snippet{ts}

::: info NOTE
The BootstrapVue field `html` on the `options` object has been deprecated. See our
[Migration Guide](/docs/migration-guide/#v-html) for details.
:::

### Options as an array of objects

<<< FRAGMENT ./demo/CheckboxObjectArrayRaw.ts#snippet{ts}

If `value` is missing, then `text` will be used as both the `value` and `text` fields.

Internally, BootstrapVueNext will convert the above array to the following array (the
[array of objects](#options-as-an-array-of-objects)) format:

<<< FRAGMENT ./demo/CheckboxObjectArrayNormalized.ts#snippet{ts}

### Changing the option field names

If you want to customize the field property names (for example using `name` field for display
`text`) you can easily change them by setting the `text-field`, `value-field`, and
`disabled-field` props to a string that contains the property name you would like to use:
<!--@include: ./_options.md{5,}-->

<<< DEMO ./demo/CheckboxCustomFields.vue

Expand Down
Loading

0 comments on commit 5930a10

Please sign in to comment.