Skip to main content

ToggleInput Component

The <ToggleInput> component provides a unified interface for creating various toggleable input elements like checkboxes, radio buttons, and switches.

Basic Usage​

A number of components in the library have a subcomponent feature witch is like a preset of the same component that is frequently used. This component can be used to render checkboxes, radio buttons, and switches by specifying the subcomponent.

note

The component does not have a parent element. That means you can use the component only like a subcomponent.

import { useState } from "react";
import { ToggleInput } from "react-better-html";

function App() {
const [isChecked, setIsChecked] = useState(false);

return (
<ToggleInput.checkbox
label="Remember me"
checked={isChecked}
onChange={setIsChecked}
/>
);
}

Common Props​

All standard <input> attributes are valid props for this component. The component works both controlled (with checked) and uncontrolled. The onChange callback receives the new checked state and the value prop.

  • label - text rendered above the input (like the label of an <InputField>)
  • text - clickable text rendered next to the input (checkbox and radio button only)
  • textAdvanced - a custom element rendered next to the input instead of text (checkbox and radio button only)
  • size - the size of the checkbox and the radio button (defaults to 26)
  • activeColor / inactiveColor - the color of the check mark, the dot or the switch ball
  • activeBackgroundColor / inactiveBackgroundColor - the background color of the input
  • errorText / infoText - text rendered underneath the input
  • required - renders a red asterisk after the label or the text
import { Text, ToggleInput } from "react-better-html";

function App() {
return (
<ToggleInput.checkbox
textAdvanced={
<Text>
I agree with the <a href="/terms">terms and conditions</a>
</Text>
}
required
/>
);
}

The label and the text can be customized with labelColor, labelFontSize, labelFontFamily, labelLetterSpacing, labelTextTransform, labelGap and textFontSize, textFontFamily, textLetterSpacing, textTextTransform.