@geneui/components
Version:
The Gene UI components library designed for BI tools
168 lines (161 loc) • 5.09 kB
JavaScript
import { _ as _extends } from '../_rollupPluginBabelHelpers-e8fb2e5c.js';
import React__default, { forwardRef, useState, useCallback, useEffect } from 'react';
import PropTypes from 'prop-types';
import { n as noop } from '../index-a0e4e333.js';
import useMount from '../hooks/useMount.js';
import '../configs-00612ce0.js';
import ExtendedInput from '../ExtendedInput/index.js';
import '../dateValidation-67caec66.js';
import '../_commonjsHelpers-24198af3.js';
import 'react-dom';
import '../index-031ff73c.js';
import '../hooks/useDeviceType.js';
import '../hooks/useWindowSize.js';
import '../hooks/useDebounce.js';
import '../useEllipsisDetection-4d997d5d.js';
import '../Icon/index.js';
import '../style-inject.es-746bb8ed.js';
import '../SuggestionList/index.js';
import '../hooks/useKeyDown.js';
import '../hooks/useClickOutside.js';
import '../config-1053d64d.js';
import '../Scrollbar/index.js';
import '../callAfterDelay-7272faca.js';
import '../index-6d7e99cd.js';
import '../tslib.es6-f211516f.js';
import '../GeneUIProvider/index.js';
const emailRegExp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
function checkValidation(required, isValid, maxLength, minLength, isEmail) {
let value = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : '';
const valueWithoutSpaces = value ? value.toString().trim() : value;
if (isValid === false) return {
key: 'customValidation',
isValid: false
};
if (minLength && valueWithoutSpaces.length <= minLength) return {
key: 'minLength',
isValid: false
};
if (maxLength && valueWithoutSpaces.length >= maxLength) return {
key: 'maxLength',
isValid: false
};
if (valueWithoutSpaces.length > 0 && isEmail && !emailRegExp.test(valueWithoutSpaces)) return {
key: 'isEmail',
isValid: false
};
if (required && valueWithoutSpaces.length === 0) return {
key: 'required',
isValid: false
};
return {
key: null,
isValid: true
};
}
const TextInput = /*#__PURE__*/forwardRef((props, ref) => {
const {
onChange,
value,
isValid,
onBlur,
required,
maxLength,
minLength,
isEmail,
isFieldValid,
forceAllowValidation,
defaultValue,
...restProps
} = props;
const isControlled = 'value' in props && typeof value !== 'undefined';
const [validationState, setValidationState] = useState(true);
const [allowValidation, setAllowValidation] = useState(false);
const validate = useCallback(defaultValue => checkValidation(required, isValid, maxLength, minLength, isEmail, value || defaultValue).isValid, [value, required, isValid, maxLength, minLength, isEmail]);
const handleChange = useCallback(e => {
const {
value
} = e.target;
const validation = checkValidation(required, isValid, maxLength, minLength, isEmail, value);
setValidationState(validation.isValid);
onChange && onChange(e, validation.isValid, validation.key);
}, [onChange, required, isValid, maxLength, minLength, isEmail]);
// we use this because need to show field validation after onBlur
const handleBlur = useCallback(e => {
setAllowValidation(true);
onBlur(e);
}, [onBlur]);
useEffect(() => {
isControlled && setValidationState(validate());
}, [isControlled, validate]);
// need this for handling user's `isValid` prop
useEffect(() => {
setValidationState(isValid);
}, [isValid]);
// call function when validation state changes
useEffect(() => {
isFieldValid(validationState);
}, [validationState]);
// set Allow validation true if submit button clicked
useEffect(() => {
forceAllowValidation && setAllowValidation(true);
}, [forceAllowValidation]);
useMount(() => setValidationState(validate(defaultValue)));
return /*#__PURE__*/React__default.createElement(ExtendedInput, _extends({}, restProps, {
ref: ref,
value: value,
isValid: !allowValidation || validationState,
onChange: handleChange,
required: required,
defaultValue: defaultValue,
onBlur: handleBlur
}));
});
TextInput.propTypes = {
/**
* Value for text input
*/
value: PropTypes.string,
/**
* Callback fires when field changes
*/
onChange: PropTypes.func,
/**
* Callback fires when field blured
*/
onBlur: PropTypes.func,
/**
* Define is field required or no.
*/
required: PropTypes.bool,
/**
* Is field accept only email or no
*/
isEmail: PropTypes.bool,
/**
* Maximum length of value
*/
maxLength: PropTypes.number,
/**
* Minimum length of value
*/
minLength: PropTypes.number,
/**
* Callback fires when field validation state changes
*/
isFieldValid: PropTypes.func,
/**
* Additional validation state
*/
isValid: PropTypes.bool,
/**
* Allow validation without onBlur, validate field when mount
*/
forceAllowValidation: PropTypes.bool
};
TextInput.defaultProps = {
isValid: true,
isFieldValid: noop,
onBlur: noop
};
export { TextInput as default };