|
| 1 | +<template> |
| 2 | + <VStepperForm |
| 3 | + v-model="answers" |
| 4 | + :pages="pages" |
| 5 | + :validationSchema="validationSchema" |
| 6 | + @submit="submitForm" |
| 7 | + > |
| 8 | + </VStepperForm> |
| 9 | + |
| 10 | + <v-container> |
| 11 | + <VCodeBlock |
| 12 | + class="mt-4 mb-2" |
| 13 | + :code="JSON.stringify(answers, null, 2)" |
| 14 | + label="Answers" |
| 15 | + lang="javascript" |
| 16 | + prismjs |
| 17 | + /> |
| 18 | + </v-container> |
| 19 | +</template> |
| 20 | + |
| 21 | +<script setup lang="ts"> |
| 22 | +import type { MaybeRef } from 'vue'; |
| 23 | +import { VCodeBlock } from '@wdns/vue-code-block'; |
| 24 | +import { |
| 25 | + object as yupObject, |
| 26 | + string as yupString, |
| 27 | +} from 'yup'; |
| 28 | +
|
| 29 | +
|
| 30 | +const answers = ref<{ |
| 31 | + animal: string | null; |
| 32 | + name: string | null; |
| 33 | + slug: string | null; |
| 34 | +}>({ |
| 35 | + animal: null, |
| 36 | + name: null, |
| 37 | + slug: null, |
| 38 | +}); |
| 39 | +
|
| 40 | +const fooItems = [ |
| 41 | + { |
| 42 | + title: 'Foo', |
| 43 | + value: 'foo', |
| 44 | + }, |
| 45 | + { |
| 46 | + title: 'Fiz', |
| 47 | + value: 'fiz', |
| 48 | + }, |
| 49 | +]; |
| 50 | +
|
| 51 | +const barItems = [ |
| 52 | + { |
| 53 | + title: 'Baz', |
| 54 | + value: 'baz', |
| 55 | + }, |
| 56 | + { |
| 57 | + title: 'Biz', |
| 58 | + value: 'biz', |
| 59 | + }, |
| 60 | +]; |
| 61 | +
|
| 62 | +const selectItems = ref(fooItems); |
| 63 | +
|
| 64 | +const pages = computed(() => { |
| 65 | + return [ |
| 66 | + { |
| 67 | + fields: [ |
| 68 | + { |
| 69 | + label: 'Name', |
| 70 | + name: 'name', |
| 71 | + type: 'text' as const, |
| 72 | + }, |
| 73 | + { |
| 74 | + label: 'Slug', |
| 75 | + name: 'slug', |
| 76 | + type: 'text' as const, |
| 77 | + }, |
| 78 | + { |
| 79 | + items: selectItems.value, |
| 80 | + label: 'Foobar', |
| 81 | + name: 'animal', |
| 82 | + type: 'select' as const, |
| 83 | + }, |
| 84 | + ], |
| 85 | + }, |
| 86 | + ]; |
| 87 | +}); |
| 88 | +
|
| 89 | +const validationSchema = yupObject({ |
| 90 | + name: yupString().required('First Name'), |
| 91 | + slug: yupString().required('Slug'), |
| 92 | +}); |
| 93 | +
|
| 94 | +const useSlugifyString: UseSlugifyString = (string, divider = '_') => { |
| 95 | + if (divider !== '_' && divider !== '-') { |
| 96 | + throw new Error('[slugifyStringHelper]: Divider must be either "_" or "-"'); |
| 97 | + } |
| 98 | +
|
| 99 | + const unrefString = unref(string); |
| 100 | +
|
| 101 | + const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/-,:;'; |
| 102 | + const b = 'aaaaaaaaaacccddeeeeeeeegghiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz------'; |
| 103 | + const p = new RegExp(a.split('').join('|'), 'g'); |
| 104 | +
|
| 105 | + let response = unrefString.toString().toLowerCase() |
| 106 | + // Replace spaces with divider // |
| 107 | + .replace(/\s+/g, divider) |
| 108 | + .replace(p, (c) => b.charAt(a.indexOf(c))) |
| 109 | + .replace(/&/g, `${divider}and${divider}`) |
| 110 | + .replace(/[^\w-]+/g, ''); |
| 111 | +
|
| 112 | + if (divider === '_') { |
| 113 | + response = response.replace(/-+/g, '_') |
| 114 | + .replace(/__+/g, '_') |
| 115 | + .replace(/^_/, '') |
| 116 | + .replace(/^-+/, '') |
| 117 | + .replace(/-+$/, ''); |
| 118 | + } |
| 119 | +
|
| 120 | + if (divider === '-') { |
| 121 | + response = response.replace(/_+/g, '-') |
| 122 | + .replace(/--+/g, '-') |
| 123 | + .replace(/^-/, '') |
| 124 | + .replace(/^_+/, '') |
| 125 | + .replace(/_+$/, ''); |
| 126 | + } |
| 127 | +
|
| 128 | + return response; |
| 129 | +}; |
| 130 | +
|
| 131 | +watch(() => answers.value.name, (newVal) => { |
| 132 | + if (answers.value.slug === 'foo' || answers.value.slug === '') { |
| 133 | + selectItems.value = fooItems; |
| 134 | + } |
| 135 | +
|
| 136 | + if (answers.value.slug === 'bar') { |
| 137 | + selectItems.value = barItems; |
| 138 | + } |
| 139 | +
|
| 140 | + setTimeout(() => { |
| 141 | + answers.value.slug = newVal ? useSlugifyString(newVal) : ''; |
| 142 | + }, 1); |
| 143 | +}); |
| 144 | +
|
| 145 | +
|
| 146 | +export interface UseSlugifyString { |
| 147 | + ( |
| 148 | + string: MaybeRef<string>, |
| 149 | + divider?: string, |
| 150 | + ): string; |
| 151 | +}; |
| 152 | +
|
| 153 | +function submitForm() { |
| 154 | + console.log('submitForm', answers.value); |
| 155 | +} |
| 156 | +</script> |
| 157 | + |
| 158 | +<style lang="scss" scoped></style> |
0 commit comments