Skip to content

Commit 68b2b00

Browse files
committed
Input icon wrapped in button when callback defined
1 parent 74e7506 commit 68b2b00

File tree

3 files changed

+77
-36
lines changed

3 files changed

+77
-36
lines changed

components/forms/input/index.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ const Input = React.createClass({
222222
'slds-has-divider--bottom': readOnly
223223
})}
224224
>
225-
{hasIcon && <InputIcon
225+
{hasIcon && iconPosition === 'left' && <InputIcon
226226
name={iconName}
227227
category={iconCategory}
228228
onClick={onIconClick}
@@ -240,6 +240,11 @@ const Input = React.createClass({
240240
type={type}
241241
value={value}
242242
/>}
243+
{hasIcon && iconPosition === 'right' && <InputIcon
244+
name={iconName}
245+
category={iconCategory}
246+
onClick={onIconClick}
247+
/>}
243248
{readOnly && <span className="slds-form-element__static">
244249
{value}
245250
</span>}

components/icon/input-icon/index.jsx

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,66 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
1010
*/
1111

1212
import React from 'react';
13+
1314
import SLDSUtilityIcon from '../../utilities/utility-icon';
1415

15-
const displayName = 'InputIcon';
16-
17-
const propTypes = {
18-
category: React.PropTypes.string,
19-
name: React.PropTypes.string,
20-
onClick: React.PropTypes.func,
21-
};
22-
const defaultProps = {
23-
category: 'utility',
24-
};
25-
26-
class InputIcon extends React.Component {
27-
28-
constructor(props) {
29-
super(props);
30-
this.state = {};
31-
}
32-
33-
render() {
34-
const className = 'slds-input__icon slds-icon-text-default';
35-
return <SLDSUtilityIcon
36-
style={this.props.style}
37-
aria-hidden='true'
38-
category={this.props.category}
39-
className={className}
40-
name={this.props.name}
41-
onClick={this.props.onClick}
42-
/>;
43-
}
44-
}
45-
46-
InputIcon.displayName = displayName;
47-
InputIcon.propTypes = propTypes;
48-
InputIcon.defaultProps = defaultProps;
16+
import { ICON_INPUT } from '../../../utilities/constants';
17+
18+
// Remove the need for `React.PropTypes`
19+
const { PropTypes } = React;
20+
21+
// ### isFunction
22+
import isFunction from 'lodash.isfunction';
23+
24+
const InputIcon = React.createClass({
25+
26+
displayName: ICON_INPUT,
27+
28+
propTypes: {
29+
category: PropTypes.string,
30+
name: PropTypes.string,
31+
style: PropTypes.string,
32+
/**
33+
* This event fires when the icon is clicked.
34+
*/
35+
onClick: PropTypes.func
36+
},
37+
38+
getDefaultProps () {
39+
return {
40+
category: 'utility'
41+
};
42+
},
43+
44+
render () {
45+
const onClickIsFunction = isFunction(this.props.onClick);
46+
47+
return (
48+
<div>
49+
{onClickIsFunction ?
50+
<button
51+
className="slds-input__icon slds-button slds-button--icon"
52+
onClick={this.props.onClick}
53+
>
54+
<SLDSUtilityIcon
55+
style={this.props.style}
56+
aria-hidden
57+
category={this.props.category}
58+
className="slds-button__icon slds-icon-text-default"
59+
name={this.props.name}
60+
/>
61+
</button>
62+
: <SLDSUtilityIcon
63+
style={this.props.style}
64+
aria-hidden
65+
category={this.props.category}
66+
className="slds-input__icon slds-icon-text-default"
67+
name={this.props.name}
68+
/>
69+
}
70+
</div>
71+
);
72+
}
73+
});
4974

5075
module.exports = InputIcon;

stories/input/index.jsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ storiesOf(FORMS_INPUT, module)
2222
placeholder="Placeholder Text"
2323
/>
2424
))
25-
.add('Input with Clickable Icon', () => (
25+
.add('Input with Left Clickable Icon', () => (
26+
<Input
27+
id="unique-id-123"
28+
label="Input Label"
29+
iconName="search"
30+
iconCategory="utility"
31+
iconPosition="left"
32+
onIconClick={iconClicked('Search icon clicked')}
33+
placeholder="Placeholder Text"
34+
/>
35+
))
36+
.add('Input with Right Clickable Icon', () => (
2637
<Input
2738
id="unique-id-2"
2839
label="Input Label"

0 commit comments

Comments
 (0)