All files / src/Input Input.tsx

84.62% Statements 22/26
66.67% Branches 8/12
75% Functions 6/8
84.62% Lines 22/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319                                                                                                                                            3x   76x   76x               76x             76x       76x                                 76x               76x         3x   76x   76x 76x       76x                                                                                         3x                                                                                       22x   22x   22x               22x                                 22x                                                                                                     3x   22x 22x                            
import * as React from 'react';
import { Box as ReakitBox } from 'reakit';
// @ts-ignore
import InputMask from 'react-input-mask';
import ConditionalWrap from 'conditional-wrap';
 
import { Size } from '../types';
import { useClassName, createComponent, createElement, createHook, pickCSSProps, omitCSSProps } from '../utils';
import { Box, BoxProps } from '../Box';
import { FieldWrapper, FieldWrapperProps } from '../FieldWrapper';
import { Group } from '../Group';
import { Icon, IconProps } from '../Icon';
import { Spinner } from '../Spinner';
 
import * as styles from './styles';
 
export type LocalInputProps = {
  after?: React.ReactElement<any>;
  autoComplete?: string;
  /** Automatically focus on the input */
  autoFocus?: boolean;
  before?: React.ReactElement<any>;
  /** Default value of the input */
  defaultValue?: string | string[];
  /** Disables the input */
  disabled?: boolean;
  /** Adds a cute loading indicator to the input field */
  isLoading?: boolean;
  /** Makes the input required and sets aria-invalid to true */
  isRequired?: boolean;
  /** Name of the input field */
  name?: string;
  /** Alters the size of the input. Can be "small", "medium" or "large" */
  size?: Size;
  mask?: string;
  /** The maximum (numeric or date-time) value for the input. Must not be less than its minimum (min attribute) value. */
  max?: number | string;
  /** If the value of the type attribute is text, email, search, password, tel, or url, this attribute specifies the maximum number of characters (in UTF-16 code units) that the user can enter. For other control types, it is ignored. */
  maxLength?: number;
  /** The minimum (numeric or date-time) value for this input, which must not be greater than its maximum (max attribute) value. */
  min?: number | string;
  /** If the value of the type attribute is text, email, search, password, tel, or url, this attribute specifies the minimum number of characters (in UTF-16 code points) that the user can enter. For other control types, it is ignored. */
  minLength?: number;
  /** This prop indicates whether the user can enter more than one value. This attribute only applies when the type attribute is set to email or file. */
  multiple?: boolean;
  /** Regex pattern to apply to the input */
  pattern?: string;
  /** Hint text to display */
  placeholder?: string;
  /** This prop prevents the user from modifying the value of the input. It is ignored if the value of the type attribute is hidden, range, color, checkbox, radio, file, or a button type (such as button or submit). */
  readOnly?: boolean;
  /** Setting the value of this attribute to true indicates that the element needs to have its spelling and grammar checked. The value default indicates that the element is to act according to a default behavior, possibly based on the parent element's own spellcheck value. The value false indicates that the element should not be checked. */
  spellCheck?: boolean;
  /** Works with the min and max attributes to limit the increments at which a numeric or date-time value can be set. */
  step?: number | string;
  /** State of the input. Can be any color in the palette. */
  state?: string;
  /** Specify the type of input. */
  type?: string;
  /** Value of the input */
  value?: string | number | string[];
  /** Function to invoke when focus is lost */
  onBlur?: React.FocusEventHandler<HTMLInputElement>;
  /** Function to invoke when input has changed */
  onChange?: React.FormEventHandler<HTMLInputElement>;
  /** Function to invoke when input is focused */
  onFocus?: React.FocusEventHandler<HTMLInputElement>;
};
export type InputProps = BoxProps & LocalInputProps;
 
const useProps = createHook<InputProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const { before, after, isLoading, isRequired, state, ...restProps } = props;
 
    const wrapperClassName = useClassName({
      style: styles.InputWrapper,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Wrapper',
      prevClassName: restProps.className
    });
    const spinnerClassName = useClassName({
      style: styles.InputSpinner,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Spinner'
    });
    const boxProps = Box.useProps({
      ...omitCSSProps(restProps),
      className: undefined,
      wrapElement: children => (
        <Box className={wrapperClassName} {...pickCSSProps(props)}>
          {before && (
            <Box display="inline-flex" position="absolute" zIndex="3">
              {before}
            </Box>
          )}
          {after && (
            <Box display="inline-flex" position="absolute" right="0" zIndex="3">
              {after}
            </Box>
          )}
          {children}
          {isLoading && <Spinner className={spinnerClassName} color="text" />}
        </Box>
      )
    });
 
    const className = useClassName({
      style: styles.Input,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
 
    return { ...boxProps, className, 'aria-invalid': state === 'danger', 'aria-required': isRequired };
  },
  { defaultProps: { type: 'text' }, themeKey: 'Input' }
);
 
export const Input = createComponent<InputProps>(
  props => {
    const inputProps = useProps(props);
 
    let use = props.use;
    Iif (props.mask) {
      use = InputMask;
    }
 
    return createElement({
      children: props.children,
      component: ReakitBox,
      use,
      htmlProps: { ...inputProps, ...(props.mask ? { mask: props.mask } : {}) }
    });
  },
  {
    attach: {
      useProps
    },
    defaultProps: {
      use: 'input'
    },
    themeKey: 'Input'
  }
);
 
////////////////////////////////////////////////////////////////
 
export function InputIcon(props: IconProps) {
  const className = useClassName({
    style: styles.InputIcon,
    styleProps: props,
    themeKey: 'Input.Icon',
    prevClassName: props.className
  });
 
  return <Icon {...props} className={className} />;
}
 
////////////////////////////////////////////////////////////////
 
export type LocalInputFieldProps = {
  /** Addon component to the input (before). Similar to the addon components in Input. */
  addonBefore?: React.ReactElement<any>;
  /** Addon component to the input (after). Similar to the addon components in Input. */
  addonAfter?: React.ReactElement<any>;
  /** Additional props for the Input component */
  inputProps?: InputProps;
  /** If addonBefore or addonAfter exists, then the addons will render vertically. */
  orientation?: 'vertical' | 'horizontal';
};
export type InputFieldProps = BoxProps & FieldWrapperProps & InputProps & LocalInputFieldProps;
 
const useInputFieldProps = createHook<InputFieldProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const {
      addonAfter,
      addonBefore,
      children,
      after,
      autoComplete,
      autoFocus,
      before,
      defaultValue,
      description,
      disabled,
      hint,
      inputProps,
      isLoading,
      isOptional,
      isRequired,
      orientation,
      label,
      name,
      size,
      mask,
      max,
      maxLength,
      min,
      minLength,
      multiple,
      pattern,
      placeholder,
      readOnly,
      spellCheck,
      step,
      state,
      tooltip,
      tooltipTriggerComponent,
      type,
      value,
      onBlur,
      onChange,
      onFocus,
      overrides,
      validationText,
      ...restProps
    } = props;
 
    const boxProps = Box.useProps(restProps);
 
    const className = useClassName({
      style: styles.InputField,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
 
    return {
      ...boxProps,
      className,
      children: (
        <FieldWrapper
          description={description}
          hint={hint}
          isOptional={isOptional}
          isRequired={isRequired}
          label={label}
          overrides={overrides}
          state={state}
          tooltip={tooltip}
          tooltipTriggerComponent={tooltipTriggerComponent}
          validationText={validationText}
        >
          {({ elementProps }) => (
            <ConditionalWrap
              condition={addonBefore || addonAfter}
              wrap={(children: React.ReactNode) => (
                <Group orientation={orientation} overrides={overrides}>
                  {children}
                </Group>
              )}
            >
              {addonBefore}
              <Input
                after={after}
                autoComplete={autoComplete}
                autoFocus={autoFocus}
                before={before}
                defaultValue={defaultValue}
                disabled={disabled}
                isLoading={isLoading}
                isRequired={isRequired}
                name={name}
                size={size}
                mask={mask}
                max={max}
                maxLength={maxLength}
                min={min}
                minLength={minLength}
                multiple={multiple}
                pattern={pattern}
                placeholder={placeholder}
                readOnly={readOnly}
                spellCheck={spellCheck}
                step={step}
                state={state}
                type={type}
                value={value}
                onBlur={onBlur}
                onChange={onChange}
                onFocus={onFocus}
                overrides={overrides}
                {...elementProps}
                {...inputProps}
              />
              {addonAfter}
            </ConditionalWrap>
          )}
        </FieldWrapper>
      )
    };
  },
  { defaultProps: { orientation: 'horizontal' }, themeKey: 'InputField' }
);
 
export const InputField = createComponent<InputFieldProps>(
  props => {
    const inputFieldProps = useInputFieldProps(props);
    return createElement({
      children: props.children,
      component: ReakitBox,
      use: props.use,
      htmlProps: inputFieldProps
    });
  },
  {
    attach: {
      useProps
    },
    themeKey: 'InputField'
  }
);