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

[PUI] Make API form Select Field theme-aware #6521

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 57 additions & 2 deletions src/frontend/src/components/forms/fields/RelatedModelField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { t } from '@lingui/macro';
import { Input } from '@mantine/core';
import { Input, useMantineTheme } from '@mantine/core';
import { useDebouncedValue } from '@mantine/hooks';
import { useId } from '@mantine/hooks';
import { useQuery } from '@tanstack/react-query';
Expand Down Expand Up @@ -209,8 +209,54 @@ export function RelatedModelField({
[pk, data]
);

// Field doesn't follow Mantine theming
// Define color theme to pass to field based on Mantine theme
const th = useMantineTheme();
let colors: any;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here, it would make sense to wrap this into a useMemo

if (th.colorScheme === 'dark') {
colors = {
neutral0: th.colors[th.colorScheme][6],
neutral5: th.colors[th.colorScheme][4],
neutral10: th.colors[th.colorScheme][4],
neutral20: th.colors[th.colorScheme][4],
neutral30: th.colors[th.colorScheme][3],
neutral40: th.colors[th.colorScheme][2],
neutral50: th.colors[th.colorScheme][1],
neutral60: th.colors[th.colorScheme][0],
neutral70: th.colors[th.colorScheme][0],
neutral80: th.colors[th.colorScheme][0],
neutral90: th.colors[th.colorScheme][0],
primary: th.colors[th.primaryColor][7],
primary25: th.colors[th.primaryColor][6],
primary50: th.colors[th.primaryColor][5],
primary75: th.colors[th.primaryColor][4]
};
} else {
colors = {
neutral0: th.white,
neutral5: th.fn.darken(th.white, 0.05),
neutral10: th.fn.darken(th.white, 0.1),
neutral20: th.fn.darken(th.white, 0.2),
neutral30: th.fn.darken(th.white, 0.3),
neutral40: th.fn.darken(th.white, 0.4),
neutral50: th.fn.darken(th.white, 0.5),
neutral60: th.fn.darken(th.white, 0.6),
neutral70: th.fn.darken(th.white, 0.7),
neutral80: th.fn.darken(th.white, 0.8),
neutral90: th.fn.darken(th.white, 0.9),
primary: th.colors[th.primaryColor][7],
primary25: th.colors[th.primaryColor][4],
primary50: th.colors[th.primaryColor][5],
primary75: th.colors[th.primaryColor][6]
};
}

return (
<Input.Wrapper {...fieldDefinition} error={error?.message}>
<Input.Wrapper
{...fieldDefinition}
error={error?.message}
styles={{ description: { paddingBottom: '5px' } }}
>
<Select
id={fieldId}
value={currentValue}
Expand Down Expand Up @@ -246,6 +292,15 @@ export function RelatedModelField({
menuPosition="fixed"
styles={{ menuPortal: (base: any) => ({ ...base, zIndex: 9999 }) }}
formatOptionLabel={(option: any) => formatOption(option)}
theme={(theme) => {
return {
...theme,
colors: {
...theme.colors,
...colors
}
};
}}
/>
</Input.Wrapper>
);
Expand Down