import * as React from "@lynx-js/react";
import type { NodesRef } from "@lynx-js/types";
import {
fieldButton,
type FieldButtonVariantProps,
} from "@seed-design/lynx-css/recipes/field-button";
import {
FieldButtonProvider,
useFieldButton,
useFieldButtonButton,
useFieldButtonClearButton,
useFieldButtonContext,
type UseFieldButtonContext,
type UseFieldButtonProps,
} from "@seed-design/lynx-react-field-button";
import clsx from "clsx";
import { useScaleFeedback, type ScaleFeedbackTriggerProps } from "../../hooks/useScaleFeedback";
import { mergeProps } from "../../utils/merge-props";
import { toArray } from "../../utils/children";
import type { LynxHostProps, LynxPressableProps, LynxTextRef, LynxViewRef } from "../../types";
import { InternalIcon, type InternalIconProps } from "../Icon/Icon";
interface StyledFieldButtonContextValue extends UseFieldButtonContext {
variantProps: FieldButtonVariantProps;
scaleFeedbackTriggerProps: ScaleFeedbackTriggerProps;
}
function isStyledFieldButtonContext(
context: UseFieldButtonContext,
): context is StyledFieldButtonContextValue {
return "variantProps" in context;
}
function useStyledFieldButtonContext(consumer: string): StyledFieldButtonContextValue {
const context = useFieldButtonContext();
if (!isStyledFieldButtonContext(context)) {
throw new Error(`<${consumer}/> must be rendered inside a styled .`);
}
return context;
}
////////////////////////////////////////////////////////////////////////////////////
/**
* @platform Lynx
*
* `@seed-design/lynx-react-field-button`의 상태·press·접근성 위에 SEED recipe, stroke,
* Content Scale과 Value/Placeholder/affix 표현을 조립합니다.
*
* 웹 대비 미지원 기능:
* - `size="responsive"`: Lynx에는 CSS viewport breakpoint가 없음
* - `name`·HiddenInput 등 HTML form 제출과 DOM ARIA id 연결
* - Content Scale을 적용하려면 Button을 Root의 직접 자식 또는 Fragment 안에 둡니다.
* 커스텀 컴포넌트로 감싼 Button은 기존 구조를 보존하며 Content Scale을 생략합니다.
*/
export interface FieldButtonRootProps
extends
Omit,
Pick,
Omit, keyof FieldButtonVariantProps | keyof UseFieldButtonProps> {}
export const FieldButtonRoot = React.forwardRef(
(props, forwardedRef) => {
const [variantProps, otherProps] = fieldButton.splitVariantProps(props);
const { children, className, values, onValuesChange, ...nativeProps } = otherProps;
const api = useFieldButton({
values,
onValuesChange,
disabled: variantProps.disabled,
invalid: variantProps.invalid,
readOnly: variantProps.readOnly,
});
const classes = fieldButton(variantProps);
// Button이 이 Main Thread touch handler를 눌림 상태 갱신과 합성해 Background로 넘긴다.
const { scaleFeedbackTriggerProps, scaleFeedbackTargetProps } = useScaleFeedback({
disabled: !api.interactive,
});
const childArray = flattenFieldButtonChildren(children);
const buttonChildren = childArray.filter(isFieldButtonButton);
const contentChildren = childArray.filter((child) => !isFieldButtonButton(child));
const contextValue = React.useMemo(
() => ({ ...api, variantProps, scaleFeedbackTriggerProps }),
[api, variantProps, scaleFeedbackTriggerProps],
);
return (
{buttonChildren.length > 0 ? (
<>
{buttonChildren}
{contentChildren}
>
) : (
children
)}
);
},
);
FieldButtonRoot.displayName = "FieldButtonRoot";
////////////////////////////////////////////////////////////////////////////////////
export interface FieldButtonButtonProps
extends Omit, keyof LynxPressableProps>, LynxPressableProps {}
export const FieldButtonButton = React.forwardRef((props, ref) => {
const context = useStyledFieldButtonContext("FieldButton.Button");
const {
children,
className,
bindtap,
"main-thread:bindtap": mainThreadBindtap,
"accessibility-element": accessibilityElement,
"accessibility-traits": accessibilityTraits,
...nativeProps
} = props;
// Scale Feedback의 Main Thread touch handler가 눌림 상태를 Background로 넘기므로
// Background touch handler는 바인딩하지 않는다.
const {
pressed,
buttonProps: {
bindtouchstart: _bindtouchstart,
bindtouchend: _bindtouchend,
bindtouchcancel: _bindtouchcancel,
...buttonProps
},
} = useFieldButtonButton({
bindtap,
"main-thread:bindtap": mainThreadBindtap,
"accessibility-element": accessibilityElement,
"accessibility-traits": accessibilityTraits,
...context.scaleFeedbackTriggerProps,
});
const classes = fieldButton({ ...context.variantProps, pressed });
return (
{children}
);
});
FieldButtonButton.displayName = "FieldButtonButton";
function isFieldButtonButton(node: React.ReactNode) {
return React.isValidElement(node) && node.type === FieldButtonButton;
}
function flattenFieldButtonChildren(children: React.ReactNode): React.ReactNode[] {
return toArray(children).flatMap((child) =>
React.isValidElement<{ children?: React.ReactNode }>(child) && child.type === React.Fragment
? flattenFieldButtonChildren(child.props.children)
: [child],
);
}
////////////////////////////////////////////////////////////////////////////////////
export interface FieldButtonValueProps extends LynxHostProps<"text"> {}
export const FieldButtonValue = React.forwardRef((props, ref) => {
const context = useStyledFieldButtonContext("FieldButton.Value");
const classes = fieldButton(context.variantProps);
const { children, className, ...nativeProps } = props;
return (
{children}
);
});
FieldButtonValue.displayName = "FieldButtonValue";
export interface FieldButtonPlaceholderProps extends LynxHostProps<"text"> {}
export const FieldButtonPlaceholder = React.forwardRef(
(props, ref) => {
const context = useStyledFieldButtonContext("FieldButton.Placeholder");
const classes = fieldButton(context.variantProps);
const { children, className, ...nativeProps } = props;
return (
{children}
);
},
);
FieldButtonPlaceholder.displayName = "FieldButtonPlaceholder";
////////////////////////////////////////////////////////////////////////////////////
export interface FieldButtonPrefixTextProps extends LynxHostProps<"text"> {}
export const FieldButtonPrefixText = React.forwardRef(
(props, ref) => {
const context = useStyledFieldButtonContext("FieldButton.PrefixText");
const classes = fieldButton(context.variantProps);
const { children, className, ...nativeProps } = props;
return (
{children}
);
},
);
FieldButtonPrefixText.displayName = "FieldButtonPrefixText";
export interface FieldButtonPrefixIconProps extends InternalIconProps {}
export const FieldButtonPrefixIcon = React.forwardRef(
(props, ref) => {
const context = useStyledFieldButtonContext("FieldButton.PrefixIcon");
const classes = fieldButton(context.variantProps);
const { className, ...otherProps } = props;
return (
);
},
);
FieldButtonPrefixIcon.displayName = "FieldButtonPrefixIcon";
export interface FieldButtonSuffixTextProps extends LynxHostProps<"text"> {}
export const FieldButtonSuffixText = React.forwardRef(
(props, ref) => {
const context = useStyledFieldButtonContext("FieldButton.SuffixText");
const classes = fieldButton(context.variantProps);
const { children, className, ...nativeProps } = props;
return (
{children}
);
},
);
FieldButtonSuffixText.displayName = "FieldButtonSuffixText";
export interface FieldButtonSuffixIconProps extends InternalIconProps {}
export const FieldButtonSuffixIcon = React.forwardRef(
(props, ref) => {
const context = useStyledFieldButtonContext("FieldButton.SuffixIcon");
const classes = fieldButton(context.variantProps);
const { className, ...otherProps } = props;
return (
);
},
);
FieldButtonSuffixIcon.displayName = "FieldButtonSuffixIcon";
////////////////////////////////////////////////////////////////////////////////////
export interface FieldButtonClearButtonProps extends InternalIconProps, LynxPressableProps {}
export const FieldButtonClearButton = React.forwardRef(
(props, ref) => {
const context = useStyledFieldButtonContext("FieldButton.ClearButton");
const {
className,
bindtap,
"main-thread:bindtap": mainThreadBindtap,
"accessibility-element": accessibilityElement,
"accessibility-label": accessibilityLabel,
"accessibility-traits": accessibilityTraits,
...otherProps
} = props;
const { scaleFeedbackTriggerProps, scaleFeedbackTargetProps } = useScaleFeedback({
disabled: !context.interactive,
});
const { pressed, rendered, clearButtonProps } = useFieldButtonClearButton({
bindtap,
"main-thread:bindtap": mainThreadBindtap,
"accessibility-element": accessibilityElement,
"accessibility-traits": accessibilityTraits,
});
const classes = fieldButton({ ...context.variantProps, pressed });
if (!rendered) return null;
if (process.env.NODE_ENV !== "production" && !accessibilityLabel) {
console.warn("FieldButton.ClearButton requires `accessibility-label` for accessibility.");
}
return (
);
},
);
FieldButtonClearButton.displayName = "FieldButtonClearButton";