{"version":3,"file":"FormSubmitFooter.mjs","names":["formMessages","Button","Flexbox"],"sources":["../../../src/Form/components/FormSubmitFooter.tsx"],"sourcesContent":["'use client';\n\nimport { Form } from 'antd';\nimport { cssVar, cx } from 'antd-style';\nimport isEqual from 'fast-deep-equal';\nimport { InfoIcon } from 'lucide-react';\nimport { memo, useEffect, useMemo, useState } from 'react';\n\nimport Button from '@/Button';\nimport { Flexbox } from '@/Flex';\nimport formMessages from '@/i18n/resources/en/form';\nimport { useTranslation } from '@/i18n/useTranslation';\nimport Icon from '@/Icon';\nimport { useMotionComponent } from '@/MotionProvider';\n\nimport { submitFooterStyles } from '../style';\nimport { type FormSubmitFooterProps } from '../type';\nimport { useFormContext } from './FormProvider';\nimport { merge, removeUndefined } from './merge';\n\nconst FormSubmitFooter = memo<FormSubmitFooterProps>(\n  ({\n    enableReset = true,\n    buttonProps,\n    float,\n    onReset,\n    saveButtonProps,\n    resetButtonProps,\n    enableUnsavedWarning,\n    children,\n    texts,\n    className,\n    ...rest\n  }) => {\n    const Motion = useMotionComponent();\n    const { form, initialValues, submitLoading } = useFormContext();\n    const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);\n    const values = Form.useWatch([], form) || {};\n    const { t } = useTranslation(formMessages);\n\n    const styles = submitFooterStyles;\n\n    const v = useMemo(() => removeUndefined(values), [values]);\n\n    const initialV = useMemo(() => removeUndefined(initialValues), [initialValues]);\n\n    const mergedV = useMemo(() => merge(initialV, v), [v, initialV]);\n\n    useEffect(() => {\n      setHasUnsavedChanges(!isEqual(mergedV, initialV));\n    }, [mergedV, initialV, submitLoading]);\n\n    const unsavedWarningText = texts?.unSavedWarning ?? t('form.unsavedWarning');\n    const unsavedText = texts?.unSaved ?? t('form.unsavedChanges');\n    const resetText = texts?.reset ?? t('form.reset');\n    const submitText = texts?.submit ?? t('form.submit');\n\n    const fn = (e: BeforeUnloadEvent) => {\n      if (hasUnsavedChanges) {\n        e.returnValue = unsavedWarningText;\n      } else {\n        delete e.returnValue;\n      }\n    };\n\n    useEffect(() => {\n      if (!enableUnsavedWarning) return;\n      if (typeof window === 'undefined' || !hasUnsavedChanges) return;\n      // 添加离开页面的提示\n      window.addEventListener('beforeunload', fn);\n      return () => window.removeEventListener('beforeunload', fn);\n    }, [enableUnsavedWarning, hasUnsavedChanges, unsavedWarningText]);\n\n    const content = (\n      <>\n        {(float || hasUnsavedChanges) && (\n          <>\n            <Icon\n              color={cssVar.colorTextDescription}\n              icon={InfoIcon}\n              size={12}\n              style={{ marginLeft: 8 }}\n            />\n            <span\n              style={{\n                color: cssVar.colorTextDescription,\n                flex: 'none',\n                fontSize: 12,\n                marginRight: float ? 16 : 4,\n              }}\n            >\n              {unsavedText}\n            </span>\n          </>\n        )}\n        {children}\n        {enableReset && (float || hasUnsavedChanges) && (\n          <Button\n            htmlType=\"button\"\n            shape={float ? 'round' : undefined}\n            variant={'filled'}\n            onClick={() => {\n              onReset?.(v, initialV);\n              form?.resetFields();\n            }}\n            {...buttonProps}\n            {...resetButtonProps}\n          >\n            {resetText}\n          </Button>\n        )}\n        <Button\n          htmlType=\"submit\"\n          loading={submitLoading}\n          shape={float ? 'round' : undefined}\n          type=\"primary\"\n          {...buttonProps}\n          {...saveButtonProps}\n        >\n          {submitText}\n        </Button>\n      </>\n    );\n\n    if (!float)\n      return (\n        <Flexbox\n          horizontal\n          align={'center'}\n          className={cx(styles.footer, className)}\n          gap={8}\n          justify={'flex-end'}\n          {...rest}\n        >\n          {content}\n        </Flexbox>\n      );\n\n    return (\n      <Motion.div\n        animate={hasUnsavedChanges ? 'visible' : 'hidden'}\n        className={styles.floatFooter}\n        initial={'hidden'}\n        transition={{ duration: 0.1, ease: 'easeOut' }}\n        variants={{\n          hidden: {\n            opacity: 0,\n            x: '-50%',\n            y: 20,\n          },\n          visible: {\n            opacity: 1,\n            x: '-50%',\n            y: 0,\n          },\n        }}\n      >\n        <Flexbox\n          horizontal\n          align={'center'}\n          className={className}\n          gap={8}\n          justify={'center'}\n          {...rest}\n        >\n          {content}\n        </Flexbox>\n      </Motion.div>\n    );\n  },\n);\n\nFormSubmitFooter.displayName = 'FormSubmitFooter';\n\nexport default FormSubmitFooter;\n"],"mappings":";;;;;;;;;;;;;;;;;AAoBA,MAAM,mBAAmB,MACtB,EACC,cAAc,MACd,aACA,OACA,SACA,iBACA,kBACA,sBACA,UACA,OACA,WACA,GAAG,WACC;CACJ,MAAM,SAAS,oBAAoB;CACnC,MAAM,EAAE,MAAM,eAAe,kBAAkB,gBAAgB;CAC/D,MAAM,CAAC,mBAAmB,wBAAwB,SAAS,MAAM;CACjE,MAAM,SAAS,KAAK,SAAS,EAAE,EAAE,KAAK,IAAI,EAAE;CAC5C,MAAM,EAAE,MAAM,eAAeA,aAAa;CAE1C,MAAM,SAAS;CAEf,MAAM,IAAI,cAAc,gBAAgB,OAAO,EAAE,CAAC,OAAO,CAAC;CAE1D,MAAM,WAAW,cAAc,gBAAgB,cAAc,EAAE,CAAC,cAAc,CAAC;CAE/E,MAAM,UAAU,cAAc,MAAM,UAAU,EAAE,EAAE,CAAC,GAAG,SAAS,CAAC;AAEhE,iBAAgB;AACd,uBAAqB,CAAC,QAAQ,SAAS,SAAS,CAAC;IAChD;EAAC;EAAS;EAAU;EAAc,CAAC;CAEtC,MAAM,qBAAqB,OAAO,kBAAkB,EAAE,sBAAsB;CAC5E,MAAM,cAAc,OAAO,WAAW,EAAE,sBAAsB;CAC9D,MAAM,YAAY,OAAO,SAAS,EAAE,aAAa;CACjD,MAAM,aAAa,OAAO,UAAU,EAAE,cAAc;CAEpD,MAAM,MAAM,MAAyB;AACnC,MAAI,kBACF,GAAE,cAAc;MAEhB,QAAO,EAAE;;AAIb,iBAAgB;AACd,MAAI,CAAC,qBAAsB;AAC3B,MAAI,OAAO,WAAW,eAAe,CAAC,kBAAmB;AAEzD,SAAO,iBAAiB,gBAAgB,GAAG;AAC3C,eAAa,OAAO,oBAAoB,gBAAgB,GAAG;IAC1D;EAAC;EAAsB;EAAmB;EAAmB,CAA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