Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: migrate autocomplete component#21323

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
jaaydenh wants to merge5 commits intomain
base:main
Choose a base branch
Loading
fromjaaydenh/migrate-autocomplete
Open
Show file tree
Hide file tree
Changes fromall 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
4 changes: 2 additions & 2 deletionssite/e2e/helpers.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -266,7 +266,7 @@ export const createTemplate = async (

// If the organization picker is present on the page, select the default
// organization.
const orgPicker = page.getByLabel("Belongs to *");
const orgPicker = page.getByTestId("organization-autocomplete");
const organizationsEnabled = await orgPicker.isVisible();
if (organizationsEnabled) {
if (orgName !== defaultOrganizationName) {
Expand DownExpand Up@@ -1233,7 +1233,7 @@ export async function createUser(

// If the organization picker is present on the page, select the default
// organization.
const orgPicker = page.getByLabel("Organization *");
const orgPicker = page.getByTestId("organization-autocomplete");
const organizationsEnabled = await orgPicker.isVisible();
if (organizationsEnabled) {
// The organization picker will be disabled if there is only one option.
Expand Down
25 changes: 24 additions & 1 deletionsite/src/components/Autocomplete/Autocomplete.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,6 +30,7 @@ interface AutocompleteProps<TOption> {
isOptionEqualToValue?: (option: TOption, value: TOption) => boolean;
renderOption?: (option: TOption, isSelected: boolean) => ReactNode;
loading?: boolean;
label?: string;
placeholder?: string;
noOptionsText?: string;
open?: boolean;
Expand All@@ -38,6 +39,7 @@ interface AutocompleteProps<TOption> {
onInputChange?: (value: string) => void;
clearable?: boolean;
disabled?: boolean;
required?: boolean;
startAdornment?: ReactNode;
className?: string;
id?: string;
Expand All@@ -53,6 +55,7 @@ export function Autocomplete<TOption>({
isOptionEqualToValue,
renderOption,
loading = false,
label,
placeholder = "Select an option",
noOptionsText = "No results found",
open: controlledOpen,
Expand All@@ -61,6 +64,7 @@ export function Autocomplete<TOption>({
onInputChange,
clearable = true,
disabled = false,
required = false,
startAdornment,
className,
id,
Expand DownExpand Up@@ -135,7 +139,7 @@ export function Autocomplete<TOption>({
const displayValue = value ? getOptionLabel(value) : "";
const showClearButton = clearable && value && !disabled;

return (
const autocomplete = (
<Popover open={isOpen} onOpenChange={handleOpenChange}>
<PopoverTrigger asChild disabled={disabled}>
<button
Expand DownExpand Up@@ -249,4 +253,23 @@ export function Autocomplete<TOption>({
</PopoverContent>
</Popover>
);

if (label) {
return (
<div className="flex flex-col gap-1">
<label
htmlFor={id}
className="text-sm font-medium text-content-primary"
>
{label}
{required && (
<span className="text-content-destructive ml-0.5">*</span>
)}
</label>
{autocomplete}
</div>
);
}

return autocomplete;
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
import { css } from "@emotion/css";
import Autocomplete from "@mui/material/Autocomplete";
import CircularProgress from "@mui/material/CircularProgress";
import TextField from "@mui/material/TextField";
import { checkAuthorization } from "api/queries/authCheck";
import { organizations } from "api/queries/organizations";
import type { AuthorizationCheck, Organization } from "api/typesGenerated";
import { Autocomplete } from "components/Autocomplete/Autocomplete";
import { Avatar } from "components/Avatar/Avatar";
import { AvatarData } from "components/Avatar/AvatarData";
import { typeComponentProps, typeFC, useEffect, useState } from "react";
import { type FC, useEffect, useState } from "react";
import { useQuery } from "react-query";

type OrganizationAutocompleteProps = {
onChange: (organization: Organization | null) => void;
label?: string;
className?: string;
size?: ComponentProps<typeof TextField>["size"];
required?: boolean;
check?: AuthorizationCheck;
error?: boolean;
helperText?: React.ReactNode;
};

export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
onChange,
label,
className,
size = "small",
required,
check,
error,
helperText,
}) => {
const [open, setOpen] = useState(false);
const [selected, setSelected] = useState<Organization | null>(null);
const organizationsQuery = useQuery(organizations());
const checks =
Expand DownExpand Up@@ -73,74 +71,45 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
}, [options, selected, onChange]);

return (
<Autocomplete
noOptionsText="No organizations found"
className={className}
options={options}
disabled={options.length === 1}
value={selected}
loading={organizationsQuery.isLoading}
data-testid="organization-autocomplete"
open={open}
isOptionEqualToValue={(a, b) => a.id === b.id}
getOptionLabel={(option) => option.display_name}
onOpen={() => {
setOpen(true);
}}
onClose={() => {
setOpen(false);
}}
onChange={(_, newValue) => {
setSelected(newValue);
onChange(newValue);
}}
renderOption={({ key, ...props }, option) => (
<li key={key} {...props}>
<div className="flex flex-col gap-2">
<Autocomplete
value={selected}
onChange={(newValue) => {
setSelected(newValue);
onChange(newValue);
}}
options={options}
getOptionValue={(option) => option.id}
getOptionLabel={(option) => option.display_name}
isOptionEqualToValue={(a, b) => a.id === b.id}
renderOption={(option) => (
<AvatarData
title={option.display_name}
subtitle={option.name}
src={option.icon}
/>
</li>
)}
label={label}
placeholder="Organization name"
noOptionsText="No organizations found"
loading={organizationsQuery.isLoading}
disabled={options.length === 1}
required={required}
startAdornment={
selected && (
<Avatar size="sm" src={selected.icon} fallback={selected.name} />
)
}
className={className}
data-testid="organization-autocomplete"
/>
{helperText && (
<span
className={`text-xs ${error ? "text-content-destructive" : "text-content-secondary"}`}
>
{helperText}
</span>
)}
renderInput={(params) => (
<TextField
{...params}
required={required}
fullWidth
size={size}
label={label}
placeholder="Organization name"
css={{
"&:not(:has(label))": {
margin: 0,
},
}}
InputProps={{
...params.InputProps,
startAdornment: selected && (
<Avatar size="sm" src={selected.icon} fallback={selected.name} />
),
endAdornment: (
<>
{organizationsQuery.isFetching && open && (
<CircularProgress size={16} />
)}
{params.InputProps.endAdornment}
</>
),
classes: { root },
}}
InputLabelProps={{
shrink: true,
}}
/>
)}
/>
</div>
);
};

const root = css`
padding-left: 14px !important; // Same padding left as input
gap: 4px;
`;
16 changes: 9 additions & 7 deletionssite/src/components/Popover/Popover.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,13 +31,15 @@ export const PopoverContent = forwardRef<
sideOffset={sideOffset}
collisionPadding={16}
className={cn(
`z-50 w-72 rounded-md border border-solid bg-surface-primary
text-content-primary shadow-md outline-none
data-[state=open]:animate-in data-[state=closed]:animate-out
data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0
data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95
data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2
data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2`,
// z-[1400] is required to appear above MUI dialogs (z-index: 1300) during
// migration. Can be reduced to z-50 once MUI is fully removed.
`z-[1400] w-72 rounded-md border border-solid bg-surface-primary
text-content-primary shadow-md outline-none
data-[state=open]:animate-in data-[state=closed]:animate-out
data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0
data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95
data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2
data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2`,
className,
)}
{...props}
Expand Down
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp