2021-04-02 04:16:26 +00:00
|
|
|
import React from 'react';
|
2021-09-06 08:50:38 +00:00
|
|
|
import { resolveReferences, resolveWidgetFieldValue, validateWidget } from '@/_helpers/utils';
|
|
|
|
|
import escapeStringRegexp from 'escape-string-regexp';
|
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-04-30 08:10:57 +00:00
|
|
|
onComponentOptionChanged
|
2021-04-30 06:31:32 +00:00
|
|
|
}) {
|
2021-04-04 13:11:17 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
const validationData = validateWidget({
|
|
|
|
|
validationObject: component.definition.validation,
|
|
|
|
|
widgetValue: value,
|
|
|
|
|
currentState
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const { isValid, validationError } = validationData;
|
|
|
|
|
|
2021-08-30 11:43:05 +00:00
|
|
|
try {
|
|
|
|
|
parsedWidgetVisibility = resolveReferences(parsedWidgetVisibility, currentState, []);
|
|
|
|
|
} 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}
|
|
|
|
|
onClick={event => {event.stopPropagation(); onComponentClick(id, component)}}
|
|
|
|
|
onChange={(e) => 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' }}
|
|
|
|
|
/>
|
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
|
|
|
};
|