Skip to content

Commit

Permalink
feat(conform-react)!: default aria attributes to true
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Jul 17, 2023
1 parent 5b68f7a commit 008a32b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 77 deletions.
1 change: 0 additions & 1 deletion docs/accessibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ function Example() {
<input
{...conform.input(message, {
type: 'text',
ariaAttributes: true,
})}
/>
<div id={conform.errorId(message)}>
Expand Down
4 changes: 0 additions & 4 deletions docs/integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ function Example() {
<input
ref={shadowInputRef}
{...conform.input(currency, {
ariaAttributes: true,
hidden: true,
})}
/>
Expand Down Expand Up @@ -148,7 +147,6 @@ function Select({ options, ...config }: SelectProps) {
<input
ref={shadowInputRef}
{...conform.input(config, {
ariaAttributes: true,
hidden: true,
})}
/>
Expand Down Expand Up @@ -184,7 +182,6 @@ function Select({ options, ...config }: SelectProps) {
<input
ref={shadowInputRef}
{...conform.input(config, {
ariaAttributes: true,
hidden: true,
})}
onFocus={() => customInputRef.current?.focus()}
Expand Down Expand Up @@ -217,7 +214,6 @@ function Select({ options, .. }: SelectProps) {
<input
ref={shadowInputRef}
{...conform.input(config, {
ariaAttributes: true,
hidden: true,
})}
onChange={(e) => setValue(e.target.value)}
Expand Down
1 change: 0 additions & 1 deletion packages/conform-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ function Example() {
<input
{...conform.input(title, {
type: 'text',
ariaAttributes: true,
})}
/>
</form>
Expand Down
26 changes: 15 additions & 11 deletions packages/conform-react/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ interface TextareaProps extends FormControlProps {

type BaseOptions =
| {
ariaAttributes?: false;
ariaAttributes?: true;
description?: boolean;
}
| {
ariaAttributes: true;
description?: boolean;
ariaAttributes: false;
};

type ControlOptions = BaseOptions & {
Expand All @@ -76,7 +76,7 @@ type InputOptions = ControlOptions &
* Cleanup `undefined` from the dervied props
* To minimize conflicts when merging with user defined props
*/
function cleanup<Props extends Object>(props: Props): Props {
function cleanup<Props>(props: Props): Props {
for (const key in props) {
if (props[key] === undefined) {
delete props[key];
Expand All @@ -87,21 +87,25 @@ function cleanup<Props extends Object>(props: Props): Props {
}

function getFormElementProps(
config: FieldConfig<any>,
options?: BaseOptions,
config: FieldConfig<unknown>,
options: BaseOptions = {},
): FormElementProps {
const hasAriaAttributes = options.ariaAttributes ?? true;

return cleanup({
id: config.id,
name: config.name,
form: config.form,
'aria-invalid':
options?.ariaAttributes && config.errorId && config.error?.length
hasAriaAttributes && config.errorId && config.error?.length
? true
: undefined,
'aria-describedby': options?.ariaAttributes
'aria-describedby': hasAriaAttributes
? [
config.errorId && config.error?.length ? config.errorId : undefined,
config.descriptionId && options?.description
config.descriptionId &&
options.ariaAttributes !== false &&
options.description
? config.descriptionId
: undefined,
].reduce((result, id) => {
Expand All @@ -120,7 +124,7 @@ function getFormElementProps(
}

function getFormControlProps(
config: FieldConfig<any>,
config: FieldConfig<unknown>,
options?: ControlOptions,
): FormControlProps {
return cleanup({
Expand Down Expand Up @@ -215,7 +219,7 @@ export function textarea<Schema extends Primitive | undefined | unknown>(
}

export function fieldset<
Schema extends Record<string, any> | undefined | unknown,
Schema extends Record<string, unknown> | undefined | unknown,
>(config: FieldConfig<Schema>, options?: BaseOptions): FormControlProps {
return getFormElementProps(config, options);
}
Expand Down
5 changes: 0 additions & 5 deletions playground/app/routes/input-attributes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,13 @@ export default function Example() {
<input
{...conform.input(title, {
type: 'text',
ariaAttributes: true,
description: enableDescription,
})}
/>
</Field>
<Field label="Description" config={description}>
<textarea
{...conform.textarea(description, {
ariaAttributes: true,
description: enableDescription,
})}
/>
Expand All @@ -98,15 +96,13 @@ export default function Example() {
<input
{...conform.input(images, {
type: 'file',
ariaAttributes: true,
description: enableDescription,
})}
/>
</Field>
<Field label="Tags" config={tags}>
<select
{...conform.select(tags, {
ariaAttributes: true,
description: enableDescription,
})}
>
Expand All @@ -124,7 +120,6 @@ export default function Example() {
<input
{...conform.input(rating, {
type: 'number',
ariaAttributes: true,
description: enableDescription,
})}
/>
Expand Down
Loading

0 comments on commit 008a32b

Please sign in to comment.