UNPKG

@selfcommunity/react-ui

Version:

React UI Components to integrate a Community created with SelfCommunity Platform.

57 lines (56 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const styles_1 = require("@mui/material/styles"); const material_1 = require("@mui/material"); const react_intl_1 = require("react-intl"); const Account_1 = require("../../constants/Account"); const messages = (0, react_intl_1.defineMessages)({ usernameError: { id: 'ui.common.error.username', defaultMessage: 'ui.common.error.username' }, usernameOrEmail: { id: 'ui.common.error.usernameOrEmail', defaultMessage: 'ui.common.error.usernameOrEmail' } }); const PREFIX = 'SCUsernameTextField'; const Root = (0, styles_1.styled)(material_1.TextField, { name: PREFIX, slot: 'Root', overridesResolver: (props, styles) => styles.root })(({ theme }) => ({})); const UsernameTextField = (props) => { // PROPS const { onChange, error = false, allowEmail = false, helperText = null } = props, rest = tslib_1.__rest(props, ["onChange", "error", "allowEmail", "helperText"]); // STATE const [errorMsg, setErrorMsg] = (0, react_1.useState)(null); // HOOKS const intl = (0, react_intl_1.useIntl)(); // HANDLERS const handleChange = (event) => { if (event.target.value) { if (allowEmail && ((event.target.value.includes('@') && !Account_1.EMAIL_REGEX.test(event.target.value)) || (!event.target.value.includes('@') && !Account_1.USERNAME_REGEX.test(event.target.value)))) { setErrorMsg(intl.formatMessage(messages.usernameOrEmail)); } else if (!allowEmail && !Account_1.USERNAME_REGEX.test(event.target.value)) { setErrorMsg(intl.formatMessage(messages.usernameError)); } else if (error !== null) { setErrorMsg(null); } } else if (error !== null) { setErrorMsg(null); } onChange && onChange(event); }; // RENDER return (0, jsx_runtime_1.jsx)(Root, Object.assign({}, rest, { onChange: handleChange, error: Boolean(errorMsg) || error, helperText: errorMsg || helperText })); }; exports.default = UsernameTextField;