Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

H-3879: Add protocol dropdown to early access form URL input #6106

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Button } from "../../../shared/ui/button";
import { MenuItem } from "../../../shared/ui/menu-item";
import { Modal } from "../../../shared/ui/modal";
import { useAuthenticatedUser } from "../../shared/auth-info-context";
import { UrlInput } from "../../shared/url-input";

type FormFieldMetadata = {
label: string;
Expand Down Expand Up @@ -93,7 +94,14 @@ const Input = ({
*
</Box>
<Box>
{options ? (
{url ? (
<UrlInput
autoFocus
onChange={onChange}
placeholder={placeholder}
value={value}
/>
) : options ? (
<Select
value={value}
onChange={(event) => onChange(event.target.value)}
Expand All @@ -107,12 +115,6 @@ const Input = ({
</Select>
) : (
<TextField
autoFocus={url}
inputProps={
url
? { pattern: "\\S+\\.\\w+", title: "Please enter a valid URL" }
: undefined
}
multiline={multiline}
onChange={(event) => onChange(event.target.value)}
placeholder={placeholder}
Expand Down
89 changes: 89 additions & 0 deletions apps/hash-frontend/src/pages/shared/url-input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Select, TextField } from "@hashintel/design-system";
import { outlinedInputClasses, Stack } from "@mui/material";

import { MenuItem } from "../../shared/ui/menu-item";

const protocols = ["https", "http"] as const;

type Protocol = (typeof protocols)[number];

const partsFromUrl = (url: string): { protocol: Protocol; rest: string } => {
if (!url) {
return { protocol: "https", rest: "" };
}

const [protocol, rest] = url.split("://");

if (!protocol) {
return { protocol: "https", rest: "" };
}

return { protocol: protocol as Protocol, rest: rest ?? "" };
};

export const UrlInput = ({
autoFocus,
onChange,
placeholder,
value,
}: {
autoFocus: boolean;
onChange: (value: string) => void;
placeholder: string;
value: string;
}) => {
const { protocol, rest } = partsFromUrl(value);

return (
<Stack direction="row">
<Select
value={protocol}
onChange={(event) => {
const newProtocol = event.target.value as Protocol;
onChange(`${newProtocol}://${rest}`);
}}
sx={{
[`& .${outlinedInputClasses.root}`]: {
borderTopRightRadius: 0,
borderBottomRightRadius: 0,
},
[`.${outlinedInputClasses.notchedOutline}`]: {
borderRight: "none",
},
width: 110,
"& svg": { color: ({ palette }) => palette.gray[30] },
}}
>
{protocols.map((option) => (
<MenuItem key={option} value={option}>
{option}://
</MenuItem>
))}
</Select>
<TextField
autoFocus={autoFocus}
inputProps={{
pattern: "\\S+\\.\\w+(\\/\\S*)?",
title: "Please enter a valid domain",
}}
onChange={(event) => {
const newRest = event.target.value;
onChange(`${protocol}://${newRest}`);
}}
placeholder={placeholder}
sx={{
width: "100%",
[`.${outlinedInputClasses.notchedOutline}`]: {
borderLeftColor: "transparent",
},
[`& .${outlinedInputClasses.root}`]: {
borderTopLeftRadius: 0,
borderBottomLeftRadius: 0,
},
}}
type="text"
value={rest}
/>
</Stack>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const MuiSelectThemeOptions: Components<Theme>["MuiSelect"] = {
},

[`&:focus ~ .${outlinedInputClasses.notchedOutline}`]: {
border: `1px solid ${theme.palette.blue[60]}`,
border: `2px solid ${theme.palette.blue[60]}`,
},
}),
icon: ({ theme }) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ export const MuiMenuItemThemeOptions: Components<Theme>["MuiMenuItem"] = {
},

[`&.${menuItemClasses.focusVisible}, &:focus`]: {
boxShadow: `0px 0px 0px 2px ${theme.palette.white}, 0px 0px 0px 4px ${theme.palette.blue[70]}`,
backgroundColor: "transparent",
backgroundColor: theme.palette.blue[20],
},

[`&.${menuItemClasses.selected}, &.${menuItemClasses.selected}:hover, &:active`]:
Expand Down
Loading