1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2025-06-15 13:36:35 +02:00

Add Switch component

This commit is contained in:
Aleksandr Gumroian
2020-08-18 10:13:57 +02:00
parent e422acc92f
commit c1b1d8c079
2 changed files with 63 additions and 13 deletions

45
src/bootstrap/Switch.js Normal file
View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2020 CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information.
*/
import React from "react";
import PropTypes from "prop-types";
import { useUID } from "react-uid";
Switch.propTypes = {
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element,
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]).isRequired,
id: PropTypes.string.isRequired,
helpText: PropTypes.string,
switchHeading: PropTypes.string,
};
export function Switch({
label, id, helpText, switchHeading, ...props
}) {
const uid = useUID();
return (
<div className="form-group">
<div className={`custom-control custom-switch ${!helpText ? "custom-control-inline" : ""} ${switchHeading ? "switch-heading" : ""}`.trim()}>
<input
type="checkbox"
className="custom-control-input"
id={uid}
{...props}
/>
<label className="custom-control-label" htmlFor={uid}>
{label}
</label>
{helpText && <small className="form-text text-muted mt-0 mb-3">{helpText}</small>}
</div>
</div>
);
}