2023-01-24 16:33:56 +03:00
|
|
|
import React, { FC, useEffect, useState } from 'react';
|
2021-02-01 08:36:14 +03:00
|
|
|
import classNames from 'classnames';
|
2023-01-24 16:33:56 +03:00
|
|
|
import { Input, Form, InputNumber, Button } from 'antd';
|
2021-02-07 06:38:58 +03:00
|
|
|
import { FieldUpdaterFunc } from '../../types/config-section';
|
|
|
|
import { StatusState } from '../../utils/input-statuses';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { FormStatusIndicator } from './FormStatusIndicator';
|
2023-02-07 19:22:52 +03:00
|
|
|
import { PASSWORD_COMPLEXITY_RULES, REGEX_PASSWORD } from '../../utils/config-constants';
|
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';
|
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export type TextFieldProps = {
|
2021-01-31 11:58:27 +03:00
|
|
|
fieldName: string;
|
2021-01-31 12:38:20 +03:00
|
|
|
|
2021-01-31 11:58:27 +03:00
|
|
|
onSubmit?: () => void;
|
|
|
|
onPressEnter?: () => void;
|
2023-01-24 16:33:56 +03:00
|
|
|
onHandleSubmit?: () => void;
|
2021-01-31 11:58:27 +03:00
|
|
|
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;
|
2023-02-21 18:10:38 +03:00
|
|
|
hasComplexityRequirements?: boolean;
|
2021-01-31 11:58:27 +03:00
|
|
|
onBlur?: FieldUpdaterFunc;
|
|
|
|
onChange?: FieldUpdaterFunc;
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
2021-01-31 11:58:27 +03:00
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export const TextField: FC<TextFieldProps> = ({
|
|
|
|
className,
|
|
|
|
disabled,
|
|
|
|
fieldName,
|
|
|
|
label,
|
|
|
|
maxLength,
|
|
|
|
onBlur,
|
|
|
|
onChange,
|
|
|
|
onPressEnter,
|
2023-01-24 16:33:56 +03:00
|
|
|
onHandleSubmit,
|
2022-09-07 10:00:28 +03:00
|
|
|
pattern,
|
|
|
|
placeholder,
|
|
|
|
required,
|
|
|
|
status,
|
|
|
|
tip,
|
|
|
|
type,
|
|
|
|
useTrim,
|
|
|
|
value,
|
2023-02-21 18:10:38 +03:00
|
|
|
hasComplexityRequirements,
|
2022-09-07 10:00:28 +03:00
|
|
|
}) => {
|
2023-01-28 18:38:33 +03:00
|
|
|
const [hasPwdChanged, setHasPwdChanged] = useState(false);
|
|
|
|
const [showPwdButton, setShowPwdButton] = useState(false);
|
2023-01-24 16:33:56 +03:00
|
|
|
const [form] = Form.useForm();
|
2021-01-31 11:58:27 +03:00
|
|
|
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;
|
2023-01-28 18:38:33 +03:00
|
|
|
setShowPwdButton(true);
|
2023-02-21 18:10:38 +03:00
|
|
|
if (hasComplexityRequirements && REGEX_PASSWORD.test(val)) {
|
2023-01-28 18:38:33 +03:00
|
|
|
setHasPwdChanged(true);
|
2023-01-24 16:33:56 +03:00
|
|
|
} else {
|
2023-01-28 18:38:33 +03:00
|
|
|
setHasPwdChanged(false);
|
2023-01-24 16:33:56 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 09:27:51 +03:00
|
|
|
onChange({ fieldName, value: useTrim ? val.trim() : val });
|
2021-01-31 11:58:27 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-23 17:34:59 +03:00
|
|
|
useEffect(() => {
|
2023-02-07 13:53:23 +03:00
|
|
|
form.setFieldsValue({ inputFieldPassword: value });
|
2023-01-23 17:34:59 +03:00
|
|
|
}, [value]);
|
|
|
|
|
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
|
|
|
};
|
2023-02-07 19:22:52 +03:00
|
|
|
// Password Complexity rules
|
|
|
|
const passwordComplexityRules = [];
|
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) {
|
2023-02-07 19:22:52 +03:00
|
|
|
PASSWORD_COMPLEXITY_RULES.forEach(element => {
|
|
|
|
passwordComplexityRules.push(element);
|
|
|
|
});
|
2021-01-31 11:58:27 +03:00
|
|
|
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}
|
|
|
|
|
2023-02-21 18:10:38 +03:00
|
|
|
{!hasComplexityRequirements ? (
|
2023-01-23 17:34:59 +03:00
|
|
|
<div className="input-side">
|
|
|
|
<div className="input-group">
|
|
|
|
<Field
|
|
|
|
id={fieldId}
|
|
|
|
className={`field ${className} ${fieldId}`}
|
|
|
|
{...fieldProps}
|
|
|
|
{...(type !== TEXTFIELD_TYPE_NUMBER && { allowClear: true })}
|
|
|
|
placeholder={placeholder}
|
|
|
|
maxLength={maxLength}
|
|
|
|
onChange={handleChange}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
onPressEnter={handlePressEnter}
|
|
|
|
disabled={disabled}
|
|
|
|
value={value as number | (readonly string[] & number)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<FormStatusIndicator status={status} />
|
|
|
|
<p className="field-tip">{tip}</p>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="input-side">
|
|
|
|
<div className="input-group">
|
|
|
|
<Form
|
|
|
|
name="basic"
|
|
|
|
form={form}
|
2023-02-07 13:53:23 +03:00
|
|
|
initialValues={{ inputFieldPassword: value }}
|
2023-01-23 17:34:59 +03:00
|
|
|
style={{ width: '100%' }}
|
|
|
|
>
|
2023-02-07 19:22:52 +03:00
|
|
|
<Form.Item name="inputFieldPassword" rules={passwordComplexityRules}>
|
2023-01-23 17:34:59 +03:00
|
|
|
<Input.Password
|
|
|
|
id={fieldId}
|
|
|
|
className={`field ${className} ${fieldId}`}
|
|
|
|
onChange={handleChange}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
placeholder={placeholder}
|
|
|
|
onPressEnter={handlePressEnter}
|
|
|
|
disabled={disabled}
|
|
|
|
value={value as number | (readonly string[] & number)}
|
|
|
|
/>
|
|
|
|
</Form.Item>
|
2023-01-28 18:38:33 +03:00
|
|
|
{showPwdButton && (
|
|
|
|
<div style={{ display: 'flex', flexDirection: 'row-reverse' }}>
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
size="small"
|
|
|
|
className="submit-button"
|
|
|
|
onClick={onHandleSubmit}
|
|
|
|
disabled={!hasPwdChanged}
|
|
|
|
>
|
|
|
|
Update
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
2023-01-23 17:34:59 +03:00
|
|
|
<FormStatusIndicator status={status} />
|
|
|
|
<p className="field-tip">{tip}</p>
|
|
|
|
</Form>
|
|
|
|
</div>
|
2021-01-31 21:13:35 +03:00
|
|
|
</div>
|
2023-01-23 17:34:59 +03:00
|
|
|
)}
|
2021-01-31 11:58:27 +03:00
|
|
|
</div>
|
2021-01-31 12:38:20 +03:00
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
|
|
|
export default TextField;
|
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: '',
|
2022-08-16 03:49:15 +03:00
|
|
|
|
|
|
|
pattern: '',
|
|
|
|
useTrim: false,
|
|
|
|
useTrimLead: false,
|
2023-02-21 18:10:38 +03:00
|
|
|
hasComplexityRequirements: false,
|
2022-08-16 03:49:15 +03:00
|
|
|
|
2021-01-31 11:58:27 +03:00
|
|
|
onSubmit: () => {},
|
|
|
|
onBlur: () => {},
|
|
|
|
onChange: () => {},
|
|
|
|
onPressEnter: () => {},
|
2023-01-24 16:33:56 +03:00
|
|
|
onHandleSubmit: () => {},
|
2021-01-31 11:58:27 +03:00
|
|
|
};
|