Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ButtonGroup/FormGroup): pass default sizes to children #1875

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/runtime/components/forms/Input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export default defineComponent({

const { emitFormBlur, emitFormInput, size: sizeFormGroup, color, inputId, name } = useFormGroup(props, config)

const size = computed(() => sizeButtonGroup.value || sizeFormGroup.value)
const size = computed(() => sizeButtonGroup.value ?? sizeFormGroup.value)

const modelModifiers = ref(defu({}, props.modelModifiers, { trim: false, lazy: false, number: false }))

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/components/forms/InputMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export default defineComponent({
const { size: sizeButtonGroup, rounded } = useInjectButtonGroup({ ui, props })
const { emitFormBlur, emitFormChange, inputId, color, size: sizeFormGroup, name } = useFormGroup(props, config)

const size = computed(() => sizeButtonGroup.value || sizeFormGroup.value)
const size = computed(() => sizeButtonGroup.value ?? sizeFormGroup.value)

const internalQuery = ref('')
const query = computed({
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/components/forms/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export default defineComponent({

const { emitFormChange, inputId, color, size: sizeFormGroup, name } = useFormGroup(props, config)

const size = computed(() => sizeButtonGroup.value || sizeFormGroup.value)
const size = computed(() => sizeButtonGroup.value ?? sizeFormGroup.value)

const onInput = (event: Event) => {
emit('update:modelValue', (event.target as HTMLInputElement).value)
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/components/forms/SelectMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export default defineComponent({
const { size: sizeButtonGroup, rounded } = useInjectButtonGroup({ ui, props })
const { emitFormBlur, emitFormChange, inputId, color, size: sizeFormGroup, name } = useFormGroup(props, config)

const size = computed(() => sizeButtonGroup.value || sizeFormGroup.value)
const size = computed(() => sizeButtonGroup.value ?? sizeFormGroup.value)

const internalQuery = ref('')
const query = computed({
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/composables/useButtonGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ export function useInjectButtonGroup ({ ui, props }: { ui: any, props: any }) {
onUnmounted(() => {
groupContext?.value.unregister(instance)
})

return {
size: computed(() => groupContext?.value.size || props.size),
size: computed(() => {
if (!groupContext?.value) return props.size
return groupContext?.value.size ?? ui.value.default.size
}),
rounded: computed(() => {
if (!groupContext || positionInGroup.value === -1) return ui.value.rounded
if (groupContext.value.children.length === 1) return groupContext.value.ui.rounded
Expand Down
9 changes: 8 additions & 1 deletion src/runtime/composables/useFormGroup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { inject, ref, computed } from 'vue'
import { type UseEventBusReturn, useDebounceFn } from '@vueuse/core'
import type { FormEvent, FormEventType, InjectedFormGroupValue } from '../types/form'
import { mergeConfig } from '../utils'
// @ts-expect-error
import appConfig from '#build/app.config'
import { formGroup } from '#ui/ui.config'

type InputProps = {
id?: string
Expand All @@ -11,6 +15,9 @@ type InputProps = {
legend?: string | null
}


const formGroupConfig = mergeConfig<typeof formGroup>(appConfig.ui.strategy, appConfig.ui.formGroup, formGroup)

export const useFormGroup = (inputProps?: InputProps, config?: any) => {
const formBus = inject<UseEventBusReturn<FormEvent, string> | undefined>('form-events', undefined)
const formGroup = inject<InjectedFormGroupValue | undefined>('form-group', undefined)
Expand Down Expand Up @@ -55,7 +62,7 @@ export const useFormGroup = (inputProps?: InputProps, config?: any) => {
name: computed(() => inputProps?.name ?? formGroup?.name.value),
size: computed(() => {
const formGroupSize = config.size[formGroup?.size.value as string] ? formGroup?.size.value : null
return inputProps?.size ?? formGroupSize ?? config?.default?.size
return inputProps?.size ?? formGroupSize ?? formGroupConfig?.default?.size
}),
color: computed(() => formGroup?.error?.value ? 'red' : inputProps?.color),
emitFormBlur,
Expand Down
Loading