Skip to content

Commit

Permalink
fix(text-field): update isRtl to a prop (#195)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: In order for rtl to work on the Notched Outline component, isRtl will need to be passed in as boolean (default is false). Updating isRtl will result in a re-render.
  • Loading branch information
Matt Goo authored Aug 1, 2018
1 parent f88f180 commit cd7c7c8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/text-field/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ dense | Boolean | Enables dense variant.
floatingLabelClassName | String | An optional class added to the floating label element of the text field.
fullWidth | Boolean | Enables fullWidth variant.
helperText | Element | Helper text that appears below the text field. Use the `<HelperText>` component that comes with this package.
isRtl | Boolean | Whether the direction of the text field element is set to RTL.
label | String | Mandatory. Label text that appears as the floating label or placeholder.
leadingIcon | Element | An icon element that appears as the leading icon.
lineRippleClassName | String | An optional class added to the line ripple element.
Expand Down
15 changes: 4 additions & 11 deletions packages/text-field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class TextField extends React.Component {
super(props);
this.floatingLabelElement = React.createRef();
this.inputElement = React.createRef();
this.textFieldElement = React.createRef();

this.state = {
// root state
Expand Down Expand Up @@ -94,7 +93,7 @@ class TextField extends React.Component {
},
hasClass: (className) => this.classes.split(' ').includes(className),
isFocused: () => this.state.isFocused,
isRtl: this.getIsRtl,
isRtl: () => this.props.isRtl,
};

return Object.assign({},
Expand Down Expand Up @@ -182,13 +181,6 @@ class TextField extends React.Component {
};
}

getIsRtl = () => {
if (this.textFieldElement.current) {
const dir = window.getComputedStyle(this.textFieldElement.current).getPropertyValue('direction');
return dir === 'rtl';
}
}

inputProps(props) {
return Object.assign({}, props, {
foundation: this.foundation_,
Expand Down Expand Up @@ -221,7 +213,6 @@ class TextField extends React.Component {
onClick={() => this.foundation_ && this.foundation_.handleTextFieldInteraction()}
onKeyDown={() => this.foundation_ && this.foundation_.handleTextFieldInteraction()}
key='text-field-container'
ref={this.textFieldElement}
>
{leadingIcon ? this.renderIcon(leadingIcon) : null}
{this.renderInput()}
Expand Down Expand Up @@ -281,7 +272,7 @@ class TextField extends React.Component {
return (
<NotchedOutline
className={notchedOutlineClassName}
isRtl={this.getIsRtl()}
isRtl={this.props.isRtl}
notch={outlineIsNotched}
notchWidth={labelWidth}
/>
Expand Down Expand Up @@ -319,6 +310,7 @@ TextField.propTypes = {
'floatingLabelClassName': PropTypes.string,
'fullWidth': PropTypes.bool,
'helperText': PropTypes.element,
'isRtl': PropTypes.bool,
'label': PropTypes.string.isRequired,
'leadingIcon': PropTypes.element,
'lineRippleClassName': PropTypes.string,
Expand All @@ -335,6 +327,7 @@ TextField.defaultProps = {
floatingLabelClassName: '',
fullWidth: false,
helperText: null,
isRtl: false,
leadingIcon: null,
lineRippleClassName: '',
notchedOutlineClassName: '',
Expand Down
2 changes: 1 addition & 1 deletion test/screenshot/text-field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestField extends React.Component {
} = this.props;
return (
<div dir={isRtl ? 'rtl' : 'ltr'}>
<TextField label='Dog' {...otherProps} className='text-field'>
<TextField label='Dog' {...otherProps} className='text-field' isRtl={isRtl}>
<Input value={this.state.value}
id={id}
disabled={disabled}
Expand Down
22 changes: 10 additions & 12 deletions test/unit/text-field/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,22 @@ test('#adapter.removeClass removes class from state.classList', () => {
assert.isTrue(wrapper.instance().foundation_.adapter_.hasClass('test-class-name'));
});

test('#adapter.isFocused returns true if wrapped in an rtl element', () => {
test('#adapter.isFocused returns true if state.isFocused updates to true', () => {
const wrapper = mount(<TextField label='my label'><Input /></TextField>);
wrapper.setState({isFocused: true});
assert.isTrue(wrapper.instance().foundation_.adapter_.isFocused());
});

test('#adapter.isRtl returns true if the direction is true', () => {
const div = document.createElement('div');
// needs to be attached to real DOM to get width
// https://github.com/airbnb/enzyme/issues/1525
document.body.style.direction = 'rtl';
document.body.append(div);
const options = {attachTo: div};
const wrapper = mount(
<TextField label='my label'><Input /></TextField>, options);
test('#adapter.isRtl returns true props.isRtl if is true', () => {
const wrapper = shallow(
<TextField isRtl label='my label'><Input /></TextField>);
assert.isTrue(wrapper.instance().foundation_.adapter_.isRtl());
document.body.style.direction = 'initial';
div.remove();
});

test('#adapter.isRtl returns false props.isRtl if is false', () => {
const wrapper = mount(
<TextField label='my label'><Input /></TextField>);
assert.isFalse(wrapper.instance().foundation_.adapter_.isRtl());
});

test('#adapter.input.getNativeInput.validity.badInput return false for valid input', () => {
Expand Down

0 comments on commit cd7c7c8

Please sign in to comment.