Skip to content

Commit 4ad419f

Browse files
committed
Merge branch 'master' into production
2 parents 82f8f5e + 9737aaf commit 4ad419f

File tree

7 files changed

+95
-13
lines changed

7 files changed

+95
-13
lines changed

apps/docs/.vitepress/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export default defineConfig({
4848
text: 'Form Generator',
4949
items: [
5050
{ text: 'Props', link: '/guide/form-generator/props' },
51-
{ text: 'Events', link: '/guide/form-generator/events' }
51+
{ text: 'Events', link: '/guide/form-generator/events' },
52+
{ text: 'Plugin options', link: '/guide/form-generator/plugin-options' }
5253
]
5354
},
5455
{
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
These are options you can pass to the plugin when installing it into your app.
2+
3+
## Usage
4+
5+
```ts
6+
import VueFormGenerator from '@kevinkosterr/vue3-form-generator'
7+
8+
app.use(
9+
VueFormGenerator,
10+
{} // Your options go here.
11+
)
12+
```
13+
14+
## `aliases`
15+
> An object where the component names are the keys and their aliases are its values.
16+
```ts
17+
{
18+
aliases: {
19+
'FieldText': 'FieldTextInput'
20+
}
21+
}
22+
```
23+
When defining something like this, the `type` inside a schema changes to everything
24+
after `"Field"`, so in this example a new schema would look like this:
25+
```ts
26+
{
27+
type: 'textinput',
28+
// Other code...
29+
}
30+
```
31+
32+
## `components`
33+
> An array of objects with two properties, `name` and `component` where name is the name of the component (must start with `"Field"`)
34+
> and component is the component instance.
35+
```ts
36+
import FieldTest from '@/components/FieldTest.vue'
37+
38+
{
39+
components: [
40+
{
41+
name: 'FieldTest',
42+
component: FieldTest
43+
}
44+
]
45+
}
46+
```
47+
48+
## `excludedComponents`
49+
> An array of field component names to exclude from the global components, this makes it
50+
> so the components can't be used inside your forms.
51+
```ts
52+
{
53+
excludedComponents: [ 'FieldObject', 'FieldNumber' ]
54+
}
55+
```
56+
57+
## `messages`
58+
> An object where the keys are the validator function names and the values its error messages.
59+
> This is for showing specific messages for your custom or existing components.
60+
```ts
61+
{
62+
messages: {
63+
'yourcustomvalidator': 'This is not valid, man!'
64+
}
65+
}
66+
```

src/fields/core/FieldSelect.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,13 @@ const { hint } = useFieldAttributes(model.value, field.value)
8888
const selectedNames: ComputedRef<string[]> = computed(() => {
8989
if (!currentModelValue.value) return []
9090
91-
const findOptionName = (option: FieldOption) => field.value.options.find(o => o.value === option.value)?.name ?? ''
91+
const findOptionName = (value: string) => field.value.options.find(o => o.value === value)?.name ?? false
9292
9393
if (Array.isArray(currentModelValue.value) && field.value.multiple) {
94-
return currentModelValue.value.map(o => findOptionName(o))
94+
return currentModelValue.value.map(findOptionName).filter(o => o !== false)
9595
} else {
96-
return [ field.value.options.find(o => o.value === currentModelValue.value)?.name ?? '' ]
96+
const optionName = findOptionName(currentModelValue.value)
97+
return optionName ? [ optionName ] : []
9798
}
9899
})
99100

src/fields/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { App, Component } from 'vue'
2+
import type { FieldPluginOptions } from '@/resources/types/generic'
23

34
import FieldText from '@/fields/core/FieldText.vue'
4-
import FieldCheckBox from '@/fields/core/FieldCheckbox.vue'
55
import FieldPassword from '@/fields/core/FieldPassword.vue'
66
import FieldSelect from '@/fields/core/FieldSelect.vue'
77
import FieldSelectNative from '@/fields/core/FieldSelectNative.vue'
@@ -21,20 +21,22 @@ import FieldButton from '@/fields/core/FieldButton.vue'
2121

2222

2323
const fieldComponents = {
24-
FieldColor, FieldText, FieldCheckBox, FieldPassword, FieldSelect, FieldSelectNative, FieldRadio,
25-
FieldNumber, FieldSubmit, FieldReset, FieldButton, FieldSwitch, FieldTextarea, FieldMask, FieldChecklist,
26-
FieldCheckbox, FieldObject
24+
FieldColor, FieldText, FieldPassword, FieldSelect, FieldSelectNative, FieldRadio, FieldNumber, FieldSubmit,
25+
FieldReset, FieldButton, FieldSwitch, FieldTextarea, FieldMask, FieldChecklist, FieldCheckbox, FieldObject
2726
} as const
2827

2928
type FieldComponentNames = keyof typeof fieldComponents
3029

3130
export default {
32-
install (app: App, aliases: Partial<Record<FieldComponentNames, string>>) {
31+
install (app: App, options: FieldPluginOptions) {
3332
const componentEntries = Object.entries(fieldComponents) as [FieldComponentNames, Component][]
33+
const isExcluded = (componentName: string) => options.excludedComponents ? options.excludedComponents.includes(componentName) : false
3434

3535
for (const [ name, component ] of componentEntries) {
36-
const alias: string | undefined = aliases[name]
37-
app.component(alias ?? name, component)
36+
if (!isExcluded(name)) {
37+
const alias: string | undefined = options.aliases ? options.aliases[name] : undefined
38+
app.component(alias ?? name, component)
39+
}
3840
}
3941
}
4042
}

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { App } from 'vue'
2-
import { PluginOptions } from '@/resources/types/generic'
2+
import { FieldPluginOptions, PluginOptions } from '@/resources/types/generic'
33

44
import { setMessages } from '@/validators/messages'
55
import { isObject } from '@/helpers'
@@ -19,7 +19,10 @@ const VueFormGenerator = {
1919
install (app: App, options: PluginOptions) {
2020
if (!options) options = {}
2121

22-
app.use(FormGeneratorFields, options.aliases ?? {})
22+
const fieldOptions: FieldPluginOptions = {
23+
aliases: options.aliases, excludedComponents: options.excludedComponents
24+
}
25+
app.use(FormGeneratorFields, fieldOptions)
2326
app.component('VueFormGenerator', FormGenerator)
2427

2528
if (options.messages !== undefined && isObject(options.messages)) {

src/resources/types/generic.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export type PluginOptions = {
1414
aliases?: Record<string, string>;
1515
messages?: Record<string, string>;
1616
components?: PluginComponentOption[];
17+
excludedComponents?: string[];
18+
}
19+
20+
export type FieldPluginOptions = {
21+
aliases?: PluginOptions['aliases'];
22+
excludedComponents?: PluginOptions['excludedComponents'];
1723
}
1824

1925
export type FormGeneratorSchema = {

tests/components/fields/FieldSelect.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ describe('FieldSelect', () => {
8282
await fieldSelect.vm.$nextTick()
8383
await fieldSelect.find('.vfg-select-option').trigger('click')
8484
expect(formWrapper.vm.model.selectModel.length).toBe(1)
85+
expect(fieldSelect.find('.vfg-select-label').find('span').element.innerHTML).toContain('Test 1')
8586

8687
await fieldSelect.find('.vfg-select-label').trigger('click')
8788
await fieldSelect.vm.$nextTick()
8889
await fieldSelect.findAll('.vfg-select-option')[2].trigger('click')
90+
await fieldSelect.vm.$nextTick()
91+
expect(fieldSelect.find('.vfg-select-label').find('span').element.innerHTML).toContain('Test 3')
8992
expect(formWrapper.vm.model.selectModel.length).toBe(2)
9093
})
9194

0 commit comments

Comments
 (0)