2021-09-13 18:04:07 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-09-06 08:50:38 +00:00
|
|
|
import { resolveReferences, resolveWidgetFieldValue, validateWidget } from '@/_helpers/utils';
|
2021-04-02 04:16:26 +00:00
|
|
|
|
2021-04-30 06:31:32 +00:00
|
|
|
export const TextInput = function TextInput({
|
|
|
|
|
id,
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
component,
|
|
|
|
|
onComponentClick,
|
|
|
|
|
currentState,
|
2021-09-16 12:01:22 +00:00
|
|
|
onComponentOptionChanged,
|
2021-04-30 06:31:32 +00:00
|
|
|
}) {
|
|
|
|
|
const placeholder = component.definition.properties.placeholder.value;
|
2021-09-02 14:11:59 +00:00
|
|
|
const widgetVisibility = component.definition.styles?.visibility?.value ?? true;
|
|
|
|
|
const disabledState = component.definition.styles?.disabledState?.value ?? false;
|
|
|
|
|
|
2021-09-16 12:01:22 +00:00
|
|
|
const parsedDisabledState =
|
|
|
|
|
typeof disabledState !== 'boolean' ? resolveWidgetFieldValue(disabledState, currentState) : disabledState;
|
2021-08-30 11:43:05 +00:00
|
|
|
|
|
|
|
|
let parsedWidgetVisibility = widgetVisibility;
|
2021-09-06 08:50:38 +00:00
|
|
|
const value = currentState?.components[component?.name]?.value;
|
2021-09-07 05:11:57 +00:00
|
|
|
const currentValidState = currentState?.components[component?.name]?.isValid;
|
2021-09-16 12:01:22 +00:00
|
|
|
|
2021-09-13 18:04:07 +00:00
|
|
|
const [text, setText] = useState(value);
|
|
|
|
|
|
|
|
|
|
const textProperty = component.definition.properties.value;
|
|
|
|
|
let newText = value;
|
|
|
|
|
if (textProperty && currentState) {
|
|
|
|
|
newText = resolveReferences(textProperty.value, currentState, '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setText(newText);
|
|
|
|
|
onComponentOptionChanged(component, 'value', newText);
|
|
|
|
|
}, [newText]);
|
2021-09-06 08:50:38 +00:00
|
|
|
|
|
|
|
|
const validationData = validateWidget({
|
|
|
|
|
validationObject: component.definition.validation,
|
|
|
|
|
widgetValue: value,
|
2021-09-16 12:01:22 +00:00
|
|
|
currentState,
|
|
|
|
|
});
|
2021-09-06 08:50:38 +00:00
|
|
|
|
|
|
|
|
const { isValid, validationError } = validationData;
|
|
|
|
|
|
2021-09-16 12:01:22 +00:00
|
|
|
if (currentValidState !== isValid) {
|
2021-09-07 05:11:57 +00:00
|
|
|
onComponentOptionChanged(component, 'isValid', isValid);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 11:43:05 +00:00
|
|
|
try {
|
|
|
|
|
parsedWidgetVisibility = resolveReferences(parsedWidgetVisibility, currentState, []);
|
2021-09-16 12:01:22 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
2021-04-04 13:11:17 +00:00
|
|
|
|
2021-04-30 06:31:32 +00:00
|
|
|
return (
|
2021-09-06 08:50:38 +00:00
|
|
|
<div>
|
|
|
|
|
<input
|
|
|
|
|
disabled={parsedDisabledState}
|
2021-09-16 12:01:22 +00:00
|
|
|
onClick={(event) => {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
onComponentClick(id, component);
|
|
|
|
|
}}
|
2021-09-13 18:04:07 +00:00
|
|
|
onChange={(e) => {
|
|
|
|
|
setText(e.target.value);
|
|
|
|
|
onComponentOptionChanged(component, 'value', e.target.value);
|
|
|
|
|
}}
|
2021-09-06 08:50:38 +00:00
|
|
|
type="text"
|
|
|
|
|
className={`form-control ${!isValid ? 'is-invalid' : ''} validation-without-icon`}
|
|
|
|
|
placeholder={placeholder}
|
2021-09-16 12:01:22 +00:00
|
|
|
style={{ width, height, display: parsedWidgetVisibility ? '' : 'none' }}
|
2021-09-13 18:04:07 +00:00
|
|
|
value={text}
|
2021-09-06 08:50:38 +00:00
|
|
|
/>
|
2021-09-06 14:32:09 +00:00
|
|
|
<div className="invalid-feedback">{validationError}</div>
|
2021-09-06 08:50:38 +00:00
|
|
|
</div>
|
2021-04-30 06:31:32 +00:00
|
|
|
);
|
2021-04-02 04:16:26 +00:00
|
|
|
};
|