-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from dabapps/input-with-prefix-suffix
Input with prefix suffix
- Loading branch information
Showing
8 changed files
with
257 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
25 changes: 25 additions & 0 deletions
25
src/ts/components/precomposed/input-with-prefix-suffix.examples.md
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,25 @@ | ||
#### Examples | ||
|
||
Example with class names | ||
|
||
```js | ||
<InputWithPrefixSuffix | ||
prefix="£" | ||
suffix="%" | ||
value="Example" | ||
className="applied-to-input-group" | ||
inputClassName="applied-to-input" | ||
prefixClassName="primary" | ||
suffixClassName="error" | ||
/> | ||
``` | ||
|
||
Display block with React element prefix | ||
|
||
```js | ||
<InputWithPrefixSuffix | ||
block | ||
prefix={<strong>Strong</strong>} | ||
value="Example" | ||
/> | ||
``` |
78 changes: 78 additions & 0 deletions
78
src/ts/components/precomposed/input-with-prefix-suffix.tsx
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,78 @@ | ||
import * as React from 'react'; | ||
import { PureComponent } from 'react'; | ||
import { ComponentProps } from '../../types'; | ||
import InputGroup from '../forms/input-group'; | ||
import InputGroupAddon from '../forms/input-group-addon'; | ||
|
||
export interface PrefixSuffixProps extends ComponentProps { | ||
/** | ||
* Content to display to the left of the input. | ||
*/ | ||
prefix?: React.ReactChild; | ||
/** | ||
* Content to display to the right of the input. | ||
*/ | ||
suffix?: React.ReactChild; | ||
/** | ||
* Set the style `display: block;` so the input group fills its parent. | ||
*/ | ||
block?: boolean; | ||
/** | ||
* Class name to apply to the input. | ||
*/ | ||
inputClassName?: string; | ||
/** | ||
* Class name to apply to the prefix. | ||
*/ | ||
prefixClassName?: string; | ||
/** | ||
* Class name to apply to the suffix. | ||
*/ | ||
suffixClassName?: string; | ||
} | ||
|
||
export type InputWithPrefixSuffixProps = React.HTMLAttributes< | ||
HTMLInputElement | ||
> & | ||
PrefixSuffixProps; | ||
|
||
/** | ||
* A precomposed Input containing an optional prefix (InputGroupAddon), an input, | ||
* and an optional suffix (InputGroupAddon). | ||
*/ | ||
export class InputWithPrefixSuffix extends PureComponent< | ||
InputWithPrefixSuffixProps, | ||
{} | ||
> { | ||
public render() { | ||
const { | ||
prefix, | ||
suffix, | ||
block, | ||
className, | ||
inputClassName, | ||
prefixClassName, | ||
suffixClassName, | ||
component, | ||
...remainingProps | ||
} = this.props; | ||
|
||
return ( | ||
<InputGroup component={component} block={block} className={className}> | ||
{typeof prefix !== 'undefined' && ( | ||
<InputGroupAddon className={prefixClassName}> | ||
{prefix} | ||
</InputGroupAddon> | ||
)} | ||
<input className={inputClassName} {...remainingProps} /> | ||
{typeof suffix !== 'undefined' && ( | ||
<InputGroupAddon className={suffixClassName}> | ||
{suffix} | ||
</InputGroupAddon> | ||
)} | ||
</InputGroup> | ||
); | ||
} | ||
} | ||
|
||
export default InputWithPrefixSuffix; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`InputWithPrefixSuffix should add the "block" class name if this prop is true 1`] = ` | ||
<div | ||
className="input-group block" | ||
> | ||
<div | ||
className="input-group-addon" | ||
style={ | ||
Object { | ||
"width": undefined, | ||
} | ||
} | ||
> | ||
£ | ||
</div> | ||
<input | ||
className={undefined} | ||
/> | ||
<div | ||
className="input-group-addon" | ||
style={ | ||
Object { | ||
"width": undefined, | ||
} | ||
} | ||
> | ||
% | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`InputWithPrefixSuffix should apply class names to appropriate elements 1`] = ` | ||
<div | ||
className="input-group group" | ||
> | ||
<div | ||
className="input-group-addon prefix" | ||
style={ | ||
Object { | ||
"width": undefined, | ||
} | ||
} | ||
> | ||
£ | ||
</div> | ||
<input | ||
className="input" | ||
/> | ||
<div | ||
className="input-group-addon suffix" | ||
style={ | ||
Object { | ||
"width": undefined, | ||
} | ||
} | ||
> | ||
% | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`InputWithPrefixSuffix should match snapshot 1`] = ` | ||
<div | ||
className="input-group" | ||
> | ||
<input | ||
className={undefined} | ||
/> | ||
</div> | ||
`; | ||
|
||
exports[`InputWithPrefixSuffix should pass props to the input 1`] = ` | ||
<div | ||
className="input-group" | ||
> | ||
<div | ||
className="input-group-addon" | ||
style={ | ||
Object { | ||
"width": undefined, | ||
} | ||
} | ||
> | ||
£ | ||
</div> | ||
<input | ||
className={undefined} | ||
value="Value" | ||
/> | ||
<div | ||
className="input-group-addon" | ||
style={ | ||
Object { | ||
"width": undefined, | ||
} | ||
} | ||
> | ||
% | ||
</div> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as React from 'react'; | ||
import * as renderer from 'react-test-renderer'; | ||
import { InputWithPrefixSuffix } from '../src/ts/components/precomposed/input-with-prefix-suffix'; | ||
|
||
describe('InputWithPrefixSuffix', () => { | ||
it('should match snapshot', () => { | ||
const tree = renderer.create(<InputWithPrefixSuffix />); | ||
|
||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
it('should apply class names to appropriate elements', () => { | ||
const tree = renderer.create( | ||
<InputWithPrefixSuffix | ||
prefix="£" | ||
suffix="%" | ||
className="group" | ||
inputClassName="input" | ||
prefixClassName="prefix" | ||
suffixClassName="suffix" | ||
/> | ||
); | ||
|
||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
it('should add the "block" class name if this prop is true', () => { | ||
const tree = renderer.create( | ||
<InputWithPrefixSuffix prefix="£" suffix="%" block /> | ||
); | ||
|
||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
it('should pass props to the input', () => { | ||
const tree = renderer.create( | ||
<InputWithPrefixSuffix prefix="£" suffix="%" value="Value" /> | ||
); | ||
|
||
expect(tree).toMatchSnapshot(); | ||
}); | ||
}); |