import * as React from 'react'; import { Platform, useWindowDimensions, View } from 'react-native'; import { useConfigContext } from '../../config'; import { ErrorCode, UIKitError } from '../../error'; import { useColors, useGetStyleProps } from '../../hook'; import { usePaletteContext, useThemeContext } from '../../theme'; import { SlideModal, SlideModalRef } from '../Modal'; import { Text } from '../Text'; import { TextInput } from '../TextInput'; import { useAlert } from './Alert.hooks'; import type { AlertProps, AlertRef } from './types'; export const Alert = React.forwardRef( (props: AlertProps, ref?: React.ForwardedRef) => { const { containerStyle } = props; const modalRef = React.useRef({} as any); const { width: winWidth } = useWindowDimensions(); const { style: themeStyle, cornerRadius: corner } = useThemeContext(); const { colors, cornerRadius } = usePaletteContext(); const { getBorderRadius } = useGetStyleProps(); const { fontFamily } = useConfigContext(); const { getColor } = useColors({ t2: { light: colors.neutral[7], dark: colors.neutral[4], }, t3: { light: colors.neutral[5], dark: colors.neutral[6], }, }); const isShow = React.useRef(false); const onRequestModalClose = React.useCallback(() => { modalRef?.current?.startHide?.(); }, []); const { props: updatedProps, getButton, onUpdate, value, onChangeText, setTextCount, textCount, } = useAlert(props); const { buttons, message, title, supportInput = false, supportInputStatistics, inputMaxCount, isSaveInput = true, enableClearButton = false, autoFocus, inputProps, } = updatedProps; const count = buttons?.length ?? 1; if (count > 3) { throw new UIKitError({ code: ErrorCode.max_count, desc: 'Alert buttons count must less than 3', }); } React.useImperativeHandle(ref, () => { return { alert: () => { isShow.current = true; modalRef?.current?.startShow?.(); }, alertWithInit: (props: AlertProps) => { isShow.current = true; onUpdate(props); }, close: (onFinished) => { isShow.current = false; if (isSaveInput === false) { onChangeText?.(''); } modalRef?.current?.startHide?.(onFinished); }, }; }, [isSaveInput, onChangeText, onUpdate]); React.useEffect(() => { if (isShow.current === true) { modalRef?.current?.startShow?.(); } }, [updatedProps]); return ( {title} {message ? ( <> {message} ) : null} {supportInput === true ? ( ) : null} {supportInput === true ? : null} {getButton(buttons, onRequestModalClose)} ); } );