Merge branch 'master' of github.com:payloadcms/payload
This commit is contained in:
@@ -241,9 +241,7 @@ const RenderBlocks = React.memo((props: RenderBlockProps) => {
|
||||
<Banner type="error">
|
||||
This field requires at least
|
||||
{' '}
|
||||
{minRows
|
||||
? `${minRows} ${labels.plural}`
|
||||
: `1 ${labels.singular}`}
|
||||
{`${minRows} ${minRows === 1 ? labels.singular : labels.plural}`}
|
||||
</Banner>
|
||||
)}
|
||||
{(rows.length === 0 && readOnly) && (
|
||||
|
||||
@@ -12,39 +12,124 @@ describe('sanitizeFields', () => {
|
||||
sanitizeFields(fields, []);
|
||||
}).toThrow(MissingFieldType);
|
||||
});
|
||||
it('should populate label if missing', () => {
|
||||
const fields = [{
|
||||
name: 'someCollection',
|
||||
type: 'text',
|
||||
}];
|
||||
const sanitizedField = sanitizeFields(fields, [])[0];
|
||||
expect(sanitizedField.name).toStrictEqual('someCollection');
|
||||
expect(sanitizedField.label).toStrictEqual('Some Collection');
|
||||
expect(sanitizedField.type).toStrictEqual('text');
|
||||
});
|
||||
it('should allow auto-label override', () => {
|
||||
const fields = [{
|
||||
name: 'someCollection',
|
||||
type: 'text',
|
||||
label: 'Do not label',
|
||||
}];
|
||||
const sanitizedField = sanitizeFields(fields, [])[0];
|
||||
expect(sanitizedField.name).toStrictEqual('someCollection');
|
||||
expect(sanitizedField.label).toStrictEqual('Do not label');
|
||||
expect(sanitizedField.type).toStrictEqual('text');
|
||||
});
|
||||
it('should allow label opt-out', () => {
|
||||
const fields = [{
|
||||
name: 'someCollection',
|
||||
type: 'text',
|
||||
label: false,
|
||||
}];
|
||||
const sanitizedField = sanitizeFields(fields, [])[0];
|
||||
expect(sanitizedField.name).toStrictEqual('someCollection');
|
||||
expect(sanitizedField.label).toStrictEqual(false);
|
||||
expect(sanitizedField.type).toStrictEqual('text');
|
||||
});
|
||||
|
||||
describe('auto-labeling', () => {
|
||||
it('should populate label if missing', () => {
|
||||
const fields = [{
|
||||
name: 'someField',
|
||||
type: 'text',
|
||||
}];
|
||||
const sanitizedField = sanitizeFields(fields, [])[0];
|
||||
expect(sanitizedField.name).toStrictEqual('someField');
|
||||
expect(sanitizedField.label).toStrictEqual('Some Field');
|
||||
expect(sanitizedField.type).toStrictEqual('text');
|
||||
});
|
||||
it('should allow auto-label override', () => {
|
||||
const fields = [{
|
||||
name: 'someField',
|
||||
type: 'text',
|
||||
label: 'Do not label',
|
||||
}];
|
||||
const sanitizedField = sanitizeFields(fields, [])[0];
|
||||
expect(sanitizedField.name).toStrictEqual('someField');
|
||||
expect(sanitizedField.label).toStrictEqual('Do not label');
|
||||
expect(sanitizedField.type).toStrictEqual('text');
|
||||
});
|
||||
|
||||
describe('opt-out', () => {
|
||||
it('should allow label opt-out', () => {
|
||||
const fields = [{
|
||||
name: 'someField',
|
||||
type: 'text',
|
||||
label: false,
|
||||
}];
|
||||
const sanitizedField = sanitizeFields(fields, [])[0];
|
||||
expect(sanitizedField.name).toStrictEqual('someField');
|
||||
expect(sanitizedField.label).toStrictEqual(false);
|
||||
expect(sanitizedField.type).toStrictEqual('text');
|
||||
});
|
||||
|
||||
it('should allow label opt-out for arrays', () => {
|
||||
const fields = [{
|
||||
name: 'items',
|
||||
type: 'array',
|
||||
label: false,
|
||||
fields: [
|
||||
{
|
||||
name: 'itemName',
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
}];
|
||||
const sanitizedField = sanitizeFields(fields, [])[0];
|
||||
expect(sanitizedField.name).toStrictEqual('items');
|
||||
expect(sanitizedField.label).toStrictEqual(false);
|
||||
expect(sanitizedField.type).toStrictEqual('array');
|
||||
expect(sanitizedField.labels).toBeUndefined();
|
||||
});
|
||||
it('should allow label opt-out for blocks', () => {
|
||||
const fields = [{
|
||||
name: 'noLabelBlock',
|
||||
type: 'blocks',
|
||||
label: false,
|
||||
blocks: [
|
||||
{
|
||||
slug: 'number',
|
||||
fields: [
|
||||
{
|
||||
name: 'testNumber',
|
||||
type: 'number',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}];
|
||||
const sanitizedField = sanitizeFields(fields, [])[0];
|
||||
expect(sanitizedField.name).toStrictEqual('noLabelBlock');
|
||||
expect(sanitizedField.label).toStrictEqual(false);
|
||||
expect(sanitizedField.type).toStrictEqual('blocks');
|
||||
expect(sanitizedField.labels).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should label arrays with plural and singular', () => {
|
||||
const fields = [{
|
||||
name: 'items',
|
||||
type: 'array',
|
||||
fields: [
|
||||
{
|
||||
name: 'itemName',
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
}];
|
||||
const sanitizedField = sanitizeFields(fields, [])[0];
|
||||
expect(sanitizedField.name).toStrictEqual('items');
|
||||
expect(sanitizedField.label).toStrictEqual('Items');
|
||||
expect(sanitizedField.type).toStrictEqual('array');
|
||||
expect(sanitizedField.labels).toMatchObject({ singular: 'Item', plural: 'Items' });
|
||||
});
|
||||
|
||||
it('should label blocks with plural and singular', () => {
|
||||
const fields = [{
|
||||
name: 'specialBlock',
|
||||
type: 'blocks',
|
||||
blocks: [
|
||||
{
|
||||
slug: 'number',
|
||||
fields: [{ name: 'testNumber', type: 'number' }],
|
||||
},
|
||||
],
|
||||
}];
|
||||
const sanitizedField = sanitizeFields(fields, [])[0];
|
||||
expect(sanitizedField.name).toStrictEqual('specialBlock');
|
||||
expect(sanitizedField.label).toStrictEqual('Special Block');
|
||||
expect(sanitizedField.type).toStrictEqual('blocks');
|
||||
expect(sanitizedField.labels).toMatchObject({ singular: 'Special Block', plural: 'Special Blocks' });
|
||||
expect(sanitizedField.blocks[0].fields[0].label).toStrictEqual('Test Number');
|
||||
});
|
||||
});
|
||||
|
||||
describe('relationships', () => {
|
||||
it('should not throw on valid relationship', () => {
|
||||
|
||||
@@ -24,7 +24,7 @@ const sanitizeFields = (fields, validRelationships: string[]) => {
|
||||
});
|
||||
}
|
||||
|
||||
if (field.type === 'blocks') {
|
||||
if ((field.type === 'blocks' || field.type === 'array') && field.label !== false) {
|
||||
field.labels = field.labels || formatLabels(field.name);
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,10 @@ export const text = baseField.keys({
|
||||
defaultValue: joi.string(),
|
||||
minLength: joi.number(),
|
||||
maxLength: joi.number(),
|
||||
admin: baseAdminFields.keys({
|
||||
placeholder: joi.string(),
|
||||
autoComplete: joi.string(),
|
||||
}),
|
||||
});
|
||||
|
||||
export const number = baseField.keys({
|
||||
@@ -62,6 +66,8 @@ export const number = baseField.keys({
|
||||
min: joi.number(),
|
||||
max: joi.number(),
|
||||
admin: baseAdminFields.keys({
|
||||
placeholder: joi.string(),
|
||||
autoComplete: joi.string(),
|
||||
step: joi.number(),
|
||||
}),
|
||||
});
|
||||
@@ -72,6 +78,10 @@ export const textarea = baseField.keys({
|
||||
defaultValue: joi.string(),
|
||||
minLength: joi.number(),
|
||||
maxLength: joi.number(),
|
||||
admin: baseAdminFields.keys({
|
||||
placeholder: joi.string(),
|
||||
rows: joi.number(),
|
||||
}),
|
||||
});
|
||||
|
||||
export const email = baseField.keys({
|
||||
@@ -80,6 +90,10 @@ export const email = baseField.keys({
|
||||
defaultValue: joi.string(),
|
||||
minLength: joi.number(),
|
||||
maxLength: joi.number(),
|
||||
admin: baseAdminFields.keys({
|
||||
placeholder: joi.string(),
|
||||
autoComplete: joi.string(),
|
||||
}),
|
||||
});
|
||||
|
||||
export const code = baseField.keys({
|
||||
@@ -181,7 +195,6 @@ export const blocks = baseField.keys({
|
||||
singular: joi.string(),
|
||||
plural: joi.string(),
|
||||
}),
|
||||
label: joi.string(),
|
||||
blocks: joi.array().items(
|
||||
joi.object({
|
||||
slug: joi.string().required(),
|
||||
@@ -202,6 +215,7 @@ export const richText = baseField.keys({
|
||||
name: joi.string().required(),
|
||||
defaultValue: joi.array().items(joi.object()),
|
||||
admin: baseAdminFields.keys({
|
||||
placeholder: joi.string(),
|
||||
elements: joi.array().items(
|
||||
joi.alternatives().try(
|
||||
joi.string(),
|
||||
@@ -232,6 +246,7 @@ export const date = baseField.keys({
|
||||
name: joi.string().required(),
|
||||
defaultValue: joi.string(),
|
||||
admin: baseAdminFields.keys({
|
||||
placeholder: joi.string(),
|
||||
date: joi.object({
|
||||
displayFormat: joi.string(),
|
||||
pickerAppearance: joi.string(),
|
||||
|
||||
Reference in New Issue
Block a user