|
| 1 | +import React from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import classnames from 'classnames' |
| 4 | + |
| 5 | +import { withTheme } from '../theme' |
| 6 | +import { BaseComponent } from '../tailwind' |
| 7 | + |
| 8 | +// https://material.io/tools/icons/?style=baseline |
| 9 | +const ExpandMore = props => ( |
| 10 | + <svg |
| 11 | + xmlns="http://www.w3.org/2000/svg" |
| 12 | + width="24" |
| 13 | + height="24" |
| 14 | + viewBox="0 0 24 24" |
| 15 | + {...props} |
| 16 | + > |
| 17 | + <path d="M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6z" /> |
| 18 | + <path d="M0 0h24v24H0z" fill="none" /> |
| 19 | + </svg> |
| 20 | +) |
| 21 | + |
| 22 | +const TextInput = ({ |
| 23 | + theme, |
| 24 | + is, |
| 25 | + field, |
| 26 | + children, |
| 27 | + className, |
| 28 | + id, |
| 29 | + name, |
| 30 | + type, |
| 31 | + disabled, |
| 32 | + readOnly, |
| 33 | + invalid, |
| 34 | + placeholder, |
| 35 | + options, |
| 36 | + icon, |
| 37 | + ...rest |
| 38 | +}) => { |
| 39 | + const describedBy = [field.errorId, field.helpId].filter(by => by) |
| 40 | + const isInvalid = field.invalid || invalid |
| 41 | + |
| 42 | + const ChevronDown = icon |
| 43 | + |
| 44 | + return ( |
| 45 | + <div className={`relative mb-${theme.spacing.sm}`}> |
| 46 | + <BaseComponent |
| 47 | + is={is} |
| 48 | + className={classnames( |
| 49 | + 'appearance-none', |
| 50 | + disabled && 'pointer-events-none', |
| 51 | + className, |
| 52 | + )} |
| 53 | + bg="white" |
| 54 | + rounded={theme.radius} |
| 55 | + text={theme.textColors.body} |
| 56 | + p={{ l: theme.spacing.md, r: theme.spacing.lg, y: theme.spacing.sm }} |
| 57 | + border={!isInvalid ? true : [true, theme.brandColors.danger]} |
| 58 | + w="full" |
| 59 | + leading="tight" |
| 60 | + opacity={disabled ? 50 : undefined} |
| 61 | + id={field.inputId || id || name} |
| 62 | + name={name} |
| 63 | + type={type} |
| 64 | + disabled={field.disabled || disabled} |
| 65 | + readOnly={readOnly} |
| 66 | + aria-invalid={isInvalid || undefined} |
| 67 | + aria-describedby={ |
| 68 | + describedBy.length ? describedBy.join(' ') : undefined |
| 69 | + } |
| 70 | + {...rest} |
| 71 | + > |
| 72 | + {!!placeholder && <option value="">{placeholder}</option>} |
| 73 | + {options.map(option => ( |
| 74 | + <option key={`${name}-${option.value}`} value={option.value}> |
| 75 | + {option.label} |
| 76 | + </option> |
| 77 | + ))} |
| 78 | + </BaseComponent> |
| 79 | + <div |
| 80 | + className={classnames( |
| 81 | + 'pointer-events-none', |
| 82 | + 'absolute', |
| 83 | + 'pin-y pin-r', |
| 84 | + 'flex items-center', |
| 85 | + `px-${theme.spacing.sm}`, |
| 86 | + )} |
| 87 | + > |
| 88 | + <ChevronDown className="h-6 w-6" /> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + ) |
| 92 | +} |
| 93 | + |
| 94 | +TextInput.propTypes = { |
| 95 | + theme: PropTypes.shape({}).isRequired, |
| 96 | + is: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.object]), |
| 97 | + field: PropTypes.shape({ |
| 98 | + inputId: PropTypes.string, |
| 99 | + invalid: PropTypes.bool, |
| 100 | + disabled: PropTypes.bool, |
| 101 | + }), |
| 102 | + children: PropTypes.node, |
| 103 | + className: PropTypes.string, |
| 104 | + id: PropTypes.string, |
| 105 | + name: PropTypes.string.isRequired, |
| 106 | + type: PropTypes.string, |
| 107 | + disabled: PropTypes.bool, |
| 108 | + readOnly: PropTypes.bool, |
| 109 | + invalid: PropTypes.bool, |
| 110 | + placeholder: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), |
| 111 | + icon: PropTypes.node, |
| 112 | + options: PropTypes.arrayOf( |
| 113 | + PropTypes.shape({ |
| 114 | + label: PropTypes.string, |
| 115 | + value: PropTypes.string, |
| 116 | + }), |
| 117 | + ), |
| 118 | +} |
| 119 | + |
| 120 | +TextInput.defaultProps = { |
| 121 | + is: 'select', |
| 122 | + field: {}, |
| 123 | + children: undefined, |
| 124 | + className: undefined, |
| 125 | + id: undefined, |
| 126 | + type: 'text', |
| 127 | + disabled: false, |
| 128 | + readOnly: false, |
| 129 | + invalid: false, |
| 130 | + placeholder: 'Please select', |
| 131 | + icon: ExpandMore, |
| 132 | + options: [], |
| 133 | +} |
| 134 | + |
| 135 | +export { TextInput as component } |
| 136 | +export default withTheme(TextInput) |
0 commit comments