Compare commits

..

1 Commits

Author SHA1 Message Date
Elliot DeNolf
e3ef443217 chore(release): db-postgres/0.1.2 2023-10-10 15:57:51 -04:00
17 changed files with 45 additions and 105 deletions

View File

@@ -347,7 +347,7 @@ The `useForm` hook returns an object with the following properties: |
value: <strong><code>rowIndex</code></strong>,
},
{
value: "The index of the row to add. If omitted, the row will be added to the end of the array.",
value: "The index of the row to add",
},
],
[

View File

@@ -1,6 +1,6 @@
{
"name": "@payloadcms/db-mongodb",
"version": "1.0.3",
"version": "1.0.2",
"description": "The officially supported MongoDB database adapter for Payload",
"repository": "https://github.com/payloadcms/payload",
"license": "MIT",

View File

@@ -68,9 +68,7 @@ export type MongooseAdapter = BaseDatabaseAdapter &
type MongooseAdapterResult = (args: { payload: Payload }) => MongooseAdapter
declare module 'payload' {
export interface DatabaseAdapter
extends Omit<BaseDatabaseAdapter, 'sessions'>,
Omit<Args, 'migrationDir'> {
export interface DatabaseAdapter extends Args {
collections: {
[slug: string]: CollectionModel
}

View File

@@ -1,6 +1,6 @@
{
"name": "@payloadcms/db-postgres",
"version": "0.1.3",
"version": "0.1.2",
"description": "The officially supported Postgres database adapter for Payload",
"repository": "https://github.com/payloadcms/payload",
"license": "MIT",

View File

@@ -69,9 +69,7 @@ export type MigrateUpArgs = { payload: Payload }
export type MigrateDownArgs = { payload: Payload }
declare module 'payload' {
export interface DatabaseAdapter
extends Omit<Args, 'migrationDir' | 'pool'>,
BaseDatabaseAdapter {
export interface DatabaseAdapter extends Omit<Args, 'pool'> {
drizzle: DrizzleDB
enums: Record<string, GenericEnum>
pool: Pool

View File

@@ -66,7 +66,7 @@ export const ArrayAction: React.FC<Props> = ({
<PopupList.Button
className={`${baseClass}__action ${baseClass}__add`}
onClick={() => {
addRow(index + 1)
addRow(index)
close()
}}
>

View File

@@ -132,9 +132,7 @@ export function fieldReducer(state: Fields, action: FieldAction): Fields {
}
case 'ADD_ROW': {
const { blockType, path, rowIndex: rowIndexFromArgs, subFieldState } = action
const rowIndex =
typeof rowIndexFromArgs === 'number' ? rowIndexFromArgs : state[path]?.rows?.length || 0
const { blockType, path, rowIndex, subFieldState } = action
const rowsMetadata = [...(state[path]?.rows || [])]
rowsMetadata.splice(
@@ -157,22 +155,19 @@ export function fieldReducer(state: Fields, action: FieldAction): Fields {
}
}
// add new row to array _field state_
const { remainingFields, rows: siblingRows } = separateRows(path, state)
siblingRows.splice(rowIndex, 0, subFieldState)
const { remainingFields, rows } = separateRows(path, state)
// add new row to array _value_
const currentValue = (Array.isArray(state[path]?.value) ? state[path]?.value : []) as Fields[]
const newValue = currentValue.splice(rowIndex, 0, reduceFieldsToValues(subFieldState, true))
// actual form state (value saved in db)
rows.splice(rowIndex, 0, subFieldState)
const newState: Fields = {
...remainingFields,
...flattenRows(path, siblingRows),
...flattenRows(path, rows),
[path]: {
...state[path],
disableFormData: true,
rows: rowsMetadata,
value: newValue,
value: rows,
},
}
@@ -181,8 +176,8 @@ export function fieldReducer(state: Fields, action: FieldAction): Fields {
case 'REPLACE_ROW': {
const { blockType, path, rowIndex: rowIndexArg, subFieldState } = action
const { remainingFields, rows: siblingRows } = separateRows(path, state)
const rowIndex = Math.max(0, Math.min(rowIndexArg, siblingRows?.length - 1 || 0))
const { remainingFields, rows } = separateRows(path, state)
const rowIndex = Math.max(0, Math.min(rowIndexArg, rows?.length - 1 || 0))
const rowsMetadata = [...(state[path]?.rows || [])]
rowsMetadata[rowIndex] = {
@@ -200,21 +195,17 @@ export function fieldReducer(state: Fields, action: FieldAction): Fields {
}
}
// replace form _field state_
siblingRows[rowIndex] = subFieldState
// replace array _value_
const newValue = Array.isArray(state[path]?.value) ? state[path]?.value : []
newValue[rowIndex] = reduceFieldsToValues(subFieldState, true)
// replace form field state
rows[rowIndex] = subFieldState
const newState: Fields = {
...remainingFields,
...flattenRows(path, siblingRows),
...flattenRows(path, rows),
[path]: {
...state[path],
disableFormData: true,
rows: rowsMetadata,
value: newValue,
value: rows,
},
}

View File

@@ -431,7 +431,7 @@ const Form: React.FC<Props> = (props) => {
[],
)
const getRowSchemaByPath = React.useCallback(
const getRowConfigByPath = React.useCallback(
({ blockType, path }: { blockType?: string; path: string }) => {
const rowConfig = traverseRowConfigs({
fieldConfig: collection?.fields || global?.fields,
@@ -449,24 +449,23 @@ const Form: React.FC<Props> = (props) => {
const addFieldRow: Context['addFieldRow'] = useCallback(
async ({ data, path, rowIndex }) => {
const preferences = await getDocPreferences()
const rowSchema = getRowSchemaByPath({
const fieldConfig = getRowConfigByPath({
blockType: data?.blockType,
path,
})
if (rowSchema) {
if (fieldConfig) {
const subFieldState = await buildStateFromSchema({
id,
config,
data,
fieldSchema: rowSchema,
fieldSchema: fieldConfig,
locale,
operation,
preferences,
t,
user,
})
dispatchFields({
blockType: data?.blockType,
path,
@@ -476,11 +475,11 @@ const Form: React.FC<Props> = (props) => {
})
}
},
[dispatchFields, getDocPreferences, id, user, operation, locale, t, getRowSchemaByPath, config],
[dispatchFields, getDocPreferences, id, user, operation, locale, t, getRowConfigByPath, config],
)
const removeFieldRow: Context['removeFieldRow'] = useCallback(
({ path, rowIndex }) => {
async ({ path, rowIndex }) => {
dispatchFields({ path, rowIndex, type: 'REMOVE_ROW' })
},
[dispatchFields],
@@ -489,17 +488,17 @@ const Form: React.FC<Props> = (props) => {
const replaceFieldRow: Context['replaceFieldRow'] = useCallback(
async ({ data, path, rowIndex }) => {
const preferences = await getDocPreferences()
const rowSchema = getRowSchemaByPath({
const fieldConfig = getRowConfigByPath({
blockType: data?.blockType,
path,
})
if (rowSchema) {
if (fieldConfig) {
const subFieldState = await buildStateFromSchema({
id,
config,
data,
fieldSchema: rowSchema,
fieldSchema: fieldConfig,
locale,
operation,
preferences,
@@ -515,7 +514,7 @@ const Form: React.FC<Props> = (props) => {
})
}
},
[dispatchFields, getDocPreferences, id, user, operation, locale, t, getRowSchemaByPath, config],
[dispatchFields, getDocPreferences, id, user, operation, locale, t, getRowConfigByPath, config],
)
const getFields = useCallback(() => contextRef.current.fields, [contextRef])

View File

@@ -12,9 +12,9 @@ export const separateRows = (path: string, fields: Fields): Result => {
const newRows = incomingRows
if (fieldPath.indexOf(`${path}.`) === 0) {
const [rowIndex] = fieldPath.replace(`${path}.`, '').split('.')
if (!newRows[rowIndex]) newRows[rowIndex] = {}
newRows[rowIndex][fieldPath.replace(`${path}.${String(rowIndex)}.`, '')] = { ...field }
const index = Number(fieldPath.replace(`${path}.`, '').split('.')[0])
if (!newRows[index]) newRows[index] = {}
newRows[index][fieldPath.replace(`${path}.${String(index)}.`, '')] = { ...field }
} else {
remainingFields[fieldPath] = field
}

View File

@@ -174,10 +174,7 @@ export type Context = {
}: {
data?: Data
path: string
/*
* by default the new row will be added to the end of the list
*/
rowIndex?: number
rowIndex: number
}) => Promise<void>
buildRowErrors: () => void
createFormData: CreateFormData
@@ -193,7 +190,7 @@ export type Context = {
getField: GetField
getFields: GetFields
getSiblingData: GetSiblingData
removeFieldRow: ({ path, rowIndex }: { path: string; rowIndex: number }) => void
removeFieldRow: ({ path, rowIndex }: { path: string; rowIndex: number }) => Promise<void>
replaceFieldRow: ({
data,
path,

View File

@@ -5,9 +5,9 @@ import type { Props } from './types'
import { createNestedFieldPath } from '../../Form/createNestedFieldPath'
import RenderFields from '../../RenderFields'
import withCondition from '../../withCondition'
import { fieldBaseClass } from '../shared'
import './index.scss'
import { RowProvider } from './provider'
import { fieldBaseClass } from '../shared'
const Row: React.FC<Props> = (props) => {
const {
@@ -29,6 +29,7 @@ const Row: React.FC<Props> = (props) => {
}))}
fieldTypes={fieldTypes}
indexPath={indexPath}
margins={false}
permissions={permissions}
readOnly={readOnly}
/>

View File

@@ -1,6 +1,6 @@
{
"name": "@payloadcms/richtext-slate",
"version": "1.0.2",
"version": "1.0.1",
"description": "The officially supported Slate richtext adapter for Payload",
"repository": "https://github.com/payloadcms/payload",
"license": "MIT",

View File

@@ -10,6 +10,8 @@ export const AddCustomBlocks: React.FC = () => {
const { addFieldRow, replaceFieldRow } = useForm()
const { value } = useField({ path: 'customBlocks' })
const nextIndex = Array.isArray(value) ? value.length : 0
return (
<div className={baseClass}>
<div className={`${baseClass}__blocks-grid`}>
@@ -19,6 +21,7 @@ export const AddCustomBlocks: React.FC = () => {
addFieldRow({
data: { block1Title: 'Block 1: Prefilled Title', blockType: 'block-1' },
path: 'customBlocks',
rowIndex: nextIndex,
})
}
type="button"
@@ -32,6 +35,7 @@ export const AddCustomBlocks: React.FC = () => {
addFieldRow({
data: { block2Title: 'Block 2: Prefilled Title', blockType: 'block-2' },
path: 'customBlocks',
rowIndex: nextIndex,
})
}
type="button"
@@ -47,12 +51,12 @@ export const AddCustomBlocks: React.FC = () => {
replaceFieldRow({
data: { block1Title: 'REPLACED BLOCK', blockType: 'block-1' },
path: 'customBlocks',
rowIndex: (Array.isArray(value) ? value.length : 0) - 1,
rowIndex: nextIndex - 1,
})
}
type="button"
>
Replace Block {Array.isArray(value) ? value.length : 0}
Replace Block {nextIndex}
</button>
</div>
</div>

View File

@@ -1,7 +1,8 @@
import type { CollectionConfig } from '../../../../packages/payload/src/collections/config/types'
import { CollapsibleLabelComponent } from './LabelComponent'
import { collapsibleFieldsSlug } from './shared'
export const collapsibleFieldsSlug = 'collapsible-fields'
const CollapsibleFields: CollectionConfig = {
slug: collapsibleFieldsSlug,

View File

@@ -1 +0,0 @@
export const collapsibleFieldsSlug = 'collapsible-fields'

View File

@@ -9,7 +9,7 @@ import wait from '../../packages/payload/src/utilities/wait'
import { saveDocAndAssert, saveDocHotkeyAndAssert } from '../helpers'
import { AdminUrlUtil } from '../helpers/adminUrlUtil'
import { initPayloadE2E } from '../helpers/configHelpers'
import { collapsibleFieldsSlug } from './collections/Collapsible/shared'
import { collapsibleFieldsSlug } from './collections/Collapsible'
import { jsonDoc } from './collections/JSON'
import { numberDoc } from './collections/Number'
import { pointFieldsSlug } from './collections/Point'

View File

@@ -1,48 +0,0 @@
import type { Page } from '@playwright/test'
import { expect, test } from '@playwright/test'
import { saveDocAndAssert } from '../helpers'
import { AdminUrlUtil } from '../helpers/adminUrlUtil'
import { initPayloadTest } from '../helpers/configHelpers'
const { beforeAll, describe } = test
let url: AdminUrlUtil
const slug = 'nested-fields'
let page: Page
describe('Nested Fields', () => {
beforeAll(async ({ browser }) => {
const { serverURL } = await initPayloadTest({
__dirname,
init: {
local: false,
},
})
url = new AdminUrlUtil(serverURL, slug)
const context = await browser.newContext()
page = await context.newPage()
})
test('should save deeply nested fields', async () => {
const assertionValue = 'sample block value'
await page.goto(url.create)
await page.locator('#field-array > button').click()
await page.locator('#field-array__0__group__namedTab__blocks > button').click()
await page.locator('button[title="Block With Field"]').click()
await page.locator('#field-array__0__group__namedTab__blocks__0__text').fill(assertionValue)
await saveDocAndAssert(page)
await expect(page.locator('#field-array__0__group__namedTab__blocks__0__text')).toHaveValue(
assertionValue,
)
})
})