/**
* Copyright (c) Paymium.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root of this projects source tree.
*/
import {
useRef,
forwardRef,
ComponentType,
isValidElement,
PropsWithChildren,
Children,
cloneElement,
useState,
} from 'react';
import { InputProvider, States, useInputContext } from './context';
import { composeEventHandlers } from '@crossed/core';
export const createInputGroup =
(Styled: ComponentType
) =>
forwardRef((props, ref) => {
const [states, setStates] = useState({
isActive: false,
isFocus: false,
isHover: false,
});
const inputRef = useRef(null);
return (
) =>
setStates((old) => ({ ...old, ...style }))
}
inputRef={inputRef}
>
);
});
const Slot = ({ children, ...props }: PropsWithChildren) => {
const { setStates, states, inputRef } = useInputContext();
return isValidElement(children) && Children.count(children) === 1
? cloneElement(children, {
...props,
states,
onPointerEnter: composeEventHandlers(props.onPointerEnter, () => {
setStates({ isHover: true });
}),
onPointerLeave: composeEventHandlers(props.onPointerLeave, () => {
setStates({ isHover: false });
}),
onPress: composeEventHandlers(() => {
inputRef.current?.focus();
}, props.onPress),
} as any)
: children;
};