Skip to content

Commit

Permalink
Fix hint+requirementLabel bug in FormLabel component
Browse files Browse the repository at this point in the history
  • Loading branch information
pwolfert authored and sawyerh committed Mar 31, 2018
1 parent 6b0516e commit 9f5cb04
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,8 @@ exports[`DateField has requirementLabel 1`] = `
className="ds-c-field__hint"
>
Optional.
For example: 4 28 1986
For example: 4 28 1986
</span>
</legend>
<div
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/components/FormLabel/FormLabel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ export class FormLabel extends React.PureComponent {
}

hint() {
let { hint, requirementLabel } = this.props;
const { hint } = this.props;
let { requirementLabel } = this.props;
if (!hint && !requirementLabel) return;

const classes = classNames('ds-c-field__hint', {
'ds-c-field__hint--inverse': this.props.inversed
});

let hintPadding = null;

if (requirementLabel && hint) {
if (typeof requirementLabel === 'string') {
// Remove any existing spacing and punctuation
Expand All @@ -38,12 +41,13 @@ export class FormLabel extends React.PureComponent {
}

// Add space between hint and preceding requirementLabel
hint = ' ' + hint;
hintPadding = ' ';
}

return (
<span className={classes}>
{requirementLabel}
{hintPadding}
{hint}
</span>
);
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/components/FormLabel/FormLabel.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,16 @@ describe('FormLabel', () => {
});

it('adds punctuation to requirementLabel when hint is also present', () => {
const props = { hint: 'Hint', requirementLabel: 'Optional' };
const wrapper = shallow(<FormLabel {...props}>{labelText}</FormLabel>);
let props = { hint: 'Hint', requirementLabel: 'Optional' };
let wrapper = shallow(<FormLabel {...props}>{labelText}</FormLabel>);

expect(wrapper).toMatchSnapshot();

props = {
hint: <span>Hint</span>,
requirementLabel: <span>Optional</span>
};
wrapper = shallow(<FormLabel {...props}>{labelText}</FormLabel>);

expect(wrapper).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,31 @@ exports[`FormLabel adds punctuation to requirementLabel when hint is also presen
className="ds-c-field__hint"
>
Optional.
Hint
Hint
</span>
</label>
`;

exports[`FormLabel adds punctuation to requirementLabel when hint is also present 2`] = `
<label
className="ds-c-label"
>
<span
className=""
>
Hello world
</span>
<span
className="ds-c-field__hint"
>
<span>
Optional
</span>
<span>
Hint
</span>
</span>
</label>
`;
Expand All @@ -31,7 +55,8 @@ exports[`FormLabel avoids duplicate punctuation after requirementLabel 1`] = `
className="ds-c-field__hint"
>
Optional.
Hint
Hint
</span>
</label>
`;
Expand Down

0 comments on commit 9f5cb04

Please sign in to comment.