UNPKG

@prosperitainova/dumbo-react-native

Version:
179 lines (177 loc) 5.03 kB
"use strict"; import React from 'react'; import { View, TextInput as ReactTextInput, StyleSheet } from 'react-native'; import { styleReferenceBreaker } from '../../helpers'; import { getColor } from '../../styles/colors'; import { Button } from '../Button'; import { Text } from '../Text'; import CloseIcon from '@carbon/icons/es/close/20'; import SearchIcon from '@carbon/icons/es/search/20'; import { defaultText } from '../../constants/defaultText'; import { getTextInputStyle } from '../BaseTextInputs'; /** Props for Search component */ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; /** * Search component for rendering a search input * * {@link https://github.com/carbon-design-system/carbon-react-native/blob/main/example/src/Views/Search.tsx | Example code} */ export class Search extends React.Component { state = { hasFocus: false }; get localStyles() { return StyleSheet.create({ textBoxArea: { flexDirection: 'row' }, textBoxWrapper: { flex: 1 } }); } get styles() { const { light } = this.props; return getTextInputStyle(light); } onFocus = event => { const { onFocus } = this.props; if (typeof onFocus === 'function') { onFocus(event); } this.setState({ hasFocus: true }); }; onBlur = event => { const { onBlur } = this.props; if (typeof onBlur === 'function') { onBlur(event); } this.setState({ hasFocus: false }); }; onChange = value => { const { onChangeText } = this.props; if (typeof onChangeText === 'function') { onChangeText(value); } this.setState({}); }; get clearText() { const { disabled, clearTextButtonText, value, onTextClearPress } = this.props; const clearTextCallback = event => { if (typeof onTextClearPress === 'function') { onTextClearPress(value, event); } this.onChange(''); }; return /*#__PURE__*/_jsx(Button, { overrideColor: disabled ? getColor('iconDisabled') : getColor('iconSecondary'), disabled: disabled, style: this.styles.passwordRevealButton, iconOnlyMode: true, kind: "ghost", icon: CloseIcon, text: clearTextButtonText || defaultText.clear, onPress: clearTextCallback }); } get searchIcon() { const { disabled, searchIconText, onSubmitEditing } = this.props; const finalStyle = styleReferenceBreaker(this.styles.passwordRevealButton); finalStyle.right = undefined; finalStyle.left = 0; finalStyle.top = 1; return /*#__PURE__*/_jsx(Button, { overrideColor: disabled ? getColor('iconDisabled') : getColor('iconSecondary'), disabled: disabled, style: finalStyle, iconOnlyMode: true, kind: "ghost", icon: SearchIcon, text: searchIconText || defaultText.search, onPress: onSubmitEditing }); } render() { const { label, value, autoCorrect, autoCapitalize, placeholder, maxLength, onSubmitEditing, componentProps, style, disabled, labelBreakMode, buttonProps } = this.props; const { hasFocus } = this.state; let textBoxStyle = styleReferenceBreaker(this.styles.textBox); if (disabled) { textBoxStyle = styleReferenceBreaker(this.styles.textBoxDisabled); } else if (hasFocus) { textBoxStyle = styleReferenceBreaker(this.styles.textBoxActive); } textBoxStyle.paddingLeft = 50; textBoxStyle.paddingRight = 50; return /*#__PURE__*/_jsxs(View, { style: styleReferenceBreaker(this.styles.wrapper, style || {}), accessibilityLabel: label, children: [!!label && /*#__PURE__*/_jsx(Text, { style: this.styles.label, type: "label-02", text: label, breakMode: labelBreakMode }), /*#__PURE__*/_jsxs(View, { style: this.localStyles.textBoxArea, children: [/*#__PURE__*/_jsxs(View, { style: styleReferenceBreaker(this.styles.textBoxWrapper, this.localStyles.textBoxWrapper), accessibilityLabel: label, children: [/*#__PURE__*/_jsx(ReactTextInput, { editable: !disabled, autoCapitalize: autoCapitalize, style: textBoxStyle, value: value, onSubmitEditing: onSubmitEditing, onChangeText: this.onChange, autoCorrect: autoCorrect, placeholder: placeholder, placeholderTextColor: getColor('textPlaceholder'), onBlur: this.onBlur, onFocus: this.onFocus, maxLength: maxLength, textAlignVertical: "top", ...(componentProps || {}) }), this.searchIcon, !!value && this.clearText] }), !!buttonProps && /*#__PURE__*/_jsx(Button, { ...buttonProps })] })] }); } } //# sourceMappingURL=index.js.map