Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit 1be6577

Browse files
authored
Merge pull request #10 from renanborgez/renan/adornments
Add start and end adornments
2 parents 42028c6 + 23b93c9 commit 1be6577

File tree

6 files changed

+68
-12
lines changed

6 files changed

+68
-12
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,13 @@ You can provide a custom `className` to the Nice Input Password and custom `clas
9292
| name | string | undefined | The name used on input element `name={name}` |
9393
| placeholder | string | (empty string) | The placeholder used on input element `placeholder={placeholder}` |
9494
| className | string | (empty string) | Optional class to be passed to niceinputpassword context |
95+
| style | object | undefined | Optional style to be passed to input field |
9596
| normalClassName | string | 'none' | The className used on level color
9697
| dangerClassName | string | 'danger' | The className used on level color
9798
| warningClassName | string | 'warning' | The className used on level color
9899
| successClassName | string | 'success' | The className used on level color
100+
| startAdornment | ReactNode | undefined | Start adornment for this component
101+
| endAdornment | ReactNode | undefined | End adornment for this component
99102
| value | string | undefined | The value to be renderized on element
100103
| showSecurityLevelBar | bool | false | Key to show or not the security level bullets of password
101104
| showSecurityLevelDescription | bool | false | Key to show or not the security level description securityLevels object
@@ -104,7 +107,7 @@ You can provide a custom `className` to the Nice Input Password and custom `clas
104107

105108
## License
106109

107-
MIT Licensed. Copyright (c) Renan Borges 2018.
110+
MIT Licensed. Copyright (c) Renan Borges.
108111

109112

110113
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Frenanborgez%2Freact-nice-input-password.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Frenanborgez%2Freact-nice-input-password?ref=badge_large)

public/index.jsx

+15
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ class App extends React.Component {
7373
onChange={this.handleChange}
7474
/>
7575
<hr />
76+
77+
<h2>With start and end adornment</h2>
78+
<NiceInputPassword
79+
label="Password"
80+
name="pass4"
81+
showSecurityLevelBar
82+
showSecurityLevelDescription
83+
securityLevels={securityLevels}
84+
value={this.state.pass4}
85+
onChange={this.handleChange}
86+
style={{ paddingLeft: 15 }}
87+
startAdornment={<div style={{ position: 'absolute', top: 2, left: 5 }}>Ξ</div>}
88+
endAdornment="OK"
89+
/>
90+
<hr />
7691
</div>
7792
);
7893
}

src/NiceInputPassword.jsx

+12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const propTypes = {
1111
]).isRequired,
1212
name: PropTypes.string.isRequired,
1313
className: PropTypes.string,
14+
style: PropTypes.object,
1415
value: PropTypes.string,
16+
startAdornment: PropTypes.node,
17+
endAdornment: PropTypes.node,
1518
showSecurityLevelBar: PropTypes.bool,
1619
showSecurityLevelDescription: PropTypes.bool,
1720
normalClassName: PropTypes.string,
@@ -37,6 +40,7 @@ const defaultProps = {
3740
value: '',
3841
className: '',
3942
placeholder: '',
43+
style: null,
4044
showSecurityLevelBar: false,
4145
showSecurityLevelDescription: false,
4246
securityLevels: [],
@@ -45,6 +49,8 @@ const defaultProps = {
4549
warningClassName: 'warning',
4650
successClassName: 'success',
4751
onChange: () => {},
52+
startAdornment: null,
53+
endAdornment: null,
4854
};
4955

5056
class NiceInputPassword extends React.Component {
@@ -103,6 +109,9 @@ class NiceInputPassword extends React.Component {
103109
className,
104110
value,
105111
placeholder,
112+
startAdornment,
113+
endAdornment,
114+
style,
106115
} = this.props;
107116

108117
let inputClassName = '';
@@ -164,6 +173,9 @@ class NiceInputPassword extends React.Component {
164173
placeholder={placeholder}
165174
className={inputClassName}
166175
value={value}
176+
style={style}
177+
startAdornment={startAdornment}
178+
endAdornment={endAdornment}
167179
onChange={this.handleChange}
168180
/>
169181
{securityLevels && securityLevels.length > 0 ? (

src/NiceInputPassword.scss

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ $font-size-small: 12px;
2727
input {
2828
box-sizing: border-box;
2929
display: block;
30-
margin: 5px 0 10px;
3130

3231
&:focus {
3332
outline: none;
@@ -40,6 +39,13 @@ $font-size-small: 12px;
4039
}
4140
}
4241

42+
&__field {
43+
display: flex;
44+
flex-direction: row;
45+
align-items: center;
46+
position: relative;
47+
}
48+
4349
&__description {
4450
list-style: none;
4551
padding: 0;

src/components/InputLabel.jsx

+23-9
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ const propTypes = {
88
PropTypes.element,
99
]).isRequired,
1010
className: PropTypes.string,
11+
style: PropTypes.object,
1112
placeholder: PropTypes.string,
1213
value: PropTypes.string,
1314
onChange: PropTypes.func.isRequired,
15+
startAdornment: PropTypes.node,
16+
endAdornment: PropTypes.node,
1417
};
1518

1619
const defaultProps = {
1720
placeholder: '',
1821
value: '',
1922
className: '',
23+
style: null,
24+
startAdornment: null,
25+
endAdornment: null,
2026
};
2127

2228
const InputLabel = ({
@@ -25,19 +31,27 @@ const InputLabel = ({
2531
placeholder,
2632
value,
2733
className,
34+
startAdornment,
35+
endAdornment,
36+
style,
2837
onChange,
2938
}) => (
3039
<label htmlFor={name} className={className}>
3140
{label}
32-
<input
33-
name={name}
34-
id={name}
35-
className={className}
36-
value={value}
37-
type="password"
38-
placeholder={placeholder}
39-
onChange={onChange}
40-
/>
41+
<div className="input-password__field">
42+
{startAdornment && <div className="input-password__startAdornment">{startAdornment}</div>}
43+
<input
44+
name={name}
45+
id={name}
46+
className={className}
47+
value={value}
48+
type="password"
49+
style={style}
50+
placeholder={placeholder}
51+
onChange={onChange}
52+
/>
53+
{endAdornment && <div className="input-password__endAdornment">{endAdornment}</div>}
54+
</div>
4155
</label>
4256
);
4357

src/components/InputLabel.test.jsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@ describe('components', () => {
2424
placeholder="myPlaceholder"
2525
onChange={() => {}}
2626
value="myValue"
27+
startAdornment={<div className="start-adorn">A</div>}
28+
endAdornment={<div className="end-adorn">B</div>}
2729
/>, div);
2830

2931
const input = div.querySelector('#myName');
3032
const label = div.querySelector('label');
33+
const startAdornment = div.querySelector('.start-adorn');
34+
const endAdornment = div.querySelector('.end-adorn');
3135

32-
expect(label.textContent).toBe('myLabel');
36+
expect(label.textContent).toContain('myLabel');
3337
expect(label.htmlFor).toBe('myName');
3438
expect(input.name).toBe('myName');
3539
expect(input.value).toBe('myValue');
3640
expect(input.placeholder).toBe('myPlaceholder');
41+
expect(startAdornment.textContent).toBe('A');
42+
expect(endAdornment.textContent).toBe('B');
3743
});
3844

3945
it('calls onChange handler', () => {

0 commit comments

Comments
 (0)