Skip to content

Commit 8c3d43b

Browse files
committed
fix: added delayed validation on create database form
1 parent 9fe28f1 commit 8c3d43b

1 file changed

Lines changed: 83 additions & 21 deletions

File tree

packages/react-core/src/demos/Animations/examples/Animations.tsx

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,44 +1318,96 @@ export const Animations: FunctionComponent = () => {
13181318

13191319
const CreateDatabaseForm: FunctionComponent = () => {
13201320
const [name, setName] = useState('');
1321-
const [isNameValid, setIsNameValid] = useState('default');
13221321
const [email, setEmail] = useState('');
13231322
const [version, setVersion] = useState('');
13241323
const [isTimeZoneOpen, setIsTimeZoneOpen] = useState(false);
13251324
const [selectedTimeZone, setSelectedTimeZone] = useState('');
13261325
const [password, setPassword] = useState('');
1327-
const [isPasswordValid, setIsPasswordValid] = useState('default');
13281326
const [isSuccess, setIsSuccess] = useState(false);
13291327
const [actionCompleted, setActionCompleted] = useState(false);
13301328

13311329
const labelHelpRef = useRef(null);
13321330

1333-
const handleNameChange = (_event, name: string) => {
1331+
// Re-introducing the type alias for validation status
1332+
type validationStatus = 'success' | 'warning' | 'error' | 'default';
1333+
1334+
// Reverting useState to infer the type as a generic string
1335+
const [isNameValid, setIsNameValid] = useState('default');
1336+
const [isPasswordValid, setIsPasswordValid] = useState('default');
1337+
const [isNameValidating, setIsNameValidating] = useState(false);
1338+
const [isPasswordValidating, setIsPasswordValidating] = useState(false);
1339+
1340+
// useEffect for delayed name validation
1341+
useEffect(() => {
1342+
if (name === '') {
1343+
setIsNameValid('default');
1344+
setIsNameValidating(false);
1345+
return;
1346+
}
1347+
1348+
setIsNameValidating(true);
1349+
const timerId = setTimeout(() => {
1350+
const isValid = name.length > 0 && /^[a-z0-9-]+$/.test(name);
1351+
setIsNameValid(isValid ? 'success' : 'error');
1352+
setIsNameValidating(false);
1353+
}, 2000);
1354+
1355+
return () => {
1356+
clearTimeout(timerId);
1357+
setIsNameValidating(false);
1358+
};
1359+
}, [name]);
1360+
1361+
// useEffect for delayed password validation
1362+
useEffect(() => {
1363+
if (password === '') {
1364+
setIsPasswordValid('default');
1365+
setIsPasswordValidating(false);
1366+
return;
1367+
}
1368+
1369+
setIsPasswordValidating(true);
1370+
const timerId = setTimeout(() => {
1371+
const isValid = password.length >= 12 && /[0-9]/.test(password) && /[A-Z]/.test(password);
1372+
setIsPasswordValid(isValid ? 'success' : 'error');
1373+
setIsPasswordValidating(false);
1374+
}, 2000);
1375+
1376+
return () => {
1377+
clearTimeout(timerId);
1378+
setIsPasswordValidating(false);
1379+
};
1380+
}, [password]);
1381+
1382+
const handleNameChange = (_event: React.FormEvent<HTMLInputElement>, name: string) => {
13341383
setName(name);
1335-
setIsNameValid(name.length > 0 && /^[a-z0-9-]+$/.test(name) ? 'success' : 'error');
1384+
setIsNameValid('default');
1385+
setIsNameValidating(false);
13361386
};
13371387

1338-
const handleEmailChange = (_event, email: string) => {
1388+
const handleEmailChange = (_event: React.FormEvent<HTMLInputElement>, email: string) => {
13391389
setEmail(email);
13401390
};
13411391

1342-
const handleVersionChange = (_event, version: string) => {
1392+
const handleVersionChange = (_event: React.FormEvent<HTMLInputElement>, version: string) => {
13431393
setVersion(version);
13441394
};
13451395

1346-
const handlePasswordChange = (_event, password: string) => {
1396+
const handlePasswordChange = (_event: React.FormEvent<HTMLInputElement>, password: string) => {
13471397
setPassword(password);
1348-
setIsPasswordValid(
1349-
password.length >= 12 && /[0-9]/.test(password) && /[A-Z]/.test(password) ? 'success' : 'error'
1350-
);
1398+
setIsPasswordValid('default');
1399+
setIsPasswordValidating(false);
13511400
};
13521401

1353-
const onTimeZoneSelect = (_event, selection) => {
1354-
setSelectedTimeZone(selection);
1402+
const onTimeZoneSelect = (
1403+
_event: React.MouseEvent | React.ChangeEvent | undefined,
1404+
selection: string | number | undefined
1405+
) => {
1406+
setSelectedTimeZone(selection as string);
13551407
setIsTimeZoneOpen(false);
13561408
};
13571409

1358-
const timeZoneToggle = (toggleRef) => (
1410+
const timeZoneToggle = (toggleRef: React.Ref<MenuToggleElement>) => (
13591411
<MenuToggle ref={toggleRef} onClick={() => setIsTimeZoneOpen(!isTimeZoneOpen)} isExpanded={isTimeZoneOpen}>
13601412
{selectedTimeZone || 'Select time zone'}
13611413
</MenuToggle>
@@ -1441,15 +1493,23 @@ export const Animations: FunctionComponent = () => {
14411493
aria-describedby="simple-form-name-01-helper"
14421494
value={name}
14431495
onChange={handleNameChange}
1444-
validated={isNameValid as 'success' | 'warning' | 'error' | 'default'}
1496+
validated={isNameValid as validationStatus}
14451497
/>
14461498
<FormHelperText>
14471499
<HelperText>
14481500
<HelperTextItem
1449-
icon={isNameValid === 'success' ? <CheckCircleIcon /> : <ExclamationCircleIcon />}
1450-
variant={isNameValid as 'success' | 'warning' | 'error' | 'default'}
1501+
icon={isNameValid === 'error' ? <ExclamationCircleIcon /> : undefined}
1502+
variant={isNameValid as validationStatus}
14511503
>
1452-
Must be unique. Can only contain letters, numbers, and hyphens.
1504+
{(() => {
1505+
if (isNameValidating) {
1506+
return 'Validating...';
1507+
}
1508+
if (isNameValid === 'error') {
1509+
return 'Must contain only lowercase letters, numbers, and hyphens.';
1510+
}
1511+
return 'Must be a unique name.';
1512+
})()}
14531513
</HelperTextItem>
14541514
</HelperText>
14551515
</FormHelperText>
@@ -1506,15 +1566,17 @@ export const Animations: FunctionComponent = () => {
15061566
placeholder="********"
15071567
value={password}
15081568
onChange={handlePasswordChange}
1509-
validated={isPasswordValid as 'success' | 'warning' | 'error' | 'default'}
1569+
validated={isPasswordValid as validationStatus}
15101570
/>
15111571
<FormHelperText>
15121572
<HelperText>
15131573
<HelperTextItem
1514-
icon={isPasswordValid === 'success' ? <CheckCircleIcon /> : <ExclamationCircleIcon />}
1515-
variant={isPasswordValid as 'success' | 'warning' | 'error' | 'default'}
1574+
icon={isPasswordValid === 'error' ? <ExclamationCircleIcon /> : undefined}
1575+
variant={isPasswordValid as validationStatus}
15161576
>
1517-
Password must be at least 12 characters and include one uppercase letter and one number.
1577+
{isPasswordValidating
1578+
? 'Validating...'
1579+
: 'Password must be at least 12 characters and include one uppercase letter and one number.'}
15181580
</HelperTextItem>
15191581
</HelperText>
15201582
</FormHelperText>

0 commit comments

Comments
 (0)