ToolJet/frontend/src/Editor/Components/TextInput.jsx

77 lines
2.3 KiB
React
Raw Normal View History

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