Skip to content

Commit 7260140

Browse files
committed
refactor: label opt-in per field component
1 parent edf2640 commit 7260140

File tree

6 files changed

+16
-24
lines changed

6 files changed

+16
-24
lines changed

src/FormGroup.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script setup>
2-
import { ref, computed } from 'vue'
3-
import { getFieldComponentName, hasLabel } from '@/helpers'
2+
import { computed, useTemplateRef } from 'vue'
3+
import { getFieldComponentName } from '@/helpers'
44
5-
const fieldComponent = ref(null)
5+
const fieldComponent = useTemplateRef('fieldComponent')
66
77
const props = defineProps({
88
formOptions: {
@@ -39,8 +39,10 @@ const fieldId = computed(() => {
3939
})
4040
4141
const shouldHaveLabel = computed(() => {
42-
// checkbox will have their own label
43-
return hasLabel(props.field) && ![ props.field.inputType, props.field.type ].includes('checkbox') && !props.field.noLabel
42+
if (fieldComponent.value?.noLabel || props.field.noLabel === true) {
43+
return false
44+
}
45+
return props.field.label
4446
})
4547
</script>
4648

src/fields/core/FieldButton.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ import { useFieldProps } from '@/composables/index.ts'
1111
const props = defineProps(useFieldProps())
1212
1313
const { model, field } = toRefs(props)
14+
15+
defineExpose({ noLabel: true })
1416
</script>

src/fields/core/FieldCheckbox.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<input
3-
:id="id"
3+
:id="props.id"
44
type="checkbox"
55
:name="field.name"
66
:required="isRequired"
@@ -9,7 +9,7 @@
99
:checked="currentModelValue"
1010
@change="onFieldValueChanged"
1111
>
12-
<label v-if="field.label" style="margin-left: .4em" :for="id"> {{ field.label }}</label>
12+
<label v-if="field.label" style="margin-left: .4em" :for="props.id"> {{ field.label }}</label>
1313
</template>
1414

1515
<script setup>
@@ -49,5 +49,5 @@ const onFieldValueChanged = ({ target }) => {
4949
emits('onInput', target.checked)
5050
}
5151
52-
defineExpose({ hint })
52+
defineExpose({ hint, noLabel: true })
5353
</script>

src/fields/core/FieldReset.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ import { useFieldProps } from '@/composables/index.ts'
99
const props = defineProps(useFieldProps())
1010
1111
const { field } = toRefs(props)
12+
13+
defineExpose({ noLabel: true })
1214
</script>

src/fields/core/FieldSubmit.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ const props = defineProps(useFieldProps())
1717
const { model, field } = toRefs(props)
1818
1919
const { isDisabled } = useFieldAttributes(model.value, field.value)
20+
21+
defineExpose({ noLabel: true })
2022
</script>

src/helpers/index.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,6 @@ export function isNotEmpty (value: any): boolean {
6666
return !isEmpty(value)
6767
}
6868

69-
/**
70-
* Determine if a field should have a top-level label.
71-
*/
72-
export function hasLabel(field: Field): boolean {
73-
if (!('label' in field) || !field.label) return false
74-
75-
switch (field.type) {
76-
case 'button':
77-
case 'submit':
78-
case 'reset':
79-
return false
80-
default:
81-
return true
82-
}
83-
}
84-
8569
/**
8670
* Reset all properties to an empty value of the same type. Will recursively try and
8771
* reset all properties if an object has a property with another object as its value.

0 commit comments

Comments
 (0)