Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/toggle-field-checkbox-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tinacms': patch
---

Associate the toggle field's label with its checkbox. The input now falls back to the form field name for its `id`, so clicking the label toggles the value and screen readers announce the field name.
9 changes: 5 additions & 4 deletions packages/tinacms/src/toolkit/fields/components/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as React from 'react';
import { Field } from '@toolkit/forms';

export interface ToggleProps {
name: string;
name?: string;
input: any;
field: ToggleFieldDefinition;
disabled?: boolean;
Expand All @@ -12,7 +12,7 @@ export interface ToggleProps {
onFocus?: <T>(_event?: React.FocusEvent<T>) => void;
}

interface ToggleFieldDefinition extends Field {
export interface ToggleFieldDefinition extends Field {
component: 'toggle';
toggleLabels?: boolean | FieldLabels;
}
Expand All @@ -26,6 +26,7 @@ export const Toggle: FC<ToggleProps> = ({
disabled = false,
}) => {
const checked = !!(input.value || input.checked);
const inputId = name ?? input.name;
let labels: null | FieldLabels = null;

if (field.toggleLabels) {
Expand Down Expand Up @@ -53,14 +54,14 @@ export const Toggle: FC<ToggleProps> = ({
</span>
)}
<div className='relative w-12 h-7'>
<ToggleInput id={name} type='checkbox' {...input} />
<ToggleInput type='checkbox' {...input} id={inputId} />
<label
className='bg-none p-0 outline-none w-12 h-7'
style={{
opacity: disabled ? 0.4 : 1,
pointerEvents: disabled ? 'none' : 'inherit',
}}
htmlFor={name}
htmlFor={inputId}
role='switch'
>
<div className='relative w-[48px] h-7 rounded-3xl bg-white shadow-inner border border-gray-200 pointer-events-none -ml-0.5'>
Expand Down
Loading