1
0
mirror of https://github.com/spacebarchat/client.git synced 2024-11-22 10:22:30 +01:00

dob input: day and year inputs

This commit is contained in:
Puyodead1 2023-04-06 17:32:27 -04:00
parent 7933c4a9b5
commit 0011ecf701
No known key found for this signature in database
GPG Key ID: A4FA4FEC0DD353FC
2 changed files with 83 additions and 6 deletions

View File

@ -1,6 +1,5 @@
.select-search-container { .select-search-container {
position: relative; position: relative;
color: var(--text-muted);
z-index: 1; z-index: 1;
} }
@ -10,7 +9,7 @@
padding: 10px; padding: 10px;
background: var(--secondary); background: var(--secondary);
border: none; border: none;
color: var(--text-muted); color: var(--text);
outline: none; outline: none;
font-size: 16px; font-size: 16px;
text-align: left; text-align: left;
@ -73,7 +72,7 @@
background: var(--primary); background: var(--primary);
border: none; border: none;
outline: none; outline: none;
color: var(--text-muted); color: var(--text);
font-size: 16px; font-size: 16px;
text-align: left; text-align: left;
cursor: pointer; cursor: pointer;

View File

@ -1,7 +1,12 @@
import React from "react";
import SelectSearch from "react-select-search"; import SelectSearch from "react-select-search";
import styled from "styled-components"; import styled from "styled-components";
import "./DOBInput.css"; import "./DOBInput.css";
// const MIN_AGE = 13;
const MIN_AGE = 3; // we do this instead so we can show an age gate if they are under 13
const MAX_AGE = 120;
const Container = styled.div` const Container = styled.div`
display: flex; display: flex;
`; `;
@ -16,6 +21,7 @@ const Input = styled.input<{ error?: boolean }>`
margin: 0 0 0 5px; margin: 0 0 0 5px;
border: none; border: none;
aria-invalid: ${(props) => (props.error ? "true" : "false")}; aria-invalid: ${(props) => (props.error ? "true" : "false")};
border: ${(props) => (props.error ? "1px solid red" : "none")};
box-sizing: border-box; box-sizing: border-box;
width: 100%; width: 100%;
`; `;
@ -72,11 +78,83 @@ const MONTHS = [
]; ];
function DOBInput() { function DOBInput() {
const [month, setMonth] = React.useState<string>();
const [day, setDay] = React.useState("");
const [year, setYear] = React.useState("");
const [errors, setErrors] = React.useState<{
month?: string;
day?: string;
year?: string;
}>({
month: undefined,
day: undefined,
year: undefined,
});
const onInputChange =
(type: string) => (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
// clear error for field
setErrors({ ...errors, [type]: undefined });
// ensure only numbers
if (isNaN(Number(value))) {
setErrors({ ...errors, [type]: "Invalid input" });
return;
}
if (type === "day") {
// day should be a number between 1-31 and not more than 2 digits
if (
value !== "" &&
(value.length > 2 || Number(value) > 31 || Number(value) < 1)
) {
setErrors({ ...errors, day: "Invalid day" });
// return;
}
setDay(value);
}
if (type === "year") {
// year must be between now-min and now-max
if (
value.length === 4 &&
(Number(value) > new Date().getFullYear() - MIN_AGE ||
Number(value) < new Date().getFullYear() - MAX_AGE)
) {
setErrors({ ...errors, year: "Invalid year" });
// return;
}
setYear(value);
}
};
return ( return (
<Container> <Container>
<SelectSearch placeholder="Month" search options={MONTHS} /> <SelectSearch
<Input /> placeholder="Month"
<Input /> search
options={MONTHS}
onChange={(e) => setMonth(e as string)}
value={month}
/>
<Input
placeholder="Day"
onChange={onInputChange("day")}
value={day}
error={!!errors.day}
maxLength={2}
/>
<Input
placeholder="Year"
onChange={onInputChange("year")}
value={year}
error={!!errors.year}
maxLength={4}
/>
</Container> </Container>
); );
} }