Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 38e9886

Browse files
author
Matt Goo
committed
feat(floating-label): update mdc web to v1.0.0 (#741)
1 parent dd95b60 commit 38e9886

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"@material/dom": "^0.41.0",
7171
"@material/drawer": "^1.0.1",
7272
"@material/fab": "^0.41.0",
73-
"@material/floating-label": "^0.41.0",
73+
"@material/floating-label": "^1.0.0",
7474
"@material/icon-button": "^0.41.0",
7575
"@material/layout-grid": "^0.41.0",
7676
"@material/line-ripple": "^1.0.0",

packages/floating-label/index.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
import * as React from 'react';
2424
import classnames from 'classnames';
25-
// @ts-ignore no mdc .d.ts
26-
import {MDCFloatingLabelFoundation} from '@material/floating-label/dist/mdc.floatingLabel';
25+
import {MDCFloatingLabelFoundation} from '@material/floating-label/foundation';
26+
import {MDCFloatingLabelAdapter} from '@material/floating-label/adapter';
2727

2828
export interface FloatingLabelProps extends React.LabelHTMLAttributes<HTMLLabelElement> {
2929
className?: string;
@@ -39,8 +39,8 @@ export default class FloatingLabel extends React.Component<
3939
FloatingLabelProps,
4040
FloatingLabelState
4141
> {
42-
foundation_?: MDCFloatingLabelFoundation;
43-
labelElement_: React.RefObject<HTMLLabelElement> = React.createRef();
42+
foundation!: MDCFloatingLabelFoundation;
43+
labelElement: React.RefObject<HTMLLabelElement> = React.createRef();
4444

4545
static defaultProps: Partial<FloatingLabelProps> = {
4646
className: '',
@@ -55,26 +55,26 @@ export default class FloatingLabel extends React.Component<
5555
this.initializeFoundation();
5656
this.handleWidthChange();
5757
if (this.props.float) {
58-
this.foundation_.float(true);
58+
this.foundation.float(true);
5959
}
6060
}
6161

6262
componentWillUnmount() {
63-
this.foundation_.destroy();
63+
this.foundation.destroy();
6464
}
6565

6666
componentDidUpdate(prevProps: FloatingLabelProps) {
6767
if (this.props.children !== prevProps.children) {
6868
this.handleWidthChange();
6969
}
7070
if (this.props.float !== prevProps.float) {
71-
this.foundation_.float(this.props.float);
71+
this.foundation.float(this.props.float!);
7272
}
7373
}
7474

7575
initializeFoundation = () => {
76-
this.foundation_ = new MDCFloatingLabelFoundation(this.adapter);
77-
this.foundation_.init();
76+
this.foundation = new MDCFloatingLabelFoundation(this.adapter);
77+
this.foundation.init();
7878
};
7979

8080
get classes() {
@@ -83,17 +83,26 @@ export default class FloatingLabel extends React.Component<
8383
return classnames('mdc-floating-label', Array.from(classList), className);
8484
}
8585

86-
get adapter() {
86+
get adapter(): MDCFloatingLabelAdapter {
8787
return {
8888
addClass: (className: string) =>
8989
this.setState({classList: this.state.classList.add(className)}),
9090
removeClass: this.removeClassFromClassList,
91+
// the adapter methods below are effectively useless since React
92+
// handles events and width differently
93+
registerInteractionHandler: () => undefined,
94+
deregisterInteractionHandler: () => undefined,
95+
// Always returns 0 beacuse MDC Web component does
96+
// only proxies to foundation.getWidth.
97+
// MDC React instead passes it from the text-field
98+
// component to floating-label component.
99+
getWidth: () => 0,
91100
};
92101
}
93102

94103
// must be called via ref
95104
shake = () => {
96-
this.foundation_.shake(true);
105+
this.foundation.shake(true);
97106
};
98107

99108
removeClassFromClassList = (className: string) => {
@@ -103,9 +112,8 @@ export default class FloatingLabel extends React.Component<
103112
};
104113

105114
handleWidthChange = () => {
106-
const {handleWidthChange} = this.props;
107-
if (handleWidthChange && this.labelElement_.current) {
108-
handleWidthChange(this.labelElement_.current.offsetWidth);
115+
if (this.props.handleWidthChange && this.labelElement.current) {
116+
this.props.handleWidthChange(this.labelElement.current.offsetWidth);
109117
}
110118
};
111119

@@ -126,7 +134,7 @@ export default class FloatingLabel extends React.Component<
126134
return (
127135
<label
128136
className={this.classes}
129-
ref={this.labelElement_}
137+
ref={this.labelElement}
130138
onAnimationEnd={this.onShakeEnd}
131139
{...otherProps}
132140
>

packages/floating-label/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"url": "https://github.com/material-components/material-components-web-react.git"
1919
},
2020
"dependencies": {
21-
"@material/floating-label": "^0.41.0",
21+
"@material/floating-label": "^1.0.0",
2222
"classnames": "^2.2.6",
2323
"react": "^16.4.2"
2424
},

test/unit/floating-label/index.test.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ import {suite, test} from 'mocha';
44
import {assert} from 'chai';
55
import {mount, shallow} from 'enzyme';
66
import FloatingLabel from '../../../packages/floating-label/index';
7+
import {MDCFloatingLabelAdapter} from '@material/floating-label/adapter';
78
import {coerceForTesting} from '../helpers/types';
89

910
suite('Floating Label');
1011

12+
function getAdapter(instance: FloatingLabel): MDCFloatingLabelAdapter {
13+
// @ts-ignore adapter_ property is protected, we need to bypass this check for testing purposes
14+
return instance.foundation.adapter_;
15+
}
16+
1117
test('classNames adds classes', () => {
1218
const wrapper = shallow(
1319
<FloatingLabel className='test-class-name'>Test</FloatingLabel>
@@ -21,22 +27,22 @@ test('adds text to children', () => {
2127
assert.equal(wrapper.text(), 'Test');
2228
});
2329

24-
test('creates labelElement_', () => {
30+
test('creates labelElement', () => {
2531
const wrapper = mount<FloatingLabel>(<FloatingLabel />);
26-
assert.exists(wrapper.instance().labelElement_.current);
32+
assert.exists(wrapper.instance().labelElement.current);
2733
});
2834

2935
test('#initializeFoundation creates foundation', () => {
3036
const wrapper = shallow<FloatingLabel>(<FloatingLabel />);
31-
assert.exists(wrapper.instance().foundation_);
37+
assert.exists(wrapper.instance().foundation);
3238
});
3339

3440
test('initializing with float to true floats the label', () => {
3541
const wrapper = shallow(<FloatingLabel float />);
3642
assert.isTrue(wrapper.hasClass('mdc-floating-label--float-above'));
3743
});
3844

39-
test('calls handleWidthChange with the offhandleWidthChange of the labelElement_', () => {
45+
test('calls handleWidthChange with the offhandleWidthChange of the labelElement', () => {
4046
const handleWidthChange = coerceForTesting<(width: number) => void>(td.func());
4147
const div = document.createElement('div');
4248
// needs to be attached to real DOM to get width
@@ -95,7 +101,7 @@ test('on animationend should remove the shake class', () => {
95101

96102
test('#adapter.addClass', () => {
97103
const wrapper = mount<FloatingLabel>(<FloatingLabel />);
98-
wrapper.instance().foundation_.adapter_.addClass('test-class-name');
104+
getAdapter(wrapper.instance()).addClass('test-class-name');
99105
assert.isTrue(wrapper.state().classList.has('test-class-name'));
100106
});
101107

@@ -105,14 +111,14 @@ test('#adapter.removeClass', () => {
105111
classList.add('test-class-name');
106112
wrapper.setState({classList});
107113
assert.isTrue(wrapper.state().classList.has('test-class-name'));
108-
wrapper.instance().foundation_.adapter_.removeClass('test-class-name');
114+
getAdapter(wrapper.instance()).removeClass('test-class-name');
109115
assert.isFalse(wrapper.state().classList.has('test-class-name'));
110116
});
111117

112118
test('#componentWillUnmount destroys foundation', () => {
113119
const wrapper = shallow<FloatingLabel>(<FloatingLabel />);
114-
const foundation = wrapper.instance().foundation_;
115-
foundation.destroy = td.func();
120+
const foundation = wrapper.instance().foundation;
121+
foundation.destroy = coerceForTesting<() => void>(td.func());
116122
wrapper.unmount();
117123
td.verify(foundation.destroy());
118124
});

0 commit comments

Comments
 (0)