import type { ViewProps } from '@tamagui/core' import { View, createStyledContext, styled } from '@tamagui/core' import { composeEventHandlers, withStaticProperties } from '@tamagui/helpers' const FORM_NAME = 'Form' export const FormFrame = styled(View, { name: FORM_NAME, render: 'form', }) /* ------------------------------------------------------------------------------------------------- * Context * -----------------------------------------------------------------------------------------------*/ type FormContextValue = { onSubmit?: () => unknown } export const FormContext = createStyledContext({ onSubmit: undefined, } as FormContextValue) export const { useStyledContext: useFormContext, Provider: FormProvider } = FormContext type FormExtraProps = { scope?: string onSubmit?: () => void } export type FormProps = ViewProps & FormExtraProps /* ------------------------------------------------------------------------------------------------- * FormTrigger * -----------------------------------------------------------------------------------------------*/ const FormTriggerFrame = styled(View, { name: 'FormTrigger', }) export interface FormTriggerProps extends ViewProps { scope?: string } export const FormTrigger = FormTriggerFrame.styleable( (props, forwardedRef) => { const { scope, children, onPress, ...triggerProps } = props const context = useFormContext(scope) return ( {children} ) } ) /* ------------------------------------------------------------------------------------------------- * Form * -----------------------------------------------------------------------------------------------*/ const FormComponent = FormFrame.styleable(function Form( { scope, onSubmit, ...props }, ref ) { return ( { e.preventDefault() onSubmit?.() }} /> ) }) export const Form = withStaticProperties(FormComponent, { Trigger: FormTrigger, })