2017-11-19 01:59:07 +02:00
|
|
|
const React = require('react');
|
2022-08-27 14:36:59 +02:00
|
|
|
import { TouchableOpacity, TouchableWithoutFeedback, Dimensions, Text, Modal, View, LayoutRectangle, ViewStyle, TextStyle } from 'react-native';
|
|
|
|
import { Component } from 'react';
|
2020-11-05 18:58:23 +02:00
|
|
|
const { ItemList } = require('./ItemList.js');
|
2017-11-19 01:59:07 +02:00
|
|
|
|
2022-08-27 14:36:59 +02:00
|
|
|
type ValueType = string;
|
|
|
|
export interface DropdownListItem {
|
|
|
|
label: string;
|
|
|
|
value: ValueType;
|
|
|
|
}
|
2017-11-19 01:59:07 +02:00
|
|
|
|
2022-08-27 14:36:59 +02:00
|
|
|
export type OnValueChangedListener = (newValue: ValueType)=> void;
|
|
|
|
|
|
|
|
interface DropdownProps {
|
|
|
|
listItemStyle?: ViewStyle;
|
|
|
|
itemListStyle?: ViewStyle;
|
|
|
|
itemWrapperStyle?: ViewStyle;
|
|
|
|
headerWrapperStyle?: ViewStyle;
|
|
|
|
headerStyle?: TextStyle;
|
|
|
|
itemStyle?: TextStyle;
|
|
|
|
disabled?: boolean;
|
|
|
|
|
|
|
|
labelTransform?: 'trim';
|
|
|
|
items: DropdownListItem[];
|
2017-11-19 01:59:07 +02:00
|
|
|
|
2022-08-27 14:36:59 +02:00
|
|
|
selectedValue: ValueType|null;
|
|
|
|
onValueChange?: OnValueChangedListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DropdownState {
|
|
|
|
headerSize: LayoutRectangle;
|
|
|
|
listVisible: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Dropdown extends Component<DropdownProps, DropdownState> {
|
|
|
|
private headerRef: TouchableOpacity;
|
|
|
|
|
|
|
|
public constructor(props: DropdownProps) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.headerRef = null;
|
|
|
|
this.state = {
|
2017-11-19 01:59:07 +02:00
|
|
|
headerSize: { x: 0, y: 0, width: 0, height: 0 },
|
|
|
|
listVisible: false,
|
2022-08-27 14:36:59 +02:00
|
|
|
};
|
2017-11-19 01:59:07 +02:00
|
|
|
}
|
|
|
|
|
2022-08-27 14:36:59 +02:00
|
|
|
private updateHeaderCoordinates() {
|
2017-11-19 01:59:07 +02:00
|
|
|
// https://stackoverflow.com/questions/30096038/react-native-getting-the-position-of-an-element
|
2022-08-27 14:36:59 +02:00
|
|
|
this.headerRef.measure((_fx, _fy, width, height, px, py) => {
|
2017-12-08 01:24:14 +02:00
|
|
|
this.setState({
|
2019-07-29 15:43:53 +02:00
|
|
|
headerSize: { x: px, y: py, width: width, height: height },
|
2017-11-19 01:59:07 +02:00
|
|
|
});
|
2017-12-08 01:24:14 +02:00
|
|
|
});
|
2017-11-19 01:59:07 +02:00
|
|
|
}
|
|
|
|
|
2022-08-27 14:36:59 +02:00
|
|
|
public render() {
|
2017-11-19 01:59:07 +02:00
|
|
|
const items = this.props.items;
|
|
|
|
const itemHeight = 60;
|
|
|
|
const windowHeight = Dimensions.get('window').height - 50;
|
|
|
|
|
|
|
|
// Dimensions doesn't return quite the right dimensions so leave an extra gap to make
|
|
|
|
// sure nothing is off screen.
|
|
|
|
const listMaxHeight = windowHeight;
|
2018-12-16 18:18:24 +02:00
|
|
|
const listHeight = Math.min(items.length * itemHeight, listMaxHeight);
|
2017-11-19 01:59:07 +02:00
|
|
|
const maxListTop = windowHeight - listHeight;
|
|
|
|
const listTop = Math.min(maxListTop, this.state.headerSize.y + this.state.headerSize.height);
|
|
|
|
|
|
|
|
const wrapperStyle = {
|
2022-06-21 12:50:10 +02:00
|
|
|
width: this.state.headerSize.width,
|
2017-11-19 01:59:07 +02:00
|
|
|
height: listHeight + 2, // +2 for the border (otherwise it makes the scrollbar appear)
|
|
|
|
marginTop: listTop,
|
2022-06-21 12:50:10 +02:00
|
|
|
marginLeft: this.state.headerSize.x,
|
2017-11-19 01:59:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const itemListStyle = Object.assign({}, this.props.itemListStyle ? this.props.itemListStyle : {}, {
|
|
|
|
borderWidth: 1,
|
|
|
|
borderColor: '#ccc',
|
|
|
|
});
|
|
|
|
|
|
|
|
const itemWrapperStyle = Object.assign({}, this.props.itemWrapperStyle ? this.props.itemWrapperStyle : {}, {
|
2019-07-29 15:43:53 +02:00
|
|
|
flex: 1,
|
2017-11-19 01:59:07 +02:00
|
|
|
justifyContent: 'center',
|
|
|
|
height: itemHeight,
|
|
|
|
paddingLeft: 20,
|
|
|
|
paddingRight: 10,
|
|
|
|
});
|
|
|
|
|
|
|
|
const headerWrapperStyle = Object.assign({}, this.props.headerWrapperStyle ? this.props.headerWrapperStyle : {}, {
|
|
|
|
height: 35,
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
});
|
|
|
|
|
|
|
|
const headerStyle = Object.assign({}, this.props.headerStyle ? this.props.headerStyle : {}, {
|
|
|
|
flex: 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
const headerArrowStyle = Object.assign({}, this.props.headerStyle ? this.props.headerStyle : {}, {
|
|
|
|
flex: 0,
|
2017-11-19 02:23:18 +02:00
|
|
|
marginRight: 10,
|
2017-11-19 01:59:07 +02:00
|
|
|
});
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
const itemStyle = Object.assign({}, this.props.itemStyle ? this.props.itemStyle : {}, {});
|
2017-11-19 01:59:07 +02:00
|
|
|
|
|
|
|
let headerLabel = '...';
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const item = items[i];
|
2017-11-23 20:47:51 +02:00
|
|
|
if (item.value === this.props.selectedValue) {
|
|
|
|
headerLabel = item.label;
|
|
|
|
break;
|
|
|
|
}
|
2017-11-19 01:59:07 +02:00
|
|
|
}
|
|
|
|
|
2022-08-27 14:36:59 +02:00
|
|
|
if (this.props.labelTransform && this.props.labelTransform === 'trim') {
|
|
|
|
headerLabel = headerLabel.trim();
|
|
|
|
}
|
2018-12-16 18:18:24 +02:00
|
|
|
|
2017-11-19 01:59:07 +02:00
|
|
|
const closeList = () => {
|
|
|
|
this.setState({ listVisible: false });
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-19 01:59:07 +02:00
|
|
|
|
2022-08-27 14:36:59 +02:00
|
|
|
const itemRenderer = (item: DropdownListItem) => {
|
2022-09-30 13:13:29 +02:00
|
|
|
const key = item.value ? item.value.toString() : '__null'; // The top item ("Move item to notebook...") has a null value.
|
2017-11-19 01:59:07 +02:00
|
|
|
return (
|
2019-07-29 15:43:53 +02:00
|
|
|
<TouchableOpacity
|
|
|
|
style={itemWrapperStyle}
|
2022-08-27 14:36:59 +02:00
|
|
|
key={key}
|
2019-07-29 15:43:53 +02:00
|
|
|
onPress={() => {
|
|
|
|
closeList();
|
|
|
|
if (this.props.onValueChange) this.props.onValueChange(item.value);
|
|
|
|
}}
|
|
|
|
>
|
2022-08-27 14:36:59 +02:00
|
|
|
<Text ellipsizeMode="tail" numberOfLines={1} style={itemStyle} key={key}>
|
2019-07-29 15:43:53 +02:00
|
|
|
{item.label}
|
|
|
|
</Text>
|
2017-11-19 01:59:07 +02:00
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-19 01:59:07 +02:00
|
|
|
|
|
|
|
return (
|
2019-07-29 15:43:53 +02:00
|
|
|
<View style={{ flex: 1, flexDirection: 'column' }}>
|
|
|
|
<TouchableOpacity
|
|
|
|
style={headerWrapperStyle}
|
2022-08-27 14:36:59 +02:00
|
|
|
ref={ref => (this.headerRef = ref)}
|
2020-05-09 17:01:39 +02:00
|
|
|
disabled={this.props.disabled}
|
2019-07-29 15:43:53 +02:00
|
|
|
onPress={() => {
|
|
|
|
this.updateHeaderCoordinates();
|
|
|
|
this.setState({ listVisible: true });
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Text ellipsizeMode="tail" numberOfLines={1} style={headerStyle}>
|
|
|
|
{headerLabel}
|
|
|
|
</Text>
|
2017-11-20 20:25:23 +02:00
|
|
|
<Text style={headerArrowStyle}>{'▼'}</Text>
|
2017-11-19 01:59:07 +02:00
|
|
|
</TouchableOpacity>
|
2019-07-29 15:43:53 +02:00
|
|
|
<Modal
|
|
|
|
transparent={true}
|
|
|
|
visible={this.state.listVisible}
|
|
|
|
onRequestClose={() => {
|
|
|
|
closeList();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<TouchableWithoutFeedback
|
|
|
|
onPressOut={() => {
|
|
|
|
closeList();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<View style={{ flex: 1 }}>
|
2017-11-19 01:59:07 +02:00
|
|
|
<View style={wrapperStyle}>
|
|
|
|
<ItemList
|
|
|
|
style={itemListStyle}
|
|
|
|
items={this.props.items}
|
|
|
|
itemHeight={itemHeight}
|
2022-08-27 14:36:59 +02:00
|
|
|
itemRenderer={itemRenderer}
|
2017-11-19 01:59:07 +02:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
</Modal>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 14:36:59 +02:00
|
|
|
export default Dropdown;
|
|
|
|
export { Dropdown };
|