|
| 1 | +import React, { ChangeEventHandler, forwardRef, HTMLAttributes } from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import classNames from 'classnames' |
| 4 | +// import { CFormLabel } from './CFormLabel' |
| 5 | + |
| 6 | +export interface CFormInputProps extends HTMLAttributes<HTMLInputElement> { |
| 7 | + /** |
| 8 | + * A string of all className you want applied to the component. [docs] |
| 9 | + */ |
| 10 | + className?: string |
| 11 | + /** |
| 12 | + * Toggle the disabled state for the component. [docs] |
| 13 | + */ |
| 14 | + disabled?: boolean |
| 15 | + /** |
| 16 | + * Set component validation state to invalid. [docs] |
| 17 | + */ |
| 18 | + invalid?: boolean |
| 19 | + // label?: string | ReactNode |
| 20 | + /** |
| 21 | + * Method called immediately after the `value` prop changes. [docs] |
| 22 | + */ |
| 23 | + onChange?: ChangeEventHandler<HTMLInputElement> |
| 24 | + /** |
| 25 | + * Render the component styled as plain text. Removes the default form field styling and preserve the correct margin and padding. Recommend to use only along side `readonly` [docs] |
| 26 | + */ |
| 27 | + plainText?: boolean |
| 28 | + /** |
| 29 | + * Toggle the readonly state for the component. [docs] |
| 30 | + */ |
| 31 | + readOnly?: boolean |
| 32 | + /** |
| 33 | + * Size the component small or large. [docs] |
| 34 | + * |
| 35 | + * @type 'sm' | 'lg' |
| 36 | + */ |
| 37 | + size?: 'sm' | 'lg' |
| 38 | + /** |
| 39 | + * Specifies the type of component. [docs] |
| 40 | + * |
| 41 | + * @type 'color' | 'file' | 'text' | string |
| 42 | + * @default 'text' |
| 43 | + */ |
| 44 | + type?: 'color' | 'file' | 'text' | string |
| 45 | + /** |
| 46 | + * Set component validation state to valid. [docs] |
| 47 | + */ |
| 48 | + valid?: boolean |
| 49 | + /** |
| 50 | + * The `value` attribute of component. [docs] |
| 51 | + * |
| 52 | + * @controllable onChange |
| 53 | + * */ |
| 54 | + value?: string | string[] | number |
| 55 | +} |
| 56 | + |
| 57 | +export const CFormInput = forwardRef<HTMLInputElement, CFormInputProps>( |
| 58 | + ({ children, className, invalid, plainText, size, type = 'text', valid, ...rest }, ref) => { |
| 59 | + const _className = classNames( |
| 60 | + plainText ? 'form-control-plaintext' : 'form-control', |
| 61 | + { |
| 62 | + [`form-control-${size}`]: size, |
| 63 | + 'form-control-color': type === 'color', |
| 64 | + 'is-invalid': invalid, |
| 65 | + 'is-valid': valid, |
| 66 | + }, |
| 67 | + className, |
| 68 | + ) |
| 69 | + return ( |
| 70 | + <input type={type} className={_className} {...rest} ref={ref}> |
| 71 | + {children} |
| 72 | + </input> |
| 73 | + ) |
| 74 | + }, |
| 75 | +) |
| 76 | + |
| 77 | +CFormInput.propTypes = { |
| 78 | + children: PropTypes.node, |
| 79 | + className: PropTypes.string, |
| 80 | + invalid: PropTypes.bool, |
| 81 | + plainText: PropTypes.bool, |
| 82 | + size: PropTypes.oneOf(['sm', 'lg']), |
| 83 | + type: PropTypes.oneOfType([PropTypes.oneOf(['color', 'file', 'text']), PropTypes.string]), |
| 84 | + valid: PropTypes.bool, |
| 85 | +} |
| 86 | + |
| 87 | +CFormInput.displayName = 'CFormInput' |
0 commit comments