Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle field placeholder when labels disabled #426

Merged
merged 5 commits into from
May 19, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion packages/uniforms/__tests__/GraphQLBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ describe('GraphQLBridge', () => {
author: {component: 'div'},
id: {
allowedValues: [1, 2, 3],
label: 'Post ID'
label: 'Post ID',
placeholder: 'Post ID'
},
title: {
initialValue: 'Some Title',
Expand Down Expand Up @@ -199,6 +200,7 @@ describe('GraphQLBridge', () => {
it('works with allowedValues', () => {
expect(bridgeI.getProps('id')).toEqual({
label: 'Post ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3]
});
Expand All @@ -207,6 +209,7 @@ describe('GraphQLBridge', () => {
it('works with allowedValues from props', () => {
expect(bridgeI.getProps('id', {allowedValues: [1]})).toEqual({
label: 'Post ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1]
});
Expand All @@ -223,6 +226,7 @@ describe('GraphQLBridge', () => {
it('works with label (custom)', () => {
expect(bridgeI.getProps('id', {label: 'ID'})).toEqual({
label: 'ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3]
});
Expand All @@ -231,6 +235,7 @@ describe('GraphQLBridge', () => {
it('works with label (true)', () => {
expect(bridgeI.getProps('id', {label: true})).toEqual({
label: 'Post ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3]
});
Expand All @@ -239,6 +244,34 @@ describe('GraphQLBridge', () => {
it('works with label (falsy)', () => {
expect(bridgeI.getProps('id', {label: null})).toEqual({
label: '',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3]
});
});

it('works with placeholder (custom)', () => {
expect(bridgeI.getProps('id', {placeholder: 'Post ID'})).toEqual({
label: 'Post ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3]
});
});

it('works with placeholder (true)', () => {
expect(bridgeI.getProps('id', {placeholder: true})).toEqual({
label: 'Post ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3]
});
});

it('works with placeholder (falsy)', () => {
expect(bridgeI.getProps('id', {placeholder: null})).toEqual({
label: 'Post ID',
placeholder: '',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test for placeholder === true and extra.placeholder === undefined is missing.

required: true,
allowedValues: [1, 2, 3]
});
Expand Down
6 changes: 6 additions & 0 deletions packages/uniforms/src/GraphQLBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ export default class GraphQLBridge extends Bridge {
ready.label = '';
}

if (props.placeholder === true && extra.placeholder) {
ready.placeholder = extra.placeholder;
} else if (props.placeholder !== undefined && !props.placeholder) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather check for false and null explicitly here.

ready.placeholder = '';
}

if (fieldType instanceof graphql.GraphQLScalarType && fieldType.name === 'Float') {
ready.decimal = true;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/uniforms/src/JSONSchemaBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,22 @@ export default class JSONSchemaBridge extends Bridge {
return defaultValue;
}

getProps (name, {label = true, options: opts, ...props} = {}) {
getProps (name, {label = true, options: opts, placeholder, ...props} = {}) {
const {enum: _enum, type: _type, options: _options, uniforms} = this.getField(name);
const {
enum: allowedValues = _enum, options = _options,type: fieldType = _type, isRequired
} = this._compiledSchema[name];

const [fieldName] = joinName(null, name).slice(-1).map(str => str.replace(/([A-Z])/g, ' $1'));

const fieldNameCapitalized = fieldName.charAt(0).toUpperCase() + fieldName.slice(1).toLowerCase();

const ready = {
allowedValues,
...(fieldType === 'number' ? {decimal: true} : {}),
options: opts || options,
label: label === true ? fieldName.charAt(0).toUpperCase() + fieldName.slice(1).toLowerCase() : label || '',
label: label === true ? fieldNameCapitalized : label || '',
placeholder: placeholder === true ? fieldNameCapitalized : placeholder || undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for || undefined.

required: isRequired
};

Expand Down