Skip to content

Commit

Permalink
TimeList: add prop preSelectedItem / List: scoll preSelectedItem to t…
Browse files Browse the repository at this point in the history
…op of list
  • Loading branch information
Martin Schwier committed Aug 11, 2016
1 parent c2f35da commit 7478b88
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export default React.createClass({

messages: React.PropTypes.shape({
emptyList: CustomPropTypes.message
})
}),

scrollToTop: React.PropTypes.bool
},


Expand All @@ -49,12 +51,13 @@ export default React.createClass({
data: [],
messages: {
emptyList: 'There are no items in this list'
}
},
scrollToTop: false
}
},

componentDidMount(){
this.move()
this.move(this.props.scrollToTop)
},

componentDidUpdate(){
Expand Down Expand Up @@ -131,14 +134,14 @@ export default React.createClass({
return this.props.data
},

move(){
move(scrollToTop = false){
var list = compat.findDOMNode(this)
, idx = this._data().indexOf(this.props.focused)
, selected = list.children[idx];

if( !selected ) return

notify(this.props.onMove, [ selected, list, this.props.focused ])
notify(this.props.onMove, [ selected, list, this.props.focused, scrollToTop ])
}

})
9 changes: 6 additions & 3 deletions src/TimeList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export default React.createClass({
onSelect: React.PropTypes.func,
preserveDate: React.PropTypes.bool,
culture: React.PropTypes.string,
delay: React.PropTypes.number
delay: React.PropTypes.number,
preSelectedItem: React.PropTypes.instanceOf(Date)
},

mixins: [
Expand All @@ -39,13 +40,14 @@ export default React.createClass({
max: new Date(2099, 11, 31),
preserveDate: true,
delay: 300,
ariaActiveDescendantKey: 'timelist'
ariaActiveDescendantKey: 'timelist',
preSelectedItem: null
}
},

getInitialState(){
var data = this._dates(this.props)
, focusedItem = this._closestDate(data, this.props.value);
, focusedItem = this._closestDate(data, this.props.value ? this.props.value : this.props.preSelectedItem);

return {
focusedItem: focusedItem || data[0],
Expand Down Expand Up @@ -86,6 +88,7 @@ export default React.createClass({
selected={date}
onSelect={onSelect}
focused={this.state.focusedItem}
scrollToTop={!!this.props.preSelectedItem}
/>
)
},
Expand Down
4 changes: 2 additions & 2 deletions src/mixins/PopupScrollToMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import scrollTo from 'dom-helpers/util/scrollTo';

export default {

_scrollTo(selected, list, focused) {
_scrollTo(selected, list, focused, scrollToTop = false) {
var state = this._scrollState || (this._scrollState = {})
, handler = this.props.onMove
, lastVisible = state.visible
Expand All @@ -20,7 +20,7 @@ export default {
handler(selected, list, focused)
else {
state.scrollCancel && state.scrollCancel()
state.scrollCancel = scrollTo(selected, list)
state.scrollCancel = scrollTo(selected, list, scrollToTop)
}
}
}
Expand Down

0 comments on commit 7478b88

Please sign in to comment.