1 | <script>
|
2 | import GlFormSelect from '../../src/components/base/form/form_select/form_select.vue';
|
3 | import { getDocumentationFor } from '../components_documentation';
|
4 | import GlExampleDisplay from './example_display.vue';
|
5 |
|
6 | export default {
|
7 | components: {
|
8 | GlExampleDisplay,
|
9 | GlFormSelect,
|
10 | },
|
11 | props: {
|
12 | componentName: {
|
13 | type: String,
|
14 | required: true,
|
15 | },
|
16 | },
|
17 | data() {
|
18 | return {
|
19 | selectedExampleId: '',
|
20 | };
|
21 | },
|
22 | computed: {
|
23 | exampleGroups() {
|
24 | return getDocumentationFor(this.componentName).examples || [];
|
25 | },
|
26 | firstExampleId() {
|
27 | if (
|
28 | this.exampleGroups &&
|
29 | this.exampleGroups.length > 0 &&
|
30 | this.exampleGroups[0].items &&
|
31 | this.exampleGroups[0].items.length > 0
|
32 | ) {
|
33 | return this.exampleGroups[0].items[0].id;
|
34 | }
|
35 | return null;
|
36 | },
|
37 | },
|
38 | mounted() {
|
39 | if (this.firstExampleId) {
|
40 | this.selectedExampleId = this.firstExampleId;
|
41 | }
|
42 | },
|
43 | };
|
44 | </script>
|
45 |
|
46 | <template>
|
47 | <div>
|
48 | <h3>Examples</h3>
|
49 | <gl-form-select
|
50 | v-if="exampleGroups && exampleGroups.length > 0"
|
51 | v-model="selectedExampleId"
|
52 | class="mb-3"
|
53 | >
|
54 | <template v-for="exampleGroup in exampleGroups">
|
55 | <optgroup :key="exampleGroup.title" :label="exampleGroup.name"></optgroup>
|
56 | <template v-for="example in exampleGroup.items">
|
57 | <option :key="example.id" :value="example.id">{{ example.name }}</option>
|
58 | </template>
|
59 | </template>
|
60 | </gl-form-select>
|
61 | <gl-example-display v-if="selectedExampleId" :example-name="selectedExampleId" />
|
62 | </div>
|
63 | </template>
|