2021-09-13 18:04:07 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-04-02 04:16:26 +00:00
|
|
|
|
2021-12-22 06:33:59 +00:00
|
|
|
export const TextInput = function TextInput({ height, validate, properties, styles, setExposedVariable, fireEvent }) {
|
2021-12-14 03:40:42 +00:00
|
|
|
const [value, setValue] = useState(properties.value);
|
|
|
|
|
const { isValid, validationError } = validate(value);
|
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]);
|
2021-09-06 08:50:38 +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]);
|
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
|
2021-12-14 03:40:42 +00:00
|
|
|
disabled={styles.disabledState}
|
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
|
|
|
}}
|
2021-09-06 08:50:38 +00:00
|
|
|
type="text"
|
|
|
|
|
className={`form-control ${!isValid ? 'is-invalid' : ''} validation-without-icon`}
|
2021-12-14 03:40:42 +00:00
|
|
|
placeholder={properties.placeholder}
|
|
|
|
|
style={{ height, display: styles.visibility ? '' : 'none' }}
|
|
|
|
|
value={value}
|
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
|
|
|
};
|