Я хочу получить значение по умолчанию, которое является глобальной строкой, и показать его в качестве описания. ценить. Тогда вам не придется переходить на страницу глобальных строк, чтобы увидеть, что это такое.
Код: Выделить всё
export const CardInstructionTitle: Field = {
type: "row",
fields: [
{
name: "title",
type: "group",
fields: [
{
name: "default",
type: "checkbox",
label: "Use Default Instruction",
defaultValue: true,
admin: {
description: `The default value is: ${defaultValue} (it would be great if this worked) `,
},
},
{
name: "text",
type: "text",
admin: {
components: {
Field: Component,
},
},
localized: true,
},
],
},
],
};
Код: Выделить всё
export const SelectThePairsCard: Block = {
slug: "selectThePairs",
interfaceName: "SelectThePairsFields",
fields: [
CardInstructionTitle("selectThePairs"),
{
name: "wordPairs",
type: "array",
fields: [
{
type: "row",
fields: [
{
name: "firstWord",
type: "text",
},
{
name: "secondWord",
type: "text",
},
],
},
],
},
],
};
Код: Выделить всё
const getDefaultValue = async (defaultTitle) => {
let locale = "fr"
try {
const response = await fetch(`/payload/api/globals/cardInstructionTitles?locale=${locale}`);
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.statusText}`);
}
const data = await response.json();
const value = data[defaultTitle];
if (typeof value !== 'string') {
throw new Error(`Invalid default value for ${defaultTitle}`);
}
return value;
} catch (error) {
console.error(`Error fetching default value: ${error.message}`);
return `Error fetching value for ${defaultTitle}`;
}
};
Код: Выделить всё
export const CardInstructionTitle: (parentName: string) => Field = (parentName: string) => {
return {
type: "row",
fields: [
{
name: "title",
type: "group",
fields: [
{
name: "default",
type: "checkbox",
label: "Use Default Instruction",
defaultValue: true,
admin: {
description: getDefaultValue(parentName),
}
},
{
name: "text",
type: "text",
admin: {
components: {
Field: Component,
},
},
localized: true,
},
],
},
],
}
};
Код: Выделить всё
The types of 'admin.description' are incompatible between these types.
Type 'Promise' is not assignable to type 'Description | undefined'.
Type 'Promise' is not assignable to type 'Record'.
Index signature for type 'string' is missing in type 'Promise'.
I' Я новичок в Payload и TypeScript, поэтому буду очень признателен за любую помощь, спасибо.
Это версия Payload 2
Подробнее здесь: https://stackoverflow.com/questions/793 ... -as-the-de
Мобильная версия