Skip to content

Commit

Permalink
feat: allow empty select & radio
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanvier committed Mar 8, 2024
1 parent e2708b5 commit d1be6ae
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/guide/inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
`FormSelect` extends `FormInput` props and adds the following options:
- `options` Key-value object for select options.
- `multiple` Boolean for multiple select.
- `empty` Boolean to allow empty value

### Example
```VUE
Expand Down Expand Up @@ -62,6 +63,7 @@
`FormRadio` extends `FormInput` props and adds the following options:
- `options` Key-value object for select options.
- `default` Default value.
- `empty` Boolean to allow empty value

### Example
```VUE
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@wgr-sa/nuxt-form",
"description": "Form builder for Nuxt",
"version": "0.8.2",
"version": "0.8.3",
"repository": "https://github.com/WGR-SA/nuxt-form.git",
"author": "jeanvier",
"license": "MIT",
Expand Down
2 changes: 2 additions & 0 deletions playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<FormInput label="" type="checkbox" name="tosnlpd" :rules="[{ equals: [true], message: 'coucou' }]" />
<FormInput name="safs" :rules="['isNotEmpty']" />
<FormTextarea name="saffs" :rules="['isNotEmpty']" />
<FormRadio :options="{'O': 'Oui', 'N': 'Non'}" name="yo" laebl="Yo" />
<FormRadio :options="{'O': 'Oui', 'N': 'Non'}" name="yos" label="Yos" :rules="['isNotEmpty']" :empty="true" />
<FormSubmit>
Submit
</FormSubmit>
Expand Down
1 change: 1 addition & 0 deletions src/runtime/components/FormInputContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const props = defineProps<{
value?: string,
default?: string,
type?: string,
empty?: boolean,
[key: string]: any
}>()
const { getFieldErrors } = useFormValidator()
Expand Down
1 change: 1 addition & 0 deletions src/runtime/types/input.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare namespace FormInput {
multiple?: boolean,
default?: string,
type?: string,
empty?: boolean,
placeholder?: string,
[key: string]: any
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/utils/data/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export class FormDataHandler {
this.state[name] = value
}

setDefaultValue (field: any) {
setDefaultValue(field: FormInput.Container) {
if (field.type === 'checkbox') {
this.state[field.name] = 'false'
}
if (field.options) {
if (field.options && !field.empty) {
this.state[field.name] = field.value ?? field.default ?? Object.keys(field.options)[0]
}
if (field.checked) {
Expand Down

0 comments on commit d1be6ae

Please sign in to comment.