{"version":3,"file":"index.js","names":["c","_c","getTranslation","getFieldPaths","toKebabCase","React","useCallback","useEffect","useState","useCollapsible","RenderCustomComponent","useFormFields","RenderFields","useField","withCondition","useDocumentInfo","usePreferences","useTranslation","FieldDescription","fieldBaseClass","TabsProvider","TabComponent","baseClass","TabsFieldComponent","props","$","field","t0","forceRender","t1","indexPath","t2","parentPath","t3","parentSchemaPath","t4","path","t5","permissions","readOnly","schemaPath","t6","admin","t7","tabs","t8","t9","undefined","className","t10","getPreference","setPreference","preferencesKey","i18n","isWithinCollapsible","t11","t12","fields","map","tab","index","id","passesCondition","tabStates","filter","_temp","activeTabIndex","setActiveTabIndex","tabsPrefKey","activeTabInfo","activeTabConfig","activeTabDescription","description","activeTabStaticDescription","t","hasVisibleTabs","some","_temp2","t13","incomingTabIndex","existingPreferences","tabIndex","handleTabChange","t14","length","getInitialPref","existingPreferences_0","initialIndex","newIndex","t15","t16","nextTab","find","_temp3","t17","t18","t19","t20","Boolean","t21","t22","index_0","passesCondition_2","tab_0","_jsx","hidden","isActive","setIsActive","join","children","_jsxs","TabContent","parentIndexPath","name","TabsField","label","customComponents","Field","bb0","AfterInput","BeforeInput","Description","CustomComponent","Fallback","marginPlacement","passesCondition_0","passesCondition_1"],"sources":["../../../src/fields/Tabs/index.tsx"],"sourcesContent":["'use client'\nimport type {\n ClientComponentProps,\n ClientTab,\n DocumentPreferences,\n SanitizedFieldPermissions,\n StaticDescription,\n TabsFieldClientComponent,\n} from 'payload'\n\nimport { getTranslation } from '@payloadcms/translations'\nimport { getFieldPaths, toKebabCase } from 'payload/shared'\nimport React, { useCallback, useEffect, useState } from 'react'\n\nimport { useCollapsible } from '../../elements/Collapsible/provider.js'\nimport { RenderCustomComponent } from '../../elements/RenderCustomComponent/index.js'\nimport { useFormFields } from '../../forms/Form/index.js'\nimport { RenderFields } from '../../forms/RenderFields/index.js'\nimport { useField } from '../../forms/useField/index.js'\nimport { withCondition } from '../../forms/withCondition/index.js'\nimport { useDocumentInfo } from '../../providers/DocumentInfo/index.js'\nimport { usePreferences } from '../../providers/Preferences/index.js'\nimport { useTranslation } from '../../providers/Translation/index.js'\nimport { FieldDescription } from '../FieldDescription/index.js'\nimport { fieldBaseClass } from '../shared/index.js'\nimport { TabsProvider } from './provider.js'\nimport { TabComponent } from './Tab/index.js'\nimport './index.scss'\n\nconst baseClass = 'tabs-field'\n\nexport { TabsProvider }\n\nconst TabsFieldComponent: TabsFieldClientComponent = (props) => {\n const {\n field: { admin: { className } = {}, tabs = [] },\n forceRender = false,\n indexPath = '',\n parentPath = '',\n parentSchemaPath = '',\n path = '',\n permissions,\n readOnly,\n schemaPath = '',\n } = props\n\n const { getPreference, setPreference } = usePreferences()\n const { preferencesKey } = useDocumentInfo()\n const { i18n } = useTranslation()\n const { isWithinCollapsible } = useCollapsible()\n\n const tabStates = useFormFields(([fields]) => {\n return tabs.map((tab, index) => {\n const id = tab?.id\n\n return {\n index,\n passesCondition: fields?.[id]?.passesCondition ?? true,\n tab,\n }\n })\n })\n\n const [activeTabIndex, setActiveTabIndex] = useState(\n () => tabStates.filter(({ passesCondition }) => passesCondition)?.[0]?.index ?? 0,\n )\n\n const tabsPrefKey = `tabs-${indexPath}`\n\n const activeTabInfo = tabStates[activeTabIndex]\n const activeTabConfig = activeTabInfo?.tab\n const activeTabDescription = activeTabConfig?.admin?.description ?? activeTabConfig?.description\n\n const activeTabStaticDescription =\n typeof activeTabDescription === 'function'\n ? activeTabDescription({ i18n, t: i18n.t })\n : activeTabDescription\n\n const hasVisibleTabs = tabStates.some(({ passesCondition }) => passesCondition)\n\n const handleTabChange = useCallback(\n async (incomingTabIndex: number): Promise => {\n setActiveTabIndex(incomingTabIndex)\n\n const existingPreferences: DocumentPreferences = await getPreference(preferencesKey)\n\n if (preferencesKey) {\n void setPreference(preferencesKey, {\n ...existingPreferences,\n ...(path\n ? {\n fields: {\n ...(existingPreferences?.fields || {}),\n [path]: {\n ...existingPreferences?.fields?.[path],\n tabIndex: incomingTabIndex,\n },\n },\n }\n : {\n fields: {\n ...existingPreferences?.fields,\n [tabsPrefKey]: {\n ...existingPreferences?.fields?.[tabsPrefKey],\n tabIndex: incomingTabIndex,\n },\n },\n }),\n })\n }\n },\n [getPreference, preferencesKey, setPreference, path, tabsPrefKey],\n )\n\n useEffect(() => {\n if (preferencesKey) {\n const getInitialPref = async () => {\n const existingPreferences: DocumentPreferences = await getPreference(preferencesKey)\n const initialIndex = path\n ? existingPreferences?.fields?.[path]?.tabIndex\n : existingPreferences?.fields?.[tabsPrefKey]?.tabIndex\n\n const newIndex = typeof initialIndex === 'number' && initialIndex < tabStates.length ? initialIndex : 0\n setActiveTabIndex(newIndex)\n }\n void getInitialPref()\n }\n }, [path, getPreference, preferencesKey, tabsPrefKey, tabs, parentPath, parentSchemaPath])\n\n useEffect(() => {\n if (activeTabInfo?.passesCondition === false) {\n const nextTab = tabStates.find(({ passesCondition }) => passesCondition)\n if (nextTab) {\n void handleTabChange(nextTab.index)\n }\n }\n }, [activeTabInfo, tabStates, handleTabChange])\n\n return (\n \n \n
\n
\n {tabStates.map(({ index, passesCondition, tab }) => (\n
\n
\n
\n {activeTabConfig && (\n
\n
\n \n )\n}\n\nexport const TabsField = withCondition(TabsFieldComponent)\n\ntype ActiveTabProps = {\n readonly description: StaticDescription\n readonly field: ClientTab\n readonly hidden: boolean\n readonly label?: string\n readonly parentIndexPath: string\n readonly parentPath: string\n readonly parentSchemaPath: string\n readonly path: string\n readonly permissions: SanitizedFieldPermissions\n readonly readOnly: boolean\n readonly tabIndex: number\n} & Pick\n\nfunction TabContent({\n description,\n field,\n forceRender,\n hidden,\n label,\n parentIndexPath,\n parentPath,\n parentSchemaPath,\n permissions,\n readOnly,\n tabIndex,\n}: ActiveTabProps) {\n const { i18n } = useTranslation()\n\n const { customComponents: { AfterInput, BeforeInput, Description, Field } = {} } = useField()\n\n if (Field) {\n return Field\n }\n\n const { indexPath, path, schemaPath } = getFieldPaths({\n field,\n index: tabIndex,\n parentIndexPath,\n parentPath,\n parentSchemaPath,\n })\n\n return (\n