Skip to content

Commit

Permalink
fix: Object examples not properly displayed as placeholders (#4126)
Browse files Browse the repository at this point in the history
* stringifying object examples

* adding test

* removing format message

Co-authored-by: Andy Brown <asbrown002@gmail.com>
  • Loading branch information
LouisEugeneMSFT and a-b-r-o-w-n authored Sep 24, 2020
1 parent eb9e10a commit ad90706
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { FieldProps, UIOptions } from '@bfc/extension-client';

import { getUiLabel, getUiDescription, getUiPlaceholder } from '../uiOptionsHelpers';
import { getUiDescription, getUiLabel, getUiPlaceholder } from '../uiOptionsHelpers';

let props;

Expand Down Expand Up @@ -90,4 +90,10 @@ describe('getUiPlaceholder', () => {
'ex. one, two'
);
});

it('correctly display examples for non string types', () => {
expect(
getUiPlaceholder({ ...props, placeholder: undefined, schema: { examples: [true, 5, { arg1: 'test' }] } })
).toEqual('ex. true, 5, {"arg1":"test"}');
});
});
9 changes: 7 additions & 2 deletions Composer/packages/adaptive-form/src/utils/uiOptionsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import { FieldProps } from '@bfc/extension-client';
import startCase from 'lodash/startCase';
import formatMessage from 'format-message';

export function getUiLabel(props: FieldProps): string | false | undefined {
const { uiOptions, schema, name, value, label } = props;
Expand Down Expand Up @@ -47,7 +46,13 @@ export function getUiPlaceholder(props: FieldProps): string | undefined {
} else if (placeholder) {
fieldUIPlaceholder = placeholder;
} else if (schema && Array.isArray(schema.examples) && schema.examples.length > 0) {
fieldUIPlaceholder = formatMessage('ex. { example }', { example: schema.examples.join(', ') });
const examplesStrings = schema.examples.map((example) => {
if (typeof example === 'object') {
return JSON.stringify(example);
}
return example;
});
fieldUIPlaceholder = `ex. ${examplesStrings.join(', ')}`;
}

if (fieldUIPlaceholder && schema.pattern) {
Expand Down

0 comments on commit ad90706

Please sign in to comment.