2025-09-11 18:38:28 +09:00
|
|
|
"use client";
|
|
|
|
|
|
2025-10-14 11:48:04 +09:00
|
|
|
import React, { useState } from "react";
|
2025-09-11 18:38:28 +09:00
|
|
|
import { ComponentRendererProps } from "@/types/component";
|
|
|
|
|
import { RadioBasicConfig } from "./types";
|
2025-10-14 11:48:04 +09:00
|
|
|
import { cn } from "@/lib/registry/components/common/inputStyles";
|
|
|
|
|
import { filterDOMProps } from "@/lib/utils/domPropsFilter";
|
2025-09-11 18:38:28 +09:00
|
|
|
|
|
|
|
|
export interface RadioBasicComponentProps extends ComponentRendererProps {
|
|
|
|
|
config?: RadioBasicConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RadioBasic 컴포넌트
|
|
|
|
|
* radio-basic 컴포넌트입니다
|
|
|
|
|
*/
|
|
|
|
|
export const RadioBasicComponent: React.FC<RadioBasicComponentProps> = ({
|
|
|
|
|
component,
|
|
|
|
|
isDesignMode = false,
|
|
|
|
|
isSelected = false,
|
2025-09-12 14:24:25 +09:00
|
|
|
isInteractive = false,
|
2025-09-11 18:38:28 +09:00
|
|
|
onClick,
|
|
|
|
|
onDragStart,
|
|
|
|
|
onDragEnd,
|
|
|
|
|
config,
|
|
|
|
|
className,
|
|
|
|
|
style,
|
2025-09-12 14:24:25 +09:00
|
|
|
formData,
|
|
|
|
|
onFormDataChange,
|
2025-09-11 18:38:28 +09:00
|
|
|
...props
|
|
|
|
|
}) => {
|
|
|
|
|
// 컴포넌트 설정
|
|
|
|
|
const componentConfig = {
|
|
|
|
|
...config,
|
|
|
|
|
...component.config,
|
|
|
|
|
} as RadioBasicConfig;
|
|
|
|
|
|
2025-10-14 11:48:04 +09:00
|
|
|
// webType에 따른 세부 타입 결정 (TextInputComponent와 동일한 방식)
|
|
|
|
|
const webType = component.componentConfig?.webType || "radio";
|
|
|
|
|
|
|
|
|
|
// 상태 관리
|
|
|
|
|
const [selectedValue, setSelectedValue] = useState<string>(component.value || "");
|
|
|
|
|
|
2025-09-11 18:38:28 +09:00
|
|
|
// 스타일 계산 (위치는 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?.();
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-14 11:48:04 +09:00
|
|
|
const handleRadioChange = (value: string) => {
|
|
|
|
|
setSelectedValue(value);
|
|
|
|
|
if (component.onChange) {
|
|
|
|
|
component.onChange(value);
|
|
|
|
|
}
|
|
|
|
|
if (isInteractive && onFormDataChange && component.columnName) {
|
|
|
|
|
onFormDataChange(component.columnName, value);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-09-11 18:38:28 +09:00
|
|
|
|
2025-10-14 11:48:04 +09:00
|
|
|
// DOM 안전한 props만 필터링
|
|
|
|
|
const safeDomProps = filterDOMProps(props);
|
|
|
|
|
|
|
|
|
|
// 세부 타입별 렌더링
|
|
|
|
|
const renderRadioByWebType = () => {
|
|
|
|
|
const options = componentConfig.options || [
|
|
|
|
|
{ value: "option1", label: "옵션 1" },
|
|
|
|
|
{ value: "option2", label: "옵션 2" },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// radio-horizontal: 가로 배치
|
|
|
|
|
if (webType === "radio-horizontal") {
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"flex flex-row gap-4 rounded-lg border border-gray-200 bg-gray-50 p-3",
|
|
|
|
|
"transition-all hover:border-orange-400 hover:shadow-md",
|
|
|
|
|
isSelected && "ring-2 ring-orange-500",
|
|
|
|
|
)}
|
2025-09-11 18:38:28 +09:00
|
|
|
>
|
2025-10-14 11:48:04 +09:00
|
|
|
{options.map((option: any, index: number) => (
|
|
|
|
|
<label key={index} className="flex cursor-pointer items-center gap-2">
|
|
|
|
|
<input
|
|
|
|
|
type="radio"
|
|
|
|
|
name={component.id || "radio-group"}
|
|
|
|
|
value={option.value}
|
|
|
|
|
checked={selectedValue === option.value}
|
|
|
|
|
onChange={() => handleRadioChange(option.value)}
|
|
|
|
|
disabled={componentConfig.disabled || isDesignMode}
|
|
|
|
|
className="h-4 w-4 border-gray-300 text-blue-600 focus:ring-blue-500"
|
|
|
|
|
/>
|
|
|
|
|
<span className="text-sm text-gray-900">{option.label}</span>
|
|
|
|
|
</label>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// radio-vertical: 세로 배치
|
|
|
|
|
if (webType === "radio-vertical") {
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"flex flex-col gap-2 rounded-lg border border-gray-200 bg-gray-50 p-3",
|
|
|
|
|
"transition-all hover:border-orange-400 hover:shadow-md",
|
|
|
|
|
isSelected && "ring-2 ring-orange-500",
|
2025-09-22 14:13:05 +09:00
|
|
|
)}
|
2025-10-14 11:48:04 +09:00
|
|
|
>
|
|
|
|
|
{options.map((option: any, index: number) => (
|
|
|
|
|
<label key={index} className="flex cursor-pointer items-center gap-2">
|
|
|
|
|
<input
|
|
|
|
|
type="radio"
|
|
|
|
|
name={component.id || "radio-group"}
|
|
|
|
|
value={option.value}
|
|
|
|
|
checked={selectedValue === option.value}
|
|
|
|
|
onChange={() => handleRadioChange(option.value)}
|
|
|
|
|
disabled={componentConfig.disabled || isDesignMode}
|
|
|
|
|
className="h-4 w-4 border-gray-300 text-blue-600 focus:ring-blue-500"
|
|
|
|
|
/>
|
|
|
|
|
<span className="text-sm text-gray-900">{option.label}</span>
|
|
|
|
|
</label>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-09-11 18:38:28 +09:00
|
|
|
|
2025-10-14 11:48:04 +09:00
|
|
|
// radio (기본 라디오 - direction 설정 따름)
|
|
|
|
|
return (
|
2025-09-11 18:38:28 +09:00
|
|
|
<div
|
2025-10-14 11:48:04 +09:00
|
|
|
className={cn(
|
|
|
|
|
"flex gap-3 rounded-lg border border-gray-200 bg-gray-50 p-3",
|
|
|
|
|
"transition-all hover:border-orange-400 hover:shadow-md",
|
|
|
|
|
componentConfig.direction === "horizontal" ? "flex-row" : "flex-col",
|
|
|
|
|
isSelected && "ring-2 ring-orange-500",
|
|
|
|
|
)}
|
2025-09-11 18:38:28 +09:00
|
|
|
>
|
2025-10-14 11:48:04 +09:00
|
|
|
{options.map((option: any, index: number) => (
|
|
|
|
|
<label key={index} className="flex cursor-pointer items-center gap-2">
|
2025-09-11 18:38:28 +09:00
|
|
|
<input
|
|
|
|
|
type="radio"
|
|
|
|
|
name={component.id || "radio-group"}
|
|
|
|
|
value={option.value}
|
2025-10-14 11:48:04 +09:00
|
|
|
checked={selectedValue === option.value}
|
|
|
|
|
onChange={() => handleRadioChange(option.value)}
|
|
|
|
|
disabled={componentConfig.disabled || isDesignMode}
|
2025-09-11 18:38:28 +09:00
|
|
|
required={componentConfig.required || false}
|
2025-10-14 11:48:04 +09:00
|
|
|
className="h-4 w-4 border-gray-300 text-blue-600 focus:ring-blue-500"
|
2025-09-11 18:38:28 +09:00
|
|
|
/>
|
2025-10-14 11:48:04 +09:00
|
|
|
<span className="text-sm text-gray-900">{option.label}</span>
|
2025-09-11 18:38:28 +09:00
|
|
|
</label>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
2025-10-14 11:48:04 +09:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div style={componentStyle} className={className} {...safeDomProps}>
|
|
|
|
|
{/* 라벨 렌더링 */}
|
|
|
|
|
{component.label && component.style?.labelDisplay !== false && (
|
|
|
|
|
<label className="absolute -top-6 left-0 text-sm font-medium text-slate-600">
|
|
|
|
|
{component.label}
|
|
|
|
|
{component.required && <span className="text-red-500">*</span>}
|
|
|
|
|
</label>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* 세부 타입별 UI 렌더링 */}
|
|
|
|
|
{renderRadioByWebType()}
|
2025-09-11 18:38:28 +09:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RadioBasic 래퍼 컴포넌트
|
|
|
|
|
* 추가적인 로직이나 상태 관리가 필요한 경우 사용
|
|
|
|
|
*/
|
|
|
|
|
export const RadioBasicWrapper: React.FC<RadioBasicComponentProps> = (props) => {
|
|
|
|
|
return <RadioBasicComponent {...props} />;
|
|
|
|
|
};
|