Skip to content

Switch

ESwitch A Switch displays two exclusive options and allows the user to toggle the status of a single item between checked and unchecked. Demo Here

Usage

jsx
import ESwitch from "@components/Form/ESwitch";

Usage

jsx
const SwitchExample = () => {
    const [isChecked, setIsChecked] = useState(false);

    const changeHandler = () => {
        setIsChecked((prevState) => !prevState);
    };

    return (
        <ESwitch
            label="Choose"
            value={value}
            onChange={changeHandler}
            options={options}
            isChecked={isChecked}
        >
            On
        </ESwitch>
    );
};

export default SwitchExample;
jsx
const SwitchExample = () => {
    const [isChecked, setIsChecked] = useState(false);

    const changeHandler = () => {
        setIsChecked((prevState) => !prevState);
    };

    return (
        <ESwitch
            falseValue="OFF"
            trueValue="ON"
            value={value}
            onChange={changeHandler}
            options={options}
            isChecked={isChecked}
        />
    );
};

export default SwitchExample;
jsx

const SwitchExample = () => {
    const [isChecked, setIsChecked] = useState(false);

    const changeHandler = () => {
        setIsChecked((prevState) => !prevState);
    };

    return (
        <ESwitch
            label="Choose"
            value={value}
            onChange={changeHandler}
            options={options}
            isChecked={isChecked}
            trueIcon={{name: "TickCircle"}}
            falseIcon={{name: "CloseCircle"}}
        />
    );
};

export default SwitchExample;

Props

For ESwitch component, the following props are available:

PropTypeDefaultNote
labelString-Component's label
idString-HTML id attribute.
disabledBooleanfalseDisables the ESwitch.
width[String, Number]-Sets the component's width. Default unit is px.
height[String, Number]-Sets the component's height. Default unit is px.
messageString-Adds a message below the switch.
isCheckedBoolean-If true, the element is checked.
validateOnString"input"Triggers validation on @values: submit, input.
rulesArray-Validation rules. For more info refer to Form Validation.
stateString-Validates the user-provided data against established criteria and performs display validation. States can be none if no validation is necessary, danger if the selected is valid, or success if it is acceptable.
trueValueString-Value of the switch if it's checked.
falseValueString-Value of the switch if it's un-checked.
classNameString-Overrides or extends the component's styles.
nameString-HTML name attribute.
trueIconObject-Displays a true icon when the switch is checked. More Info here.
falseIconObject-Displays a false icon when the switch is unchecked. More Info here.
iconObject-Adds an icon to the switch. More Info about icons here.
squareBooleanfalseRemoves rounded corners of the element.
colorString"primary"The given color is applied to the element - utility colors are supported (e.g primary or danger). you'll find a list of built-in classes on the src/styles/_vars.scsspage.

Callback Functions

The ESwitch component contains following callback functions.

FunctionDescription
onChangeControls the checked status of the element. function(event: React.SyntheticEvent, isChecked: _Boolean_) => void 👇

event: The event source of the callback.
isChecked: switch value.

Children

The ESwitch component provides a children props that enable you to customize the content.

ChildrenDescription
✔️Component’s content.

COPYRIGHT