Files
cunningham/packages/react/src/components/Forms/Switch/index.tsx
Anthony Le Courric c7000f37d2 🐛(react) fix props not recognized on the input element
The props propagation was adding unknown props to the input element,
which was causing an error warning to be displayed in the console.
2023-09-11 16:15:13 +02:00

51 lines
1.3 KiB
TypeScript

import React, { InputHTMLAttributes, forwardRef } from "react";
import classNames from "classnames";
import { Field, FieldProps } from ":/components/Forms/Field";
export type SwitchOnlyProps = {
label?: string;
labelSide?: "left" | "right";
};
export type SwitchProps = InputHTMLAttributes<HTMLInputElement> &
FieldProps &
SwitchOnlyProps;
export const Switch = forwardRef<HTMLInputElement, SwitchProps>(
({ label, labelSide = "left", ...props }: SwitchProps, ref) => {
const {
compact,
fullWidth,
rightText,
state,
text,
textItems,
...inputProps
} = props;
return (
<label
className={classNames(
"c__checkbox",
"c__switch",
"c__switch--" + labelSide,
{
"c__checkbox--disabled": props.disabled,
"c__switch--full-width": props.fullWidth,
},
)}
>
<Field compact={true} {...props}>
<div className="c__checkbox__container">
{label && <div className="c__checkbox__label">{label}</div>}
<div className="c__switch__rail__wrapper">
<input type="checkbox" {...inputProps} ref={ref} />
<div className="c__switch__rail" />
</div>
</div>
</Field>
</label>
);
},
);