react-native-platform-searchbar
Version:
A searchbar component for React Native
130 lines (120 loc) • 5.66 kB
JavaScript
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 _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 _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, { forwardRef, useRef, useImperativeHandle } from 'react';
import { View, TextInput, StyleSheet } from 'react-native';
import SearchIcon from '../../icons/SearchIcon';
import { androidLightPlaceholderGray, androidDarkPlaceholderGray } from '../../../constants/colors';
import ClearIcon from '../../icons/ClearIconAndroid';
import Button from '../../Button';
const SearchBar = /*#__PURE__*/forwardRef((_ref, ref) => {
let {
value,
theme = 'light',
placeholderTextColor = theme === 'light' ? androidLightPlaceholderGray : androidDarkPlaceholderGray,
iconColor = placeholderTextColor,
selectionColor = theme === 'light' ? androidLightPlaceholderGray : androidDarkPlaceholderGray,
clearAccessibilityLabel,
leftIcon,
style,
inputStyle,
children,
onChangeText,
onClear
} = _ref,
props = _objectWithoutProperties(_ref, ["value", "theme", "placeholderTextColor", "iconColor", "selectionColor", "clearAccessibilityLabel", "leftIcon", "style", "inputStyle", "children", "onChangeText", "onClear"]);
const styles = theme === 'light' ? defaultStyles : darkStyles;
const inputRef = useRef(null);
useImperativeHandle(ref, () => inputRef.current);
const handleClear = () => {
if (onClear) {
onClear();
}
onChangeText('');
};
return /*#__PURE__*/React.createElement(View, {
style: [styles.wrapper, style]
}, /*#__PURE__*/React.createElement(TextInput, _extends({
ref: inputRef,
value: value,
selectionColor: selectionColor,
placeholderTextColor: placeholderTextColor,
onChangeText: onChangeText,
clearButtonMode: "never",
autoCorrect: false,
accessibilityRole: "search",
accessibilityTraits: "search"
}, props, {
style: [styles.input, 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(Button, {
onPress: handleClear,
hitSlop: 8,
accessibilityLabel: clearAccessibilityLabel,
style: styles.clearButton
}, /*#__PURE__*/React.createElement(ClearIcon, {
color: iconColor,
style: styles.clearIcon
})) : undefined));
});
const defaultStyles = StyleSheet.create({
wrapper: {
flexDirection: 'row',
alignItems: 'center'
},
input: {
flex: 1,
backgroundColor: '#FFF',
paddingHorizontal: 40,
paddingVertical: 12,
borderRadius: 4,
fontSize: 16,
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 2
},
shadowOpacity: 0.22,
shadowRadius: 2.22,
elevation: 3
},
children: {
position: 'absolute',
flexDirection: 'row',
width: '100%',
height: '100%',
alignItems: 'center',
elevation: 3
},
leftIcon: {
height: '40%',
aspectRatio: 1,
marginLeft: 10,
marginRight: 'auto'
},
clearButton: {
marginRight: 10
},
clearIcon: {
height: '40%',
aspectRatio: 1
}
});
const darkStyles = StyleSheet.create(_objectSpread(_objectSpread({}, defaultStyles), {}, {
input: _objectSpread(_objectSpread({}, defaultStyles.input), {}, {
backgroundColor: '#28282d',
color: '#FFF'
})
}));
export default SearchBar;
//# sourceMappingURL=index.js.map