2021-01-31 11:58:27 +03:00
|
|
|
import React from 'react';
|
2021-02-01 08:36:14 +03:00
|
|
|
import classNames from 'classnames';
|
2021-01-31 11:58:27 +03:00
|
|
|
import { Input, InputNumber } from 'antd';
|
2021-02-07 06:38:58 +03:00
|
|
|
import { FieldUpdaterFunc } from '../../types/config-section';
|
2021-02-01 08:36:14 +03:00
|
|
|
// import InfoTip from '../info-tip';
|
2021-02-07 06:38:58 +03:00
|
|
|
import { StatusState } from '../../utils/input-statuses';
|
2021-02-01 11:36:27 +03:00
|
|
|
import FormStatusIndicator from './form-status-indicator';
|
2021-01-31 11:58:27 +03:00
|
|
|
|
|
|
|
export const TEXTFIELD_TYPE_TEXT = 'default';
|
|
|
|
export const TEXTFIELD_TYPE_PASSWORD = 'password'; // Input.Password
|
|
|
|
export const TEXTFIELD_TYPE_NUMBER = 'numeric'; // InputNumber
|
|
|
|
export const TEXTFIELD_TYPE_TEXTAREA = 'textarea'; // Input.TextArea
|
|
|
|
export const TEXTFIELD_TYPE_URL = 'url';
|
|
|
|
|
|
|
|
export interface TextFieldProps {
|
|
|
|
fieldName: string;
|
2021-01-31 12:38:20 +03:00
|
|
|
|
2021-01-31 11:58:27 +03:00
|
|
|
onSubmit?: () => void;
|
|
|
|
onPressEnter?: () => void;
|
|
|
|
|
|
|
|
className?: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
label?: string;
|
|
|
|
maxLength?: number;
|
2021-05-23 09:27:51 +03:00
|
|
|
pattern?: string;
|
2021-01-31 11:58:27 +03:00
|
|
|
placeholder?: string;
|
|
|
|
required?: boolean;
|
|
|
|
status?: StatusState;
|
|
|
|
tip?: string;
|
|
|
|
type?: string;
|
2021-05-23 09:27:51 +03:00
|
|
|
useTrim?: boolean;
|
2021-07-03 23:52:37 +03:00
|
|
|
useTrimLead?: boolean;
|
2021-01-31 11:58:27 +03:00
|
|
|
value?: string | number;
|
|
|
|
onBlur?: FieldUpdaterFunc;
|
|
|
|
onChange?: FieldUpdaterFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function TextField(props: TextFieldProps) {
|
|
|
|
const {
|
|
|
|
className,
|
|
|
|
disabled,
|
|
|
|
fieldName,
|
|
|
|
label,
|
|
|
|
maxLength,
|
|
|
|
onBlur,
|
|
|
|
onChange,
|
|
|
|
onPressEnter,
|
2021-05-23 09:27:51 +03:00
|
|
|
pattern,
|
2021-01-31 11:58:27 +03:00
|
|
|
placeholder,
|
|
|
|
required,
|
|
|
|
status,
|
|
|
|
tip,
|
|
|
|
type,
|
2021-05-23 09:27:51 +03:00
|
|
|
useTrim,
|
2021-01-31 11:58:27 +03:00
|
|
|
value,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const handleChange = (e: any) => {
|
|
|
|
// if an extra onChange handler was sent in as a prop, let's run that too.
|
|
|
|
if (onChange) {
|
2021-05-23 09:27:51 +03:00
|
|
|
const val = type === TEXTFIELD_TYPE_NUMBER ? e : e.target.value;
|
|
|
|
onChange({ fieldName, value: useTrim ? val.trim() : val });
|
2021-01-31 11:58:27 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// if you blur a required field with an empty value, restore its original value in state (parent's state), if an onChange from parent is available.
|
|
|
|
const handleBlur = (e: any) => {
|
|
|
|
const val = e.target.value;
|
|
|
|
if (onBlur) {
|
|
|
|
onBlur({ value: val });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handlePressEnter = () => {
|
|
|
|
if (onPressEnter) {
|
|
|
|
onPressEnter();
|
|
|
|
}
|
2021-01-31 12:38:20 +03:00
|
|
|
};
|
2021-01-31 11:58:27 +03:00
|
|
|
|
|
|
|
// display the appropriate Ant text field
|
2021-01-31 12:38:20 +03:00
|
|
|
let Field = Input as
|
|
|
|
| typeof Input
|
|
|
|
| typeof InputNumber
|
|
|
|
| typeof Input.TextArea
|
|
|
|
| typeof Input.Password;
|
2021-01-31 11:58:27 +03:00
|
|
|
let fieldProps = {};
|
|
|
|
if (type === TEXTFIELD_TYPE_TEXTAREA) {
|
|
|
|
Field = Input.TextArea;
|
|
|
|
fieldProps = {
|
|
|
|
autoSize: true,
|
|
|
|
};
|
|
|
|
} else if (type === TEXTFIELD_TYPE_PASSWORD) {
|
|
|
|
Field = Input.Password;
|
|
|
|
fieldProps = {
|
|
|
|
visibilityToggle: true,
|
|
|
|
};
|
|
|
|
} else if (type === TEXTFIELD_TYPE_NUMBER) {
|
|
|
|
Field = InputNumber;
|
|
|
|
fieldProps = {
|
|
|
|
type: 'number',
|
|
|
|
min: 1,
|
2021-01-31 12:38:20 +03:00
|
|
|
max: 10 ** maxLength - 1,
|
2021-01-31 11:58:27 +03:00
|
|
|
};
|
|
|
|
} else if (type === TEXTFIELD_TYPE_URL) {
|
|
|
|
fieldProps = {
|
|
|
|
type: 'url',
|
2021-05-23 09:27:51 +03:00
|
|
|
pattern,
|
2021-01-31 11:58:27 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const fieldId = `field-${fieldName}`;
|
|
|
|
|
2021-02-01 08:36:14 +03:00
|
|
|
const { type: statusType } = status || {};
|
2021-01-31 11:58:27 +03:00
|
|
|
|
2021-02-01 08:36:14 +03:00
|
|
|
const containerClass = classNames({
|
2021-02-13 10:55:59 +03:00
|
|
|
'formfield-container': true,
|
2021-02-01 08:36:14 +03:00
|
|
|
'textfield-container': true,
|
|
|
|
[`type-${type}`]: true,
|
|
|
|
required,
|
|
|
|
[`status-${statusType}`]: status,
|
|
|
|
});
|
2021-05-23 09:27:51 +03:00
|
|
|
|
2021-01-31 11:58:27 +03:00
|
|
|
return (
|
2021-02-01 08:36:14 +03:00
|
|
|
<div className={containerClass}>
|
2021-01-31 21:13:35 +03:00
|
|
|
{label ? (
|
|
|
|
<div className="label-side">
|
2021-02-13 10:55:59 +03:00
|
|
|
<label htmlFor={fieldId} className="formfield-label">
|
2021-02-01 08:36:14 +03:00
|
|
|
{label}
|
2021-01-31 21:13:35 +03:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
<div className="input-side">
|
|
|
|
<div className="input-group">
|
|
|
|
<Field
|
|
|
|
id={fieldId}
|
|
|
|
className={`field ${className} ${fieldId}`}
|
|
|
|
{...fieldProps}
|
2021-05-23 09:27:51 +03:00
|
|
|
{...(type !== TEXTFIELD_TYPE_NUMBER && { allowClear: true })}
|
2021-01-31 21:13:35 +03:00
|
|
|
placeholder={placeholder}
|
|
|
|
maxLength={maxLength}
|
|
|
|
onChange={handleChange}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
onPressEnter={handlePressEnter}
|
|
|
|
disabled={disabled}
|
2021-02-07 06:38:58 +03:00
|
|
|
value={value as number | (readonly string[] & number)}
|
2021-01-31 21:13:35 +03:00
|
|
|
/>
|
|
|
|
</div>
|
2021-02-01 11:36:27 +03:00
|
|
|
<FormStatusIndicator status={status} />
|
2021-02-13 10:55:59 +03:00
|
|
|
<p className="field-tip">{tip}</p>
|
2021-01-31 11:58:27 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-01-31 12:38:20 +03:00
|
|
|
);
|
2021-01-31 11:58:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TextField.defaultProps = {
|
|
|
|
className: '',
|
|
|
|
disabled: false,
|
|
|
|
label: '',
|
2021-02-07 06:38:58 +03:00
|
|
|
maxLength: 255,
|
2021-01-31 11:58:27 +03:00
|
|
|
|
|
|
|
placeholder: '',
|
|
|
|
required: false,
|
|
|
|
status: null,
|
|
|
|
tip: '',
|
|
|
|
type: TEXTFIELD_TYPE_TEXT,
|
|
|
|
value: '',
|
|
|
|
onSubmit: () => {},
|
|
|
|
onBlur: () => {},
|
|
|
|
onChange: () => {},
|
|
|
|
onPressEnter: () => {},
|
|
|
|
};
|