2022-09-28 12:21:09 +00:00
|
|
|
import React, { useEffect, useState, useRef } from 'react';
|
2021-04-02 04:16:26 +00:00
|
|
|
|
2022-07-15 10:23:35 +00:00
|
|
|
export const TextInput = function TextInput({
|
|
|
|
|
height,
|
|
|
|
|
validate,
|
|
|
|
|
properties,
|
|
|
|
|
styles,
|
|
|
|
|
setExposedVariable,
|
|
|
|
|
fireEvent,
|
|
|
|
|
registerAction,
|
2022-09-05 08:36:45 +00:00
|
|
|
component,
|
2022-09-12 08:02:58 +00:00
|
|
|
darkMode,
|
2022-07-15 10:23:35 +00:00
|
|
|
}) {
|
2022-09-28 12:21:09 +00:00
|
|
|
const textInputRef = useRef();
|
|
|
|
|
|
|
|
|
|
const [disable, setDisable] = useState(styles.disabledState);
|
2021-12-14 03:40:42 +00:00
|
|
|
const [value, setValue] = useState(properties.value);
|
2022-09-28 12:21:09 +00:00
|
|
|
const [visibility, setVisibility] = useState(styles.visibility);
|
2021-12-14 03:40:42 +00:00
|
|
|
const { isValid, validationError } = validate(value);
|
2021-09-13 18:04:07 +00:00
|
|
|
|
2022-10-26 05:15:43 +00:00
|
|
|
const computedStyles = {
|
|
|
|
|
height,
|
|
|
|
|
borderRadius: `${styles.borderRadius}px`,
|
|
|
|
|
color: darkMode && styles.textColor === '#000' ? '#fff' : styles.textColor,
|
|
|
|
|
borderColor: styles.borderColor,
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-28 12:21:09 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
disable !== styles.disabledState && setDisable(styles.disabledState);
|
|
|
|
|
}, [styles.disabledState]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
visibility !== styles.visibility && setVisibility(styles.visibility);
|
|
|
|
|
}, [styles.visibility]);
|
|
|
|
|
|
2021-09-13 18:04:07 +00:00
|
|
|
useEffect(() => {
|
2021-12-14 03:40:42 +00:00
|
|
|
setExposedVariable('isValid', isValid);
|
2021-09-21 13:48:28 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2021-12-14 03:40:42 +00:00
|
|
|
}, [isValid]);
|
2022-09-28 12:21:09 +00:00
|
|
|
|
2021-12-14 03:40:42 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
setValue(properties.value);
|
|
|
|
|
setExposedVariable('value', properties.value);
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [properties.value]);
|
2022-07-02 12:30:30 +00:00
|
|
|
|
2022-09-28 12:21:09 +00:00
|
|
|
registerAction('setFocus', async function () {
|
|
|
|
|
textInputRef.current.focus();
|
|
|
|
|
});
|
|
|
|
|
registerAction('setBlur', async function () {
|
|
|
|
|
textInputRef.current.blur();
|
|
|
|
|
});
|
|
|
|
|
registerAction('disable', async function (value) {
|
|
|
|
|
setDisable(value);
|
|
|
|
|
});
|
|
|
|
|
registerAction('visibility', async function (value) {
|
|
|
|
|
setVisibility(value);
|
|
|
|
|
});
|
2022-09-27 05:25:51 +00:00
|
|
|
registerAction(
|
|
|
|
|
'setText',
|
|
|
|
|
async function (text) {
|
|
|
|
|
setValue(text);
|
|
|
|
|
setExposedVariable('value', text).then(fireEvent('onChange'));
|
|
|
|
|
},
|
|
|
|
|
[setValue]
|
|
|
|
|
);
|
|
|
|
|
registerAction(
|
|
|
|
|
'clear',
|
|
|
|
|
async function () {
|
|
|
|
|
setValue('');
|
|
|
|
|
setExposedVariable('value', '').then(fireEvent('onChange'));
|
|
|
|
|
},
|
|
|
|
|
[setValue]
|
|
|
|
|
);
|
2022-07-15 10:23:35 +00:00
|
|
|
|
2021-04-30 06:31:32 +00:00
|
|
|
return (
|
2022-10-04 07:36:59 +00:00
|
|
|
<div data-disabled={disable} className={`text-input ${visibility || 'invisible'}`} data-cy="text-disable-div">
|
2021-09-06 08:50:38 +00:00
|
|
|
<input
|
2022-09-28 12:21:09 +00:00
|
|
|
ref={textInputRef}
|
2022-08-08 11:07:12 +00:00
|
|
|
onKeyUp={(e) => {
|
|
|
|
|
if (e.key == 'Enter') {
|
|
|
|
|
setValue(e.target.value);
|
|
|
|
|
setExposedVariable('value', e.target.value).then(() => {
|
|
|
|
|
fireEvent('onEnterPressed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}}
|
2021-09-13 18:04:07 +00:00
|
|
|
onChange={(e) => {
|
2021-12-14 03:40:42 +00:00
|
|
|
setValue(e.target.value);
|
|
|
|
|
setExposedVariable('value', e.target.value);
|
2021-12-22 06:33:59 +00:00
|
|
|
fireEvent('onChange');
|
2021-09-13 18:04:07 +00:00
|
|
|
}}
|
2022-09-28 12:21:09 +00:00
|
|
|
onBlur={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
fireEvent('onBlur');
|
|
|
|
|
}}
|
|
|
|
|
onFocus={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
fireEvent('onFocus');
|
|
|
|
|
}}
|
2021-09-06 08:50:38 +00:00
|
|
|
type="text"
|
2022-09-12 08:02:58 +00:00
|
|
|
className={`form-control ${!isValid ? 'is-invalid' : ''} validation-without-icon ${
|
|
|
|
|
darkMode && 'dark-theme-placeholder'
|
|
|
|
|
}`}
|
2021-12-14 03:40:42 +00:00
|
|
|
placeholder={properties.placeholder}
|
2022-10-26 05:15:43 +00:00
|
|
|
style={computedStyles}
|
2021-12-14 03:40:42 +00:00
|
|
|
value={value}
|
2022-09-23 12:33:05 +00:00
|
|
|
data-cy={`draggable-widget-${String(component.name).toLowerCase()}`}
|
2021-09-06 08:50:38 +00:00
|
|
|
/>
|
2022-09-23 12:33:05 +00:00
|
|
|
<div className="invalid-feedback" data-cy={`${String(component.name).toLowerCase()}-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
|
|
|
};
|