All files / app/ui/form DynamicInput.tsx

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  111x 111x 111x   111x   111x 111x   111x 111x 111x                     111x 586x 586x 586x 586x 586x 586x 586x 586x 586x 586x   586x 586x                     586x   60x                           120x 120x                     1x       1x 2x 2x                           1x       1x 2x 2x                           1x                                         1x                         522x                               111x  
import { FormFieldItem } from '@bigcommerce/checkout-sdk';
import { isDate, noop } from 'lodash';
import React, { memo, useCallback, FunctionComponent } from 'react';
import ReactDatePicker from 'react-datepicker';
 
import { withDate, WithDateProps } from '../../locale';
 
import CheckboxInput from './CheckboxInput';
import DynamicFormFieldType from './DynamicFormFieldType';
import { InputProps } from './Input';
import RadioInput from './RadioInput';
import TextArea from './TextArea';
import TextInput from './TextInput';
 
export interface DynamicInputProps extends InputProps {
    id: string;
    additionalClassName?: string;
    value?: string | string[];
    rows?: number;
    fieldType?: DynamicFormFieldType;
    options?: FormFieldItem[];
}
 
const DynamicInput: FunctionComponent<DynamicInputProps & WithDateProps> = ({
    additionalClassName,
    date,
    fieldType,
    id,
    name,
    onChange = noop,
    options,
    placeholder,
    value,
    ...rest
}) => {
    const { inputFormat } = date;
    const handleDateChange = useCallback((dateValue, event) => onChange({
        ...event,
        target: {
            name,
            value: dateValue,
        },
    }), [
        onChange,
        name,
    ]);
 
    switch (fieldType) {
    case DynamicFormFieldType.dropdown:
        return (
            <select
                { ...rest as any }
                className="form-select optimizedCheckout-form-select"
                data-test={ `${id}-select` }
                id={ id }
                name={ name }
                onChange={ onChange }
                value={ value === null ? '' : value }
            >
                { placeholder &&
                    <option value="">
                        { placeholder }
                    </option> }
                { options && options.map(({ label, value: optionValue }) =>
                    <option
                        key={ optionValue }
                        value={ optionValue }
                    >
                        { label }
                    </option>
                ) }
            </select>
        );
 
    case DynamicFormFieldType.radio:
        Iif (!options || !options.length) {
            return null;
        }
 
        return <>
            { options.map(({ label, value: optionValue }) =>
                <RadioInput
                    { ...rest }
                    checked={ optionValue === value }
                    id={ `${id}-${optionValue}` }
                    key={ optionValue }
                    label={ label }
                    name={ name }
                    onChange={ onChange }
                    testId={ `${id}-${optionValue}-radio` }
                    value={ optionValue }
                />) }
        </>;
 
    case DynamicFormFieldType.checkbox:
        Iif (!options || !options.length) {
            return null;
        }
 
        return <>
            { options.map(({ label, value: optionValue }) =>
                <CheckboxInput
                    { ...rest }
                    checked={ Array.isArray(value) ? value.includes(optionValue) : false }
                    id={ `${id}-${optionValue}` }
                    key={ optionValue }
                    label={ label }
                    name={ name }
                    onChange={ onChange }
                    testId={ `${id}-${optionValue}-checkbox` }
                    value={ optionValue }
                />) }
        </>;
 
    case DynamicFormFieldType.date:
        return (
            <ReactDatePicker
                { ...rest as any }
                autoComplete="off"
                // FIXME: we can avoid this by simply using onChangeRaw, but it's not being triggered properly
                // https://github.com/Hacker0x01/react-datepicker/issues/1357
                // onChangeRaw={ rest.onChange }
                calendarClassName="optimizedCheckout-contentPrimary"
                className="form-input optimizedCheckout-form-input"
                dateFormat={ inputFormat }
                maxDate={ rest.max ? new Date(`${rest.max}T00:00:00Z`) : undefined }
                minDate={ rest.min ? new Date(`${rest.min}T00:00:00Z`) : undefined }
                name={ name }
                onChange={ handleDateChange }
                placeholderText={ inputFormat.toUpperCase() }
                popperClassName="optimizedCheckout-contentPrimary"
                selected={ isDate(value) ? value : undefined }
            />
        );
 
    case DynamicFormFieldType.multiline:
        return (
            <TextArea
                { ...rest as any }
                id={ id }
                name={ name }
                onChange={ onChange }
                testId={ `${id}-text` }
                type={ fieldType }
                value={ value }
            />
        );
 
    default:
        return (
            <TextInput
                { ...rest }
                id={ id }
                name={ name }
                onChange={ onChange }
                testId={ `${id}-${ fieldType === DynamicFormFieldType.password ?
                    'password' :
                    'text' }` }
                type={ fieldType }
                value={ value }
            />
        );
    }
};
 
export default memo(withDate(DynamicInput));