Overview
TheSwitchElement component provides a simple toggle switch interface for marking payments as paid or unpaid. It wraps the react-switch library with proper accessibility attributes and styling integration.
Features
- Binary State Toggle: Simple on/off payment status control
- Accessibility: Includes proper label and aria attributes
- Controlled Component: Integrates with parent state management
- Visual Feedback: Clear indication of paid/unpaid status
- Custom Styling: Wrapped in a custom container for consistent appearance
Location
Props
The current state of the switch
true: Payment is marked as paid (switch is ON)false: Payment is unpaid (switch is OFF)
Callback function invoked when the switch is toggledSignature:Receives the new checked state as its argument.
Usage Example
Basic Implementation
Integration with usePago Hook
As used in thePago component:
With localStorage Persistence
Component Structure
The component renders:Wrapper Div
Theswitch-wrapper class provides:
- Consistent spacing and positioning
- Container for the switch and its label
- Custom styling hook point
Hidden Label
The label usesvisually-hidden class to:
- Provide context for screen readers
- Remain hidden from visual users
- Maintain accessibility without affecting layout
Ensure your CSS includes a
visually-hidden utility class that hides content visually but keeps it accessible to screen readers.Accessibility
ARIA and HTML Attributes
- htmlFor/id connection: Associates the label with the switch
- Visually hidden label: Provides context without visual clutter
- Switch role: Automatically handled by
react-switch
Screen Reader Behavior
Screen readers will announce:- “Switch” (from the label)
- Current state (“on” or “off”)
- Interactive control type
Keyboard Navigation
- Space/Enter: Toggle the switch
- Tab: Focus the switch
- Works without mouse interaction
Styling
The component uses theswitch-wrapper class for container styling.
CSS Example
react-switch Customization
Customize the switch appearance by passing additional props to theSwitch component:
Dependencies
react-switch- Toggle switch UI component
Installing react-switch
State Management in Pagosapp
In the Pagosapp context, the switch state is managed by theusePago hook:
- Updates component state immediately
- Persists state to localStorage
- Uses a compound key (
mes+pago.nombre) for unique identification
Visual Status Integration
The switch state affects the visual appearance of the parentPago component:
checked === true, the payment item displays with green styling to indicate completion.
Event Flow
User Interaction
- User clicks/taps the switch
react-switchinternal state changesonChangeevent fireshandleChange(newChecked)is called- Parent component updates state
- Component re-renders with new
checkedvalue - Visual feedback reflects new state
State Persistence
In Pagosapp:- Switch toggled
handleChangecalled viausePago- State updated in React
- Value saved to localStorage
- Persists across page reloads
Common Patterns
List of Payments
Controlled with Side Effects
Testing Considerations
Unit Test Example
Related Components
- Pago Component - Primary consumer of SwitchElement
- DatePickerElement - Complementary control in Pago
- Mes Component - Container for Pago components
react-switch Props Reference
For additional customization options, see the react-switch documentation. Commonly used additional props:disabled- Disable the switchonColor/offColor- Background colorsonHandleColor/offHandleColor- Handle colorshandleDiameter- Size of the toggle handlecheckedIcon/uncheckedIcon- Custom iconsclassName- Additional CSS class
Source Code Reference
Component implementation atsrc/components/SwitchElement.jsx:5-12
Label implementation at src/components/SwitchElement.jsx:8