ERP-node/frontend/lib/registry/components/toggle-switch/ToggleSwitchComponent.tsx

160 lines
4.3 KiB
TypeScript
Raw Normal View History

2025-09-11 18:38:28 +09:00
"use client";
import React from "react";
import { ComponentRendererProps } from "@/types/component";
import { ToggleSwitchConfig } from "./types";
export interface ToggleSwitchComponentProps extends ComponentRendererProps {
config?: ToggleSwitchConfig;
}
/**
* ToggleSwitch
* toggle-switch
*/
export const ToggleSwitchComponent: React.FC<ToggleSwitchComponentProps> = ({
component,
isDesignMode = false,
isSelected = false,
onClick,
onDragStart,
onDragEnd,
config,
className,
style,
...props
}) => {
// 컴포넌트 설정
const componentConfig = {
...config,
...component.config,
} as ToggleSwitchConfig;
// 스타일 계산 (위치는 RealtimePreviewDynamic에서 처리하므로 제외)
const componentStyle: React.CSSProperties = {
width: "100%",
height: "100%",
...component.style,
...style,
};
// 디자인 모드 스타일
if (isDesignMode) {
componentStyle.border = "1px dashed #cbd5e1";
componentStyle.borderColor = isSelected ? "#3b82f6" : "#cbd5e1";
}
// 이벤트 핸들러
const handleClick = (e: React.MouseEvent) => {
e.stopPropagation();
onClick?.();
};
// DOM에 전달하면 안 되는 React-specific props 필터링
const {
selectedScreen,
onZoneComponentDrop,
onZoneClick,
componentConfig: _componentConfig,
component: _component,
isSelected: _isSelected,
onClick: _onClick,
onDragStart: _onDragStart,
onDragEnd: _onDragEnd,
size: _size,
position: _position,
style: _style,
...domProps
} = props;
return (
<div style={componentStyle} className={className} {...domProps}>
{/* 라벨 렌더링 */}
{component.label && (
<label
style={{
position: "absolute",
top: "-25px",
left: "0px",
fontSize: component.style?.labelFontSize || "14px",
color: component.style?.labelColor || "#374151",
fontWeight: "500",
}}
>
{component.label}
{component.required && <span style={{ color: "#ef4444" }}>*</span>}
</label>
)}
<label
style={{
display: "flex",
alignItems: "center",
gap: "12px",
cursor: "pointer",
width: "100%",
height: "100%",
fontSize: "14px",
}}
onClick={handleClick}
onDragStart={onDragStart}
onDragEnd={onDragEnd}
>
<div
style={{
position: "relative",
width: "48px",
height: "24px",
backgroundColor: component.value === true ? "#3b82f6" : "#d1d5db",
borderRadius: "12px",
transition: "background-color 0.2s",
cursor: componentConfig.disabled ? "not-allowed" : "pointer",
opacity: componentConfig.disabled ? 0.5 : 1,
}}
>
<input
type="checkbox"
checked={component.value === true || component.value === "true"}
disabled={componentConfig.disabled || false}
required={componentConfig.required || false}
style={{
position: "absolute",
opacity: 0,
width: "100%",
height: "100%",
cursor: "pointer",
}}
onChange={(e) => {
if (component.onChange) {
component.onChange(e.target.checked);
}
}}
/>
<div
style={{
position: "absolute",
top: "2px",
left: component.value === true ? "26px" : "2px",
width: "20px",
height: "20px",
backgroundColor: "white",
borderRadius: "50%",
transition: "left 0.2s",
boxShadow: "0 2px 4px rgba(0, 0, 0, 0.1)",
}}
/>
</div>
<span style={{ color: "#374151" }}>{componentConfig.toggleLabel || (component.value ? "켜짐" : "꺼짐")}</span>
</label>
</div>
);
};
/**
* ToggleSwitch
*
*/
export const ToggleSwitchWrapper: React.FC<ToggleSwitchComponentProps> = (props) => {
return <ToggleSwitchComponent {...props} />;
};