|
|
@ -1,19 +1,27 @@ |
|
|
|
import React, { memo, FC, useMemo, useState } from 'react'; |
|
|
|
import React, { useEffect, FC, useMemo, useState, useRef } from 'react'; |
|
|
|
import style from './index.module.scss'; |
|
|
|
import style from './index.module.scss'; |
|
|
|
|
|
|
|
import { defaultTheme } from '@/core/config'; |
|
|
|
export interface switchType { |
|
|
|
export interface switchType { |
|
|
|
sole: string; |
|
|
|
sole: string; |
|
|
|
label: string; |
|
|
|
label: string; |
|
|
|
onChange?: Function; |
|
|
|
onChange?: Function; |
|
|
|
|
|
|
|
theme?: string; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const Index: FC<switchType> = function Index({ sole, label, onChange }) { |
|
|
|
const Index: FC<switchType> = function Index({ sole, label, onChange, theme }) { |
|
|
|
const [on, setOn] = useState('no'); |
|
|
|
const [on, setOn] = useState('no'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const refs = useRef<HTMLInputElement>(null!); |
|
|
|
|
|
|
|
|
|
|
|
const switchChange: React.ChangeEventHandler<HTMLInputElement> = (e) => { |
|
|
|
const switchChange: React.ChangeEventHandler<HTMLInputElement> = (e) => { |
|
|
|
const status = e.target.value === 'yes' ? 'no' : 'yes'; |
|
|
|
const status = e.target.value === 'yes' ? 'no' : 'yes'; |
|
|
|
setOn(status); |
|
|
|
setOn(status); |
|
|
|
onChange && onChange(status); |
|
|
|
onChange && onChange(status); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
|
|
refs.current.style.setProperty('--JoL-theme', theme ? theme : defaultTheme); |
|
|
|
|
|
|
|
}, [theme]); |
|
|
|
|
|
|
|
|
|
|
|
const render = useMemo( |
|
|
|
const render = useMemo( |
|
|
|
() => ( |
|
|
|
() => ( |
|
|
|
<div className={style.container}> |
|
|
|
<div className={style.container}> |
|
|
@ -21,6 +29,7 @@ const Index: FC<switchType> = function Index({ sole, label, onChange }) { |
|
|
|
{label} |
|
|
|
{label} |
|
|
|
</label> |
|
|
|
</label> |
|
|
|
<input |
|
|
|
<input |
|
|
|
|
|
|
|
ref={refs} |
|
|
|
className={style.switch} |
|
|
|
className={style.switch} |
|
|
|
type="checkbox" |
|
|
|
type="checkbox" |
|
|
|
id={sole} |
|
|
|
id={sole} |
|
|
|