Skip to content

Commit

Permalink
Merge branch 'master' into date-input-blur
Browse files Browse the repository at this point in the history
  • Loading branch information
chawes13 authored Feb 1, 2019
2 parents 6d5dec4 + b5b23ac commit 135f2f8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 24 deletions.
6 changes: 4 additions & 2 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1051,13 +1051,15 @@ For instance: `'person.firstName'` becomes `'First Name'`
- `hint` **[String][131]?** A usage hint for the associated input
- `label` **([String][131] \| [Boolean][133])?** Custom text for the label
- `tooltip` **[String][131]?** A message to display in a tooltip
- `required` **[Boolean][133]?** A boolean value to indicate whether the field is required
- `requiredIndicator` **[String][131]?** Custom character to denote a field is required (optional, default `''`)

### Examples

```javascript
// A custom input to use with redux-forms

function EmailInput ({
function EmailInput ({
input: { name, value, onBlur, onChange },
label,
}) {
Expand All @@ -1069,7 +1071,7 @@ function EmailInput ({
name,
value,
onBlur,
onChange,
onChange,
}}
</div>
)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@launchpadlab/lp-components",
"version": "3.15.10",
"version": "3.16.0",
"engines": {
"node": "^8.0.0"
},
Expand Down
40 changes: 29 additions & 11 deletions src/forms/labels/input-label.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { toggle, togglePropTypes } from '../../utils'
/**
*
* A dynamic label associated with an input component.
*
*
* This component is used within {@link LabeledField}, and therefore is incorporated into most `lp-components` input components by default.
*
* The text of the label is set using the following rules:
Expand All @@ -18,19 +18,21 @@ import { toggle, togglePropTypes } from '../../utils'
* If `name` is used to set the text, it will be stripped of its prefixes and converted to [start case](https://en.wikipedia.org/wiki/Letter_case#Stylistic_or_specialised_usage).
*
* For instance: `'person.firstName'` becomes `'First Name'`
*
*
* @name InputLabel
* @type Function
* @param {String} name - The name of the associated input
* @param {String} [hint] - A usage hint for the associated input
* @param {String|Boolean} [label] - Custom text for the label
* @param {String} [tooltip] - A message to display in a tooltip
* @param {Boolean} [required] - A boolean value to indicate whether the field is required
* @param {String} [requiredIndicator] - Custom character to denote a field is required (optional, default `''`)
* @example
*
*
* // A custom input to use with redux-forms
*
* function EmailInput ({
*
* function EmailInput ({
* input: { name, value, onBlur, onChange },
* label,
* }) {
Expand All @@ -42,7 +44,7 @@ import { toggle, togglePropTypes } from '../../utils'
* name,
* value,
* onBlur,
* onChange,
* onChange,
* }}
* </div>
* )
Expand All @@ -55,34 +57,50 @@ const propTypes = {
label: PropTypes.oneOfType([ PropTypes.string, PropTypes.bool ]),
name: PropTypes.string.isRequired,
tooltip: PropTypes.string,
required: PropTypes.bool,
requiredIndicator: PropTypes.string,
...togglePropTypes('tooltipShown')
}

const defaultProps = {
hint: '',
label: '',
tooltip: '',
requiredIndicator: '',
}

function InputLabel ({ hint, label, name, tooltip, tooltipShown, toggleTooltipShown }) {
function InputLabel ({
hint,
label,
name,
tooltip,
tooltipShown,
toggleTooltipShown,
required,
requiredIndicator,
}) {
const labelText = label || convertNameToLabel(name)
return (
<span>
{
{
label !== false &&
<label htmlFor={ name }>
{ labelText }
{
{
required && requiredIndicator &&
<span className="required-indicator" aria-hidden="true">{ requiredIndicator }</span>
}
{
hint &&
<i>{ hint }</i>
}
</label>
}
{
{
tooltip &&
<span className="tooltip-trigger" onClick={ toggleTooltipShown }/>
}
{
{
tooltip &&
<div className={ classnames('tooltip-content', { 'is-active': tooltipShown }) }>
{ tooltip }
Expand Down
4 changes: 3 additions & 1 deletion src/forms/labels/labeled-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ function LabeledField ({
hint,
label,
tooltip,
required,
requiredIndicator,
children,
hideErrorLabel,
}) {
return (
<fieldset className={ classnames(className, { 'error': touched && invalid }) }>
<InputLabel { ...{ hint, label, name, tooltip } } />
<InputLabel { ...{ hint, label, name, tooltip, required, requiredIndicator } } />
{ children }
{ !hideErrorLabel && <InputError { ...{ error, invalid, touched } } /> }
</fieldset>
Expand Down
25 changes: 17 additions & 8 deletions stories/forms/inputs/input.story.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,42 @@ const inputProps = {

storiesOf('Input', module)
.add('with default label', () => (
<Input
input={inputProps}
<Input
input={inputProps}
meta={{}}
/>
))
.add('with custom label', () => (
<Input
<Input
input={inputProps}
meta={{}}
label="Custom Label"
/>
))
.add('with no label', () => (
<Input
<Input
input={inputProps}
meta={{}}
label={false}
/>
))
.add('with required true custom indicator', () => (
<Input
input={inputProps}
meta={{}}
label="Custom Label"
required
requiredIndicator={ '*' }
/>
))
.add('with error', () => (
<Input
<Input
input={inputProps}
meta={{
meta={{
invalid: true,
touched: true,
error: 'Invalid input'
error: 'Invalid input'
}}
value="0000"
/>
))
))
17 changes: 16 additions & 1 deletion stories/forms/labels/input-label.story.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InputLabel } from 'src'

storiesOf('InputLabel', module)
.add('with default label', () => (
<InputLabel
<InputLabel
name="nameOfInput"
/>
))
Expand All @@ -20,6 +20,21 @@ storiesOf('InputLabel', module)
label={false}
/>
))
.add('with required true default indicator', () => (
<InputLabel
name="nameOfInput"
label="Custom Label"
required
/>
))
.add('with required true custom indicator', () => (
<InputLabel
name="nameOfInput"
label="Custom Label"
required
requiredIndicator={ '*' }
/>
))
.add('with hint', () => (
<InputLabel
name="nameOfInput"
Expand Down
10 changes: 10 additions & 0 deletions test/forms/labels/input-label.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ test('when tooltip provided - toggle tooltip', () => {
wrapper.find('span.tooltip-trigger').simulate('click')
expect(wrapper.find('div.tooltip-content.is-active').exists()).toEqual(false)
})

test('when no custom required indicator provided, do not show required indicator', () => {
const wrapper = mount(<InputLabel name={name} required />)
expect(wrapper.find('span.required-indicator').exists()).toEqual(false)
})

test('when required true and custom requiredIndicator provided, show custom indicator', () => {
const wrapper = mount(<InputLabel name={name} required requiredIndicator={ '*' } />)
expect(wrapper.find('label > span').text()).toEqual('*')
})

0 comments on commit 135f2f8

Please sign in to comment.