-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0b3ce6
commit 2b5de94
Showing
9 changed files
with
242 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
import safeRest from '../../../safeRest' | ||
import ColoredTextProvider from '../../Typography/ColoredTextProvider/ColoredTextProvider' | ||
import Paragraph from '../../Typography/Paragraph/Paragraph' | ||
|
||
import styles from './Helper.modules.scss' | ||
|
||
const getClassName = feedback => (feedback ? styles[feedback] : styles.default) | ||
|
||
const getContent = (feedback, children) => { | ||
const content = ( | ||
<Paragraph> | ||
{children} | ||
</Paragraph> | ||
) | ||
|
||
if (feedback === 'error') { | ||
return ( | ||
<ColoredTextProvider colorClassName={styles.errorText}> | ||
{content} | ||
</ColoredTextProvider> | ||
) | ||
} | ||
|
||
return content | ||
} | ||
|
||
const Helper = ({ feedback, children, ...rest }) => ( | ||
<div {...safeRest(rest)} className={getClassName(feedback)}> | ||
{getContent(feedback, children)} | ||
</div> | ||
) | ||
Helper.propTypes = { | ||
feedback: PropTypes.oneOf(['success', 'error']), | ||
children: PropTypes.node.isRequired | ||
} | ||
Helper.defaultProps = { | ||
feedback: undefined | ||
} | ||
Helper.displayName = 'Input.Helper' | ||
|
||
export default Helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
@import '../../../scss/settings/colours'; | ||
@import '../../../scss/settings/spacing'; | ||
|
||
$border-radius: 4px; | ||
|
||
.base { | ||
padding: $spacing-base; | ||
margin-bottom: $spacing-tight; // TODO: This should be moved up to be consistent with form field spacing | ||
|
||
border-radius: $border-radius; | ||
|
||
// transition: background-color .1s linear; TODO: Why? | ||
} | ||
|
||
.default { | ||
composes: base; | ||
|
||
background: $color-athens-grey; | ||
} | ||
|
||
.success { | ||
composes: base; | ||
|
||
background: $color-panache; | ||
} | ||
|
||
.error { | ||
composes: base; | ||
|
||
background-color: $color-lavender-blush; | ||
} | ||
|
||
// TODO: This is duplicated in Notification... | ||
.errorText { | ||
color: $color-cardinal; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React from 'react' | ||
import { shallow, render } from 'enzyme' | ||
import toJson from 'enzyme-to-json' | ||
|
||
import ColoredTextProvider from '../../../Typography/ColoredTextProvider/ColoredTextProvider' | ||
import Paragraph from '../../../Typography/Paragraph/Paragraph' | ||
|
||
import Helper from '../Helper' | ||
|
||
describe('Helper', () => { | ||
const defaultChildren = 'Some helper text.' | ||
const doShallow = (props = {}, children = defaultChildren) => ( | ||
shallow(<Helper {...props}>{children}</Helper>) | ||
) | ||
const doRender = (props = {}, children = defaultChildren) => ( | ||
render(<Helper {...props}>{children}</Helper>) | ||
) | ||
|
||
it('renders', () => { | ||
const helper = doRender() | ||
|
||
expect(toJson(helper)).toMatchSnapshot() | ||
}) | ||
|
||
it('can have a feedback state', () => { | ||
let helper = doShallow() | ||
expect(helper).toHaveClassName('default') | ||
|
||
helper = doShallow({ feedback: 'success' }) | ||
expect(helper).toHaveClassName('success') | ||
}) | ||
|
||
it('does not color the success content', () => { | ||
const helper = doShallow({ feedback: 'success' }, 'A success message') | ||
|
||
expect(helper).toContainReact( | ||
<Paragraph>A success message</Paragraph> | ||
) | ||
}) | ||
|
||
it('colors the error content', () => { | ||
const helper = doShallow({ feedback: 'error' }, 'An error message') | ||
|
||
expect(helper).toContainReact( | ||
<ColoredTextProvider colorClassName="errorText"> | ||
<Paragraph>An error message</Paragraph> | ||
</ColoredTextProvider> | ||
) | ||
}) | ||
|
||
it('passes additional attributes to the element', () => { | ||
const helper = doShallow({ id: 'the-helper', role: 'alert' }) | ||
|
||
expect(helper).toHaveProp('id', 'the-helper') | ||
expect(helper).toHaveProp('role', 'alert') | ||
}) | ||
|
||
it('does not allow custom CSS', () => { | ||
const helper = doShallow({ className: 'my-custom-class', style: { color: 'hotpink' } }) | ||
|
||
expect(helper).not.toHaveProp('className', 'my-custom-class') | ||
expect(helper).not.toHaveProp('style') | ||
}) | ||
}) |
19 changes: 19 additions & 0 deletions
19
src/components/Input/Helper/__tests__/__snapshots__/Helper.spec.jsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Helper renders 1`] = ` | ||
<div | ||
class="default" | ||
> | ||
<p | ||
class=" | ||
noSpacing | ||
color | ||
medium | ||
mediumFont | ||
leftAlign | ||
" | ||
> | ||
Some helper text. | ||
</p> | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,21 @@ | ||
``` | ||
<div> | ||
<Input label="First name" /> | ||
<Input label="First name (default)" /> | ||
<Input type="password" label="Password" value="123abc" /> | ||
<Input type="number" label="Age" value="32" /> | ||
<Input label="First name 2" autoFocus /> | ||
<Input label="First name (auto focus)" autoFocus /> | ||
<Input label="First name 3" disabled /> | ||
<Input label="First name (disabled)" disabled /> | ||
<Input label="First name 4" feedback="success" /> | ||
<Input label="First name 5" feedback="error" /> | ||
<Input label="First name (success)" feedback="success" /> | ||
<Input label="First name (error)" feedback="error" /> | ||
<Input label="First name 6" feedback="error" error="First name is required" /> | ||
<Input label="First name (error msg)" feedback="error" error="First name is required" /> | ||
<Input label="First name (helper)" feedback="success" | ||
helper={<Input.Helper>Some helper stuff</Input.Helper>} | ||
/> | ||
</div> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters