File size: 1,184 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import React, { useEffect, FC, useMemo, useState, useRef } from "react";
import "./index.module.scss";
import { defaultTheme } from "@/core/config";
export interface switchType {
  sole: string;
  label: string;
  onChange?: Function;
  theme?: string;
}

const Index: FC<switchType> = function Index({ sole, label, onChange, theme }) {
  const [on, setOn] = useState("no");

  const refs = useRef<HTMLInputElement>(null!);

  const switchChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
    e.stopPropagation();
    const status = e.target.value === "yes" ? "no" : "yes";
    setOn(status);
    onChange && onChange(status);
  };
  useEffect(() => {
    refs.current.style.setProperty("--JoL-theme", theme ? theme : defaultTheme);
  }, [theme]);

  const render = useMemo(
    () => (
      <div className="container">
        <label htmlFor={sole} className="label">
          {label}
        </label>
        <input
          ref={refs}
          className="switch"
          type="checkbox"
          id={sole}
          onChange={switchChange}
          value={on}
        />
      </div>
    ),
    [sole, label, on]
  );
  return render;
};

export default Index;