-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(next): ssr collapsible field (#4894)
- Loading branch information
1 parent
64ff9d9
commit 5691760
Showing
17 changed files
with
274 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/ui/src/forms/field-types/Collapsible/Input/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use client' | ||
import React, { Fragment, useCallback, useState } from 'react' | ||
|
||
import type { DocumentPreferences } from 'payload/types' | ||
|
||
import { Collapsible } from '../../../../elements/Collapsible' | ||
import { ErrorPill } from '../../../../elements/ErrorPill' | ||
import { useDocumentInfo } from '../../../../providers/DocumentInfo' | ||
import { usePreferences } from '../../../../providers/Preferences' | ||
import { useTranslation } from '../../../..' | ||
import { WatchChildErrors } from '../../../WatchChildErrors' | ||
|
||
export const CollapsibleInput: React.FC<{ | ||
initCollapsed?: boolean | ||
children: React.ReactNode | ||
path: string | ||
baseClass: string | ||
RowLabel?: React.ReactNode | ||
fieldPreferencesKey?: string | ||
pathSegments?: string[] | ||
}> = (props) => { | ||
const { initCollapsed, children, path, baseClass, RowLabel, fieldPreferencesKey, pathSegments } = | ||
props | ||
|
||
const { getPreference, setPreference } = usePreferences() | ||
const { preferencesKey } = useDocumentInfo() | ||
const [errorCount, setErrorCount] = useState(0) | ||
const { i18n } = useTranslation() | ||
|
||
const onToggle = useCallback( | ||
async (newCollapsedState: boolean) => { | ||
const existingPreferences: DocumentPreferences = await getPreference(preferencesKey) | ||
|
||
setPreference(preferencesKey, { | ||
...existingPreferences, | ||
...(path | ||
? { | ||
fields: { | ||
...(existingPreferences?.fields || {}), | ||
[path]: { | ||
...existingPreferences?.fields?.[path], | ||
collapsed: newCollapsedState, | ||
}, | ||
}, | ||
} | ||
: { | ||
fields: { | ||
...(existingPreferences?.fields || {}), | ||
[fieldPreferencesKey]: { | ||
...existingPreferences?.fields?.[fieldPreferencesKey], | ||
collapsed: newCollapsedState, | ||
}, | ||
}, | ||
}), | ||
}) | ||
}, | ||
[preferencesKey, fieldPreferencesKey, getPreference, setPreference, path], | ||
) | ||
|
||
return ( | ||
<Fragment> | ||
<WatchChildErrors pathSegments={pathSegments} setErrorCount={setErrorCount} /> | ||
<Collapsible | ||
className={`${baseClass}__collapsible`} | ||
collapsibleStyle={errorCount > 0 ? 'error' : 'default'} | ||
header={ | ||
<div className={`${baseClass}__row-label-wrap`}> | ||
{RowLabel} | ||
{errorCount > 0 && <ErrorPill count={errorCount} withMessage i18n={i18n} />} | ||
</div> | ||
} | ||
initCollapsed={initCollapsed} | ||
onToggle={onToggle} | ||
> | ||
{children} | ||
</Collapsible> | ||
</Fragment> | ||
) | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/ui/src/forms/field-types/Collapsible/Wrapper/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use client' | ||
import React from 'react' | ||
|
||
import { fieldBaseClass } from '../../shared' | ||
import { useFormFields } from '../../../Form/context' | ||
|
||
const baseClass = 'collapsible-field' | ||
|
||
export const CollapsibleFieldWrapper: React.FC<{ | ||
className?: string | ||
path: string | ||
children: React.ReactNode | ||
id?: string | ||
}> = (props) => { | ||
const { children, className, path, id } = props | ||
|
||
const field = useFormFields(([fields]) => fields[path]) | ||
|
||
const { valid } = field || {} | ||
|
||
return ( | ||
<div | ||
className={[ | ||
fieldBaseClass, | ||
baseClass, | ||
className, | ||
!valid ? `${baseClass}--has-error` : `${baseClass}--has-no-error`, | ||
] | ||
.filter(Boolean) | ||
.join(' ')} | ||
id={id} | ||
> | ||
{children} | ||
</div> | ||
) | ||
} |
Oops, something went wrong.