react-native-scrollable-tab-view icon indicating copy to clipboard operation
react-native-scrollable-tab-view copied to clipboard

Tab view always has the height equal to height of the highest tab

Open you-fail-me opened this issue 10 years ago • 38 comments

How can I make it adopt active tab's height?

you-fail-me avatar Sep 06 '16 13:09 you-fail-me

I don't understand. Please provide more information. Pictures are very useful

skv-headless avatar Sep 07 '16 08:09 skv-headless

@skv-headless Let's say the first tab has a height of 200px and the second one has a height of 1000px. As a result every tab will have the height of 1000px.

Sample video: http://recordit.co/sKuRC94nsm

Index.js

<View style={styles.container}>
     <ScrollableTabView initialPage={0} scrollWithoutAnimation={false} style={{marginTop: 20, }}>
          <Matrix tabLabel='Do First' important='true' urgent='true' dataSource={this.state.dataDoFirst} style={styles.matrix} />
          <Matrix tabLabel='Schedule' important='true' urgent='false' dataSource={this.state.dataSource} style={styles.matrix} />
          <Matrix tabLabel='Delegate' important='false' urgent='true' dataSource={this.state.dataSource} style={styles.matrix} />
          <Matrix tabLabel='Do Later' important='false' urgent='false' dataSource={this.state.dataSource} style={styles.matrix} />
     </ScrollableTabView>
     <Button style={styles.button} onPress={this.logout.bind(this)}>Logout</Button>
</View>

Matrix.js

<View style={styles.container}>
     <View style={styles.todoListWrapper}>
          <View style={styles.title}>
            <Text style={styles.welcome}>
              Do First
            </Text>
          </View>
          <ListView
            dataSource={this.state.dataSource}
            renderRow={this.renderRow.bind(this)}/>
        </View>
        <View style={styles.newTask}>
          <Text styl={styles.newTaskInput}>
            Hallo
         </Text>
     </View>
</View>

vscholz avatar Sep 08 '16 15:09 vscholz

@skv-headless yes, @vscholz is right. sorry for the delay( And as a result if you have a <View>, containing tabs, and a <NextView /> after it, that <NextView /> comes after unused free space if a smaller tab is active.

Example markup (think that tabs have height, as said in their tabLabels):

<View>
  <ScrollableTabView>
        <ReactPage tabLabel="200px_high" />
        <FlowPage tabLabel="500px_high" />
        <JestPage tabLabel="1000px_high" />
    </ScrollableTabView>
</View>
<NextView />

The whole <View> will have a height of 1000px, disregarding which tab is active. If e.g. <ReactPage> is active there will be 800px of unused free space under its content, which is gonna create a gap between the <View> and <NextView />

you-fail-me avatar Sep 08 '16 15:09 you-fail-me

Has anybody found any fixes for this?

roscioli avatar Oct 05 '16 22:10 roscioli

Anyone?

denizs avatar Nov 14 '16 23:11 denizs

Ok here's how I did it. It's a hot fix, but I'll revisit this topic on a later stage and make a PR as I'm in a hurry right now. So let's dive into it:

I made use of the onChangeTab callback, that is already provided out of the box. The callback function takes an object which has the current slide's ref, which is equal to the tabLabel prop and will fire whenever a tab is changed. Yaay!

1st

Set your Tab Components ref names respectively to your tabLabels like so:


<View ref='someTabName' tabLabel="someTabName" />

2nd

We want to apply the calculated height to our SrollableTabView. As the style prop is already passed to the relevant component, we can simply keep a style object in our parent component's state and pass it to our TabView:

//constructor
constructor(props){
    super(props)
    this.state ={
      tabViewStyle:{
       // Some style you don't care for just to init something
        backgroundColor:'transparent'
      },
     // I'll explain that in a bit
      firstTabSwitch: true
    }
  }
// Component:

<ScrollableTabView
           onChangeTab={(obj) => this._handleTabHeight(obj)}
           style={this.state.tabViewStyle}
           prerenderingSiblingsNumber={0} 
          {{...other}}  />

A few things to note here:

On init, we don't pass any height to our style in order to let the TabView expand to the it's initial view's height rather than manually setting the height on mount. On the other hand, this means we have to stop our other - maybe larger views - from rendering. We achieve this by setting prerenderingSiblingsNumber={0}. Result: Our ScrollableTabView renders with the height of the initial view. As we haven't set any height yet, it won't depend whether your second tab is shorter or taller - it will adapt to the height of the current view.

HOWEVER: We have set the prop prerenderingSiblingsNumber={0} meaning, that on first tab change, the new view will have a height of 0, causing the TabView to collapse. That's what the firstTabSwitchis for. We'll use this to prevent our callback function to set the height to 0 on first render.

Lastly

Ok let's write our callback function. As we already defined our refs, we can now access the height like so:


_handleTabHeight(obj){
    this.refs[obj.ref.props.tabLabel].measure(this._setTabHeight.bind(this))
  }

  _setTabHeight(ox, oy, width, height, px, py) {
    if(!this.state.firstTabSwitch){
      this.setState({tabViewStyle: {height: height}})
    } else{
      this.setState({firstTabSwitch:false})
    }
  }

Oddly, you can't simply call measure on obj.ref but then who cares - 10 minute fix 💯 Hope that helps :)

denizs avatar Nov 15 '16 15:11 denizs

@denizs your fix works well (thanks) except that if I keep switching between the tabs the height get's smaller and smaller up the point where everything is gone. :(

mxmtsk avatar Dec 08 '16 17:12 mxmtsk

Up this issue.

I tried the proposed solution but it seems the onLayout measurements in those sub views still return the height of the highest tab.

Kowaio avatar Jan 10 '17 12:01 Kowaio

Try this workaround. Use ScrollView as tabs.

<ScrollableTabView style={{ height: Dimensions.get('window').height }}>
  <ScrollView>
    <View style={{ height: 300, backgroundColor: 'cadetblue' }} />
  </ScrollView>

  <SrcollView>
    <View style={{ height: 5000, backgroundColor: 'lightgreen' }} />
  </ScrollView>
</ScrollableTabView>

Note that the ScrollableTabView height can be also expressed with flexbox values or some calculation result. I'm using Dimensions becuase I guess it's the most common desired effect.

sospedra avatar Jan 11 '17 11:01 sospedra

Hello @sospedra, Thanks for your answer. I tried with ScrollView instead of View but it didn't changed much since the content of those tabs is dynamic. Therefore I can't set a fixed height as in your example. And like I said above, even when adding onLayout measurement of those inner views, the ScrollableTableView will keep the highest height of its siblings.

Kowaio avatar Jan 11 '17 12:01 Kowaio

Hey @Kowaio ,

The fixed height is fot the container, aka the ScrollableTabView. Not the tabs, aka the ScrollView. I don't know how's your code but it'll be -- probably -- more than just replace View by the scrollable version.

Mayebe if you post a rnplay.org example I could help you better ;)

sospedra avatar Jan 12 '17 12:01 sospedra

Thanks again @sospedra. Yeah sorry, I was talking about your height in your Views content.

About setting a fixed height for the ScrollableTabView, it's unfortunately impossible since this ScrollableTabView is itself inside a ScrollView.

Here's a draft of my current code: http://www.heypasteit.com/clip/3ASU

Moreover, using your code above as is, (except that all that is already in a parent <ScrollView) gives me this result: http://cl.kowaio.me/292O3y2u0Y0r As you can see, after swiping to the second tab, when coming back to the first, the content height was the same as the second tab (the highest).

Kowaio avatar Jan 12 '17 12:01 Kowaio

Ok so I dug into this issue a little bit: My approach was to use the onLayout and onChangeTab event of every scene component to calculate the height of the displayed scene and change the contentContainerStyle respectively on every swipe. However, it seems that the StaticContainer Component used in SceneComponent.js is causing the container to take the full height of the parent container resulting in a missing onLayout event, as long as one tab does not exceed the current tab view's height (hence the height of the tallest component). @skv-headless is there any good reason to stick to StaticContainer? Otherwise I would start working on a fix for that. I found that replacing the StaticContainer with a View component works fine regarding the onLayoutevent. Best, D

denizs avatar Jan 29 '17 09:01 denizs

Solved by use @denizs method.

But use setTimeout(fn, 1) to ensure get correct height.

Lxxyx avatar Mar 03 '17 16:03 Lxxyx

Awesome @Lxxyx!

denizs avatar Mar 06 '17 08:03 denizs

There will be any fix on the package or the only fix is with the @denizs 's hotfix ?

eduduardo avatar Mar 20 '17 14:03 eduduardo

If you have problems with StaticContainer you can try prerenderingSiblingsNumber={Infinity}.

skv-headless avatar Mar 21 '17 06:03 skv-headless

Anyone? Is there an official method of doing this? Can this be implemented as a feature?

sankalpsingha avatar Apr 07 '17 14:04 sankalpsingha

main.js

  _handleTabHeight(obj){
    console.log(this.refs[obj.ref.props.tabLabel].state.name)
    this.refs[obj.ref.props.tabLabel].gogogo((x,y,w,h,l,t) => {
      if(h!==0){
        this.setState({tabViewStyle: {height: h}})
      }
    })
  }
        <ScrollableTabView
           onChangeTab={(obj) => this._handleTabHeight(obj)}
           style={this.state.tabViewStyle}
        >
            <ReactPage ref='high1' tabLabel="high1" />
            <ReactPage2 ref='high2' tabLabel="high2" />
            <ReactPage3 ref='high3' tabLabel="high3" />
        </ScrollableTabView>

ReactPage.js/ReactPage2.js/ReactPage3.js

  gogogo(cb) {
      this.refs.test.measure(cb)
  }
  render() {
    return (
      <View
        ref='test'
        style={{
          height: 300,
          backgroundColor: 'blue'
        }}
      >
      </View>
    );
  }
}

worked~!

laclys avatar Sep 08 '17 09:09 laclys

Why didn't this issue come up with a new solution

aijason avatar Dec 09 '17 11:12 aijason