-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic form group impl with aria support
- Loading branch information
Showing
7 changed files
with
70 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useFormGroup'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { computed, Ref, shallowRef } from 'vue'; | ||
import { useLabel } from '../a11y/useLabel'; | ||
import { FieldTypePrefixes } from '../constants'; | ||
import { FormObject, Reactivify, TypedSchema } from '../types'; | ||
import { normalizeProps, useUniqId, withRefCapture } from '../utils/common'; | ||
|
||
export interface FormGroupProps<TInput extends FormObject = FormObject, TOutput extends FormObject = TInput> { | ||
label?: string; | ||
schema?: TypedSchema<TInput, TOutput>; | ||
} | ||
|
||
export function useFormGroup<TInput extends FormObject = FormObject, TOutput extends FormObject = TInput>( | ||
_props: Reactivify<FormGroupProps<TInput, TOutput>>, | ||
elementRef?: Ref<HTMLElement>, | ||
) { | ||
const id = useUniqId(FieldTypePrefixes.FormGroup); | ||
const props = normalizeProps(_props); | ||
const groupRef = elementRef || shallowRef<HTMLInputElement>(); | ||
|
||
const { labelProps, labelledByProps } = useLabel({ | ||
for: id, | ||
label: props.label, | ||
targetRef: groupRef, | ||
}); | ||
|
||
const groupProps = computed(() => { | ||
const isFieldSet = groupRef.value?.tagName === 'FIELDSET'; | ||
|
||
return withRefCapture( | ||
{ | ||
id, | ||
...(isFieldSet ? {} : labelledByProps.value), | ||
role: isFieldSet ? undefined : 'group', | ||
}, | ||
groupRef, | ||
elementRef, | ||
); | ||
}); | ||
|
||
return { | ||
groupRef, | ||
labelProps, | ||
groupProps, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<template> | ||
<fieldset v-bind="groupProps"> | ||
<legend v-bind="labelProps">Shipping Address</legend> | ||
|
||
<slot /> | ||
</fieldset> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { FormGroupProps, useFormGroup } from '@formwerk/core'; | ||
const props = defineProps<FormGroupProps>(); | ||
const { labelProps, groupProps } = useFormGroup(props); | ||
</script> |