Skip to content

Commit

Permalink
fix(tab): update readme and ref name in tab component (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Goo authored Aug 22, 2018
1 parent fedf138 commit f18fda1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ matrix:
- docker pull mdcreact/screenshots
- docker image ls
script:
- docker run -it --rm --cap-add=SYS_ADMIN -e MDC_GCLOUD_SERVICE_ACCOUNT_KEY="${MDC_GCLOUD_SERVICE_ACCOUNT_KEY}" mdcreact/screenshots /bin/sh -c "git fetch; git checkout \"${CURRENT_BRANCH}\"; git pull; npm i; /home/pptruser/material-components-web-react/test/screenshot/start.sh; sleep 300s; npm run test:image-diff"
- docker run -it --rm --cap-add=SYS_ADMIN -e MDC_GCLOUD_SERVICE_ACCOUNT_KEY="${MDC_GCLOUD_SERVICE_ACCOUNT_KEY}" mdcreact/screenshots /bin/sh -c "git fetch; git checkout \"${CURRENT_BRANCH}\"; git pull; npm i; /home/pptruser/material-components-web-react/test/screenshot/start.sh; sleep 330s; npm run test:image-diff"
43 changes: 28 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"line-ripple",
"ripple",
"top-app-bar",
"tab",
"tab-scroller",
"infrastructure",
"tab-indicator",
"text-field"
Expand Down
33 changes: 5 additions & 28 deletions packages/tab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ class MyApp extends React.Component {

render() {
return (
<Tab
active={this.state.active}
// this will be another tab's clientRect object
previousActiveClientRect={previousTabClientRect}
>
<Tab active={this.state.active}>
<MaterialIcon className='mdc-tab__icon' icon='favorite' />
<span className='mdc-tab__text-label'>Love</span>
</Tab>
Expand All @@ -51,7 +47,7 @@ class MyApp extends React.Component {

#### With Custom Indicator

Possibly you don't want to use the default underline indicator, but instead would like to use an icon. You'll need to add an `indicator` prop, which is a function that returns a `<TabIndicator />` element.
Possibly you don't want to use the default underline indicator, but instead would like to use an icon. You'll need to add an `indicatorContent` prop, which should be set to an icon element.

```js
import React from 'react';
Expand All @@ -65,32 +61,12 @@ class MyApp extends React.Component {
return (
<Tab
active={this.state.active}
previousActiveClientRect={previousTabClientRect}
indicator={this.renderIndicator}
indicatorContent={<MaterialIcon icon='favorite' />}
>
<span className='mdc-tab__text-label'>Love</span>
</Tab>
);
}

renderIndicator(props) {
// must return a <TabIndicator /> element
return (
<TabIndicator
icon
{/*--
You need to pass
active, ref, and previousIndicatorClientRect props to the
TabIndicator element
--*/}
active={props.active}
ref={props.ref}
previousIndicatorClientRect={props.previousIndicatorClientRect}
>
<MaterialIcon icon='favorite' />
</TabIndicator>
);
}
}
```

Expand All @@ -101,9 +77,10 @@ Prop Name | Type | Description
active | boolean | If true will activate the tab and indicator.
className | string | Classes to appear on className attribute of root element.
isFadingIndicator | boolean | Enables a fading indicator, instead of sliding (default).
indicator | function | Function that is passed props as an argument, that must return a `<TabIndicator />` element. The `<TabIndicator />` element must be passed `active`, `ref`, and `previousIndicatorClientRect` props. See example above.
indicatorContent | element | Element that will appear within the `<TabIndicator />` element.
minWidth | boolean | If true will display the `<Tab />` as narrow as possible.
isMinWidthIndicator | boolean | If true will display the `<TabIndicator />` to the size of the longest content element.
isIconIndicator | boolean | If true will display the indicator content in the center of the tab.
previousIndicatorClientRect | ClientRect | The indicator's clientRect that was previously activated.
onTransitionEnd | function | transitionend event callback handler.

Expand Down
8 changes: 4 additions & 4 deletions packages/tab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class Tab extends Component {
foundation_ = null;
tabElement_ = React.createRef();
tabContentElement_ = React.createRef();
tabIndicatorElement_ = React.createRef();
tabIndicator_ = React.createRef();

state = {
'classList': new Set(),
Expand Down Expand Up @@ -77,8 +77,8 @@ export default class Tab extends Component {
}

computeIndicatorClientRect = () => {
if (!this.tabIndicatorElement_.current) return;
return this.tabIndicatorElement_.current.computeContentClientRect();
if (!this.tabIndicator_.current) return;
return this.tabIndicator_.current.computeContentClientRect();
}

computeDimensions = () => {
Expand Down Expand Up @@ -143,7 +143,7 @@ export default class Tab extends Component {
icon={isIconIndicator}
active={active}
fade={isFadingIndicator}
ref={this.tabIndicatorElement_}
ref={this.tabIndicator_}
previousIndicatorClientRect={previousActiveClientRect}
>
{indicatorContent}
Expand Down
8 changes: 4 additions & 4 deletions test/unit/tab/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ test('#adapter.focus focuses the tabElement_', () => {
td.verify(wrapper.instance().tabElement_.current.focus(), {times: 1});
});

test('#computeIndicatorClientRect returns the tabIndicatorElement_ clientRect', () => {
test('#computeIndicatorClientRect returns the tabIndicator_ clientRect', () => {
const wrapper = mount(<Tab/>);
wrapper.instance().tabIndicatorElement_.current.computeContentClientRect = td.func();
wrapper.instance().tabIndicator_.current.computeContentClientRect = td.func();
wrapper.instance().computeIndicatorClientRect();
td.verify(wrapper.instance().tabIndicatorElement_.current.computeContentClientRect(), {times: 1});
td.verify(wrapper.instance().tabIndicator_.current.computeContentClientRect(), {times: 1});
});

test('#computeDimensions calls foundation.computeDimensions', () => {
Expand Down Expand Up @@ -244,7 +244,7 @@ test('custom tabIndicator should render with a ref attached', () => {
indicatorContent={<i className='icon'>icon</i>}
/>);

assert.instanceOf(wrapper.instance().tabIndicatorElement_.current, TabIndicator);
assert.instanceOf(wrapper.instance().tabIndicator_.current, TabIndicator);
});

test('isMinWidthIndicator renders indicator within the content element', () => {
Expand Down

0 comments on commit f18fda1

Please sign in to comment.