Skip to content

Commit

Permalink
feat(Textfield): support trailing and leading icons (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
kradio3 committed Dec 2, 2017
1 parent 29c1a25 commit 279deff
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Menu/Menu.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import PropTypes from 'prop-types';
import React, { Children } from 'react';
/* eslint-disable import/no-unresolved, import/extensions*/
/* eslint-disable import/no-unresolved, import/extensions */
import { clamp } from './util';
/* eslint-enable import/no-unresolved, import/extensions*/
/* eslint-enable import/no-unresolved, import/extensions */
import AnimationController from '../Animation/AnimationController';
import DocumentClickHandler from './DocumentClickHandler';
import MenuList from './MenuList';
Expand Down
4 changes: 2 additions & 2 deletions src/Menu/scaleCalculator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable import/no-unresolved, import/extensions*/
/* eslint-disable import/no-unresolved, import/extensions */
import { clamp, bezierProgress } from './util';
/* eslint-enable import/no-unresolved, import/extensions*/
/* eslint-enable import/no-unresolved, import/extensions */

import { TRANSITION_SCALE_ADJUSTMENT_X, TRANSITION_SCALE_ADJUSTMENT_Y } from './constants';

Expand Down
58 changes: 45 additions & 13 deletions src/Textfield/Field.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import PropTypes from 'prop-types';
import React from 'react';
import React, { Children, cloneElement } from 'react';
import classnames from 'classnames';
import Icon from '../Icon';

const propTypes = {
box: PropTypes.bool,
children: PropTypes.node,
className: PropTypes.string,
disabled: PropTypes.bool,
focused: PropTypes.bool,
invalid: PropTypes.bool,
textarea: PropTypes.bool,
trailingIcon: PropTypes.bool,
};

const ROOT = 'mdc-text-field';
Expand All @@ -17,18 +20,47 @@ const DISABLED = `${ROOT}--disabled`;
const FOCUSED = `${ROOT}--focused`;
const INVALID = `${ROOT}--invalid`;
const UPGRADED = `${ROOT}--upgraded`;
const BOX = `${ROOT}--box`;
const LEADING_ICON = `${ROOT}--with-leading-icon`;
const TRAILING_ICON = `${ROOT}--with-trailing-icon`;
const ICON = `${ROOT}__icon`;

const Field = ({ className, focused, disabled, invalid, children, textarea }) => (
<div
className={classnames(ROOT, {
[TEXTAREA]: textarea,
[FOCUSED]: focused,
[DISABLED]: disabled,
[INVALID]: invalid,
}, UPGRADED, className)}
>
{children}
</div>
);
const Field = ({
className,
box,
focused,
disabled,
invalid,
children,
textarea,
trailingIcon,
}) => {
let hasIcon = false;
const childs = Children.map(children, (child) => {
if (child && child.type === Icon) {
hasIcon = true;
const { className: childClassName } = child.props;
const classes = classnames(ICON, childClassName);
return cloneElement(child, { childClassName: classes });
}
return child;
});

return (
<div
className={classnames(ROOT, {
[TEXTAREA]: textarea,
[BOX]: box,
[FOCUSED]: focused,
[DISABLED]: disabled,
[INVALID]: invalid,
[LEADING_ICON]: hasIcon && !trailingIcon,
[TRAILING_ICON]: hasIcon && trailingIcon,
}, UPGRADED, className)}
>
{childs}
</div>
);
};
Field.propTypes = propTypes;
export default Field;
6 changes: 3 additions & 3 deletions src/Textfield/Helptext.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const propTypes = {
invalid: PropTypes.bool,
};

const ROOT = 'mdc-text-field-helptext';
const PERSISTENT = 'mdc-textfield-helptext--persistent';
const VALIDATION = 'mdc-textfield-helptext--validation-msg';
const ROOT = 'mdc-text-field-helper-text';
const PERSISTENT = 'mdc-text-field-helper-text--persistent';
const VALIDATION = 'mdc-text-field-helper-text--validation-msg';

const Helptext = ({
children,
Expand Down
15 changes: 12 additions & 3 deletions src/Textfield/Textfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import BottomLine from './BottomLine';
class Textfield extends React.PureComponent {

static propTypes = {
box: PropTypes.bool,
children: PropTypes.node,
className: PropTypes.string,
disabled: PropTypes.bool,
floatingLabel: PropTypes.string,
helptext: PropTypes.string,
helptextPersistent: PropTypes.bool,
helptextValidation: PropTypes.bool,
useInvalidProp: PropTypes.bool,
invalid: PropTypes.bool,
id: PropTypes.string,
textarea: PropTypes.bool,
invalid: PropTypes.bool,
required: PropTypes.bool,
textarea: PropTypes.bool,
trailingIcon: PropTypes.bool,
useInvalidProp: PropTypes.bool,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
}

Expand All @@ -41,7 +44,9 @@ class Textfield extends React.PureComponent {
const focus = this.state.focus;
let invalid = this.state.invalid;
const {
box,
className,
children,
disabled,
floatingLabel,
helptext, // eslint-disable-line no-unused-vars
Expand All @@ -51,6 +56,7 @@ class Textfield extends React.PureComponent {
invalid: invalidProp,
id,
textarea,
trailingIcon,
value, // eslint-disable-line no-unused-vars
...otherProps
} = this.props;
Expand All @@ -61,11 +67,14 @@ class Textfield extends React.PureComponent {
return (
<Field
className={className}
box={box}
disabled={disabled}
focused={focus}
invalid={invalid}
textarea={textarea}
trailingIcon={trailingIcon}
>
{children}
<Input
disabled={disabled}
id={customId}
Expand Down

0 comments on commit 279deff

Please sign in to comment.