Skip to content

Commit

Permalink
fix(FieldFormatters): correctly resolve formatter in editor preview
Browse files Browse the repository at this point in the history
  • Loading branch information
maxpatiiuk committed Jul 13, 2024
1 parent d8ef2a5 commit a95634c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { LocalizedString } from 'typesafe-i18n';

import { requireContext } from '../../../tests/helpers';
import { formatterToParser } from '../../../utils/parser/definitions';
import { fieldFormatterToParser } from '../../../utils/parser/definitions';
import type { IR, RA } from '../../../utils/types';
import { localized } from '../../../utils/types';
import { tables } from '../../DataModel/tables';
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('file names resolution test', () => {
: syncFieldFormat(
field,
value.toString(),
formatterToParser(field, formatter),
fieldFormatterToParser(field, formatter),
undefined,
true
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from 'react';

import { useParser } from '../../hooks/resource';
import { formsText } from '../../localization/forms';
import { resourcesText } from '../../localization/resources';
import { schemaText } from '../../localization/schema';
import { getValidationAttributes } from '../../utils/parser/definitions';
import {
fieldFormatterToParser,
getValidationAttributes,
} from '../../utils/parser/definitions';
import type { GetSet, RA } from '../../utils/types';
import { ErrorMessage } from '../Atoms';
import { Input, Label } from '../Atoms/Form';
Expand Down Expand Up @@ -164,7 +166,13 @@ function FieldFormatterPreviewField({
() => resolvedFormatter?.parse(value) !== undefined,
[value, resolvedFormatter]
);
const parser = useParser(field);
const parser = React.useMemo(
() =>
field === undefined || resolvedFormatter === undefined
? { type: 'text' as const }
: fieldFormatterToParser(field, resolvedFormatter),
[field, resolvedFormatter]
);

const validationAttributes = getValidationAttributes(parser);
return resolvedFormatter === undefined ? null : (
Expand All @@ -173,6 +181,7 @@ function FieldFormatterPreviewField({
isConforming ? '' : resourcesText.nonConformingInline()
}`}
<Input.Generic
type="text"
value={value}
onValueChange={setValue}
{...validationAttributes}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { Parser } from '../definitions';
import {
browserifyRegex,
formatter,
formatterToParser,
fieldFormatterToParser,
getValidationAttributes,
lengthToRegex,
mergeParsers,
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('resolveParser', () => {
...parserFromType('java.lang.String'),
required: false,
...removeKey(
formatterToParser(field, uiFormatter),
fieldFormatterToParser(field, uiFormatter),
'formatters',
'parser',
'validators'
Expand Down Expand Up @@ -237,7 +237,7 @@ describe('formatterToParser', () => {
validators,
parser: parserFunction,
...parser
} = formatterToParser({}, uiFormatter);
} = fieldFormatterToParser({}, uiFormatter);
expect(parser).toEqual({
pattern: uiFormatter.regex,
title,
Expand Down Expand Up @@ -269,7 +269,7 @@ describe('formatterToParser', () => {
userPreferences.set('form', 'preferences', 'autoNumbering', {
CollectionObject: [],
});
expect(formatterToParser(field, uiFormatter).value).toBeUndefined();
expect(fieldFormatterToParser(field, uiFormatter).value).toBeUndefined();
});
});

Expand Down
4 changes: 2 additions & 2 deletions specifyweb/frontend/js_src/lib/utils/parser/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export function resolveParser(
? undefined
: (fullField as LiteralField).whiteSpaceSensitive,
...(typeof formatter === 'object'
? formatterToParser(field, formatter)
? fieldFormatterToParser(field, formatter)
: {}),
});
}
Expand Down Expand Up @@ -361,7 +361,7 @@ function resolveDate(
return callback(...(values as RA<number | undefined>));
}

export function formatterToParser(
export function fieldFormatterToParser(
field: Partial<LiteralField | Relationship>,
formatter: UiFormatter
): Parser {
Expand Down

0 comments on commit a95634c

Please sign in to comment.