react-native-platform-searchbar
Version:
A searchbar component for React Native
170 lines (154 loc) • 6.83 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useRef, useState, forwardRef, useImperativeHandle } from 'react';
import { LayoutAnimation, StyleSheet, TextInput, useWindowDimensions, View } from 'react-native';
import { iosBlue, iosDarkPlaceholderGray, iosLightPlaceholderGray } from '../../../constants/colors';
import SearchIcon from '../../icons/SearchIcon';
import CancelButton from './CancelButton';
import ClearButton from './ClearButton';
const SearchBar = /*#__PURE__*/forwardRef((_ref, ref) => {
let {
value,
theme = 'light',
cancelText = 'Cancel',
cancelTextStyle,
cancelAccessibilityLabel,
clearAccessibilityLabel,
returnKeyType = 'search',
selectionColor = iosBlue,
placeholderTextColor = theme === 'light' ? iosLightPlaceholderGray : iosDarkPlaceholderGray,
iconColor = placeholderTextColor,
leftIcon,
style,
inputStyle,
children,
onFocus,
onChangeText,
onCancel,
onClear
} = _ref,
props = _objectWithoutProperties(_ref, ["value", "theme", "cancelText", "cancelTextStyle", "cancelAccessibilityLabel", "clearAccessibilityLabel", "returnKeyType", "selectionColor", "placeholderTextColor", "iconColor", "leftIcon", "style", "inputStyle", "children", "onFocus", "onChangeText", "onCancel", "onClear"]);
const {
fontScale
} = useWindowDimensions();
const styles = theme === 'light' ? defaultStyles : darkStyles;
const [cancelButtonVisible, setCancelButtonVisible] = useState(false);
const inputRef = useRef(null);
useImperativeHandle(ref, () => inputRef.current);
const handleFocus = e => {
LayoutAnimation.configureNext(_objectSpread(_objectSpread({}, LayoutAnimation.Presets.easeInEaseOut), {}, {
duration: 300
}));
setCancelButtonVisible(true);
if (onFocus) {
onFocus(e);
}
};
const handleClear = () => {
if (onClear) {
onClear();
} // somehow using inputRef.current.clear() doesn't work
onChangeText('');
};
const handleCancel = () => {
if (onCancel) {
onCancel();
}
if (inputRef.current) {
handleClear();
inputRef.current.blur();
}
LayoutAnimation.configureNext(_objectSpread(_objectSpread({}, LayoutAnimation.Presets.easeInEaseOut), {}, {
duration: 300
}));
setCancelButtonVisible(false);
};
return /*#__PURE__*/React.createElement(View, {
style: [styles.wrapper, style]
}, /*#__PURE__*/React.createElement(View, {
style: styles.inputWrapper
}, /*#__PURE__*/React.createElement(TextInput, _extends({
ref: inputRef,
value: value,
clearButtonMode: "never",
autoCorrect: false,
onChangeText: onChangeText,
onFocus: handleFocus,
returnKeyType: returnKeyType,
placeholderTextColor: placeholderTextColor,
selectionColor: selectionColor,
accessibilityRole: "search",
accessibilityTraits: "search"
}, props, {
style: [styles.input, {
paddingHorizontal: 25 + 10 * fontScale
}, inputStyle]
})), /*#__PURE__*/React.createElement(View, {
pointerEvents: "box-none",
style: styles.children
}, leftIcon ? /*#__PURE__*/React.createElement(View, {
style: styles.leftIcon
}, leftIcon) : /*#__PURE__*/React.createElement(SearchIcon, {
color: iconColor,
style: styles.leftIcon
}), children, value ? /*#__PURE__*/React.createElement(ClearButton, {
color: iconColor,
onPress: handleClear,
accessibilityLabel: clearAccessibilityLabel,
style: styles.clearButton
}) : undefined)), /*#__PURE__*/React.createElement(CancelButton, {
text: cancelText,
visible: cancelButtonVisible,
onPress: handleCancel,
style: styles.cancelButton,
textStyle: cancelTextStyle,
accessibilityLabel: cancelAccessibilityLabel
}));
});
const defaultStyles = StyleSheet.create({
wrapper: {
flexDirection: 'row',
alignItems: 'center'
},
inputWrapper: {
flex: 1
},
input: {
backgroundColor: '#E3E3E9',
borderRadius: 12,
paddingVertical: 10
},
children: {
position: 'absolute',
flexDirection: 'row',
width: '100%',
height: '100%',
alignItems: 'center'
},
cancelButton: {
marginLeft: 10
},
clearButton: {
height: '40%',
aspectRatio: 1,
marginRight: 10
},
leftIcon: {
marginLeft: 10,
marginRight: 'auto',
height: '50%',
aspectRatio: 1
}
});
const darkStyles = StyleSheet.create(_objectSpread(_objectSpread({}, defaultStyles), {}, {
input: _objectSpread(_objectSpread({}, defaultStyles.input), {}, {
backgroundColor: '#1c1c1f',
color: '#FFF'
})
}));
export default SearchBar;
//# sourceMappingURL=index.js.map