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.
The component does not have a parent element. That means you can use the component only like a subcomponent.
- Checkbox
- Radio Button
- Switch
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}
/>
);
}
import { useState } from "react";
import { Div, Text, ToggleInput } from "react-better-html";
function App() {
const [selectedFruit, setSelectedFruit] = useState("apple");
return (
<Div>
<Text marginBottom={10}>Select a fruit</Text>
<ToggleInput.radiobutton
text="Apple"
value="apple"
checked={selectedFruit === "apple"}
onChange={(checked, value) => setSelectedFruit(value)}
/>
<ToggleInput.radiobutton
text="Banana"
value="banana"
checked={selectedFruit === "banana"}
onChange={(checked, value) => setSelectedFruit(value)}
/>
</Div>
);
}
import { useState } from "react";
import { ToggleInput } from "react-better-html";
function App() {
const [isSwitchOn, setIsSwitchOn] = useState(false);
return (
<ToggleInput.switch
label="Enable Feature"
checked={isSwitchOn}
onChange={setIsSwitchOn}
/>
);
}
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 oftext(checkbox and radio button only)size- the size of the checkbox and the radio button (defaults to26)activeColor/inactiveColor- the color of the check mark, the dot or the switch ballactiveBackgroundColor/inactiveBackgroundColor- the background color of the inputerrorText/infoText- text rendered underneath the inputrequired- renders a red asterisk after the label or the text
- Text Advanced
- Custom Colors
- With Error
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
/>
);
}
import { ToggleInput } from "react-better-html";
function App() {
return (
<ToggleInput.switch
label="Notifications"
activeBackgroundColor="#28a745"
inactiveBackgroundColor="#dc3545"
/>
);
}
import { ToggleInput } from "react-better-html";
function App() {
return (
<ToggleInput.checkbox
text="Accept the privacy policy"
errorText="You need to accept the privacy policy"
/>
);
}
The label and the text can be customized with labelColor, labelFontSize, labelFontFamily, labelLetterSpacing, labelTextTransform, labelGap and textFontSize, textFontFamily, textLetterSpacing, textTextTransform.