Skip to content

Commit

Permalink
Merge pull request #1757 from gettakaro/bugfix/select-width
Browse files Browse the repository at this point in the history
bugfix: always render select in portal
  • Loading branch information
emielvanseveren authored Nov 1, 2024
2 parents 2a6aaf6 + f17f1a1 commit 464fa85
Show file tree
Hide file tree
Showing 22 changed files with 25 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export function FilterRow<DataType extends object>({
control={control}
name={`filters.${index}.column`}
label="Column"
inPortal
render={(selectedItems) => {
if (selectedItems.length === 0) {
return 'Select a column';
Expand All @@ -95,7 +94,6 @@ export function FilterRow<DataType extends object>({
control={control}
name={`filters.${index}.operator`}
label="Condition"
inPortal
render={() => {
const operator = currentOperator?.toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const PageSizeSelect: FC<PageSizeSelectProps> = ({ onPageSizeChange, page
id="page-size"
multiple={false}
name="pageSize"
inPortal={true}
value={pageSize.toString() || '10'}
onChange={onPageSizeChange}
render={(selectedItems) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export const QuickSelect: FC<QuickSelectProps> = ({ id }) => {
<StyledForm onSubmit={handleSubmit(onSubmit)}>
<InputsContainer>
<SelectField
inPortal
control={control}
name="tense"
render={(selectedItems) => (
Expand All @@ -120,7 +119,6 @@ export const QuickSelect: FC<QuickSelectProps> = ({ id }) => {
<TextField control={control} type="number" name="step" />

<SelectField
inPortal
control={control}
name="unit"
render={(selectedItems) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export const RelativePicker: FC<RelativePickerProps> = ({ onChange, id, timeDire
<StyledForm onSubmit={handleSubmit(handleChange)}>
<InputsContainer>
<SelectField
inPortal
control={control}
readOnly={timeDirection !== 'pastAndFuture'}
name="tense"
Expand All @@ -121,7 +120,6 @@ export const RelativePicker: FC<RelativePickerProps> = ({ onChange, id, timeDire
</SelectField>
<TextField control={control} type="number" name="step" />
<SelectField
inPortal
control={control}
name="unit"
render={(selectedItems) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ export const GenericDurationField = forwardRef<HTMLDivElement, GenericDurationFi
/>
<SelectField
key={`${name}-${index}-unit`}
inPortal
control={control}
name={`durations.${index}.unit`}
multiple={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const ControlledSelectField: FC<ControlledSelectFieldProps> & SubComponen
control,
loading,
enableFilter,
inPortal,
canClear,
} = defaultsApplier(props);

Expand Down Expand Up @@ -114,7 +113,6 @@ export const ControlledSelectField: FC<ControlledSelectFieldProps> & SubComponen
onFocus={handleOnFocus}
render={render}
value={field.value}
inPortal={inPortal}
>
{children}
</GenericSelectField>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
MouseEvent,
} from 'react';
import { GroupContainer, GroupLabel } from '../style';
import { SelectContainer, StyledFloatingOverlay, StyledArrowIcon, SelectButton } from '../../sharedStyle';
import { SelectContainer, StyledArrowIcon, SelectButton } from '../../sharedStyle';
import { FilterInput } from './FilterInput';
import { AiOutlineClose as ClearIcon } from 'react-icons/ai';

Expand Down Expand Up @@ -40,9 +40,6 @@ import { IconButton } from '../../../../../components/';
interface SharedSelectFieldProps {
render: (selectedItems: SelectItem[]) => React.ReactNode;
enableFilter?: boolean;
/// Rendering in portal will render the selectDropdown independent from its parent container.
/// this is useful when select is rendered in other floating elements with limited space.
inPortal?: boolean;

/// When true, The select icon will be replaced by a cross icon to clear the selected value.
canClear?: boolean;
Expand Down Expand Up @@ -85,7 +82,6 @@ export const GenericSelectField: FC<GenericSelectFieldProps> & SubComponentTypes
hasError,
hasDescription,
name,
inPortal = false,
disabled,
enableFilter = false,
multiple = false,
Expand Down Expand Up @@ -116,14 +112,17 @@ export const GenericSelectField: FC<GenericSelectFieldProps> & SubComponentTypes
size({
apply({ availableHeight, elements, availableWidth }) {
const refWidth = elements.reference.getBoundingClientRect().width;
const floatingContentWidth = elements.floating.scrollWidth;

Object.assign(elements.floating.style, {
// Note: we cannot use the rects.reference.width here because if the referenced item is very small compared to the other options, there will be horizontal overflow.
// fit-content isn't the perfect solution either, because if there is no space available it might render outside the viewport.
width: availableWidth < refWidth ? `${availableWidth}px` : `${refWidth}px`,
minWidth:
availableWidth > refWidth ? `${availableWidth}px` : `${Math.max(refWidth, floatingContentWidth)}px`,
maxHeight: `${Math.max(150, availableHeight)}px`,
});
},
padding: 10,
}),
flip({
fallbackStrategy: 'bestFit',
Expand Down Expand Up @@ -286,14 +285,7 @@ export const GenericSelectField: FC<GenericSelectFieldProps> & SubComponentTypes
<StyledArrowIcon size={16} />
)}
</SelectButton>
{open &&
(!inPortal ? (
<StyledFloatingOverlay lockScroll style={{ zIndex: 1000 }}>
{renderSelect()}
</StyledFloatingOverlay>
) : (
<FloatingPortal>{renderSelect()}</FloatingPortal>
))}
{open && <FloatingPortal>{renderSelect()}</FloatingPortal>}
</SelectContext.Provider>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const ControlledSelectQueryField: FC<ControlledSelectQueryFieldProps> & S
name,
multiple,
control,
inPortal,
debounce,
isLoadingData,
hasNextPage,
Expand Down Expand Up @@ -83,7 +82,6 @@ export const ControlledSelectQueryField: FC<ControlledSelectQueryFieldProps> & S
multiple={multiple}
required={required}
size={componentSize}
inPortal={inPortal}
onChange={field.onChange}
onBlur={handleOnBlur}
canClear={canClear}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
FloatingPortal,
size,
useDismiss,
flip,
useFloating,
useInteractions,
useRole,
Expand All @@ -32,7 +33,7 @@ import { PaginationProps } from '../../../';

/* The SearchField depends on a few things of <Select/> */
import { GroupLabel } from '../../SelectField/style';
import { SelectContainer, SelectButton, StyledArrowIcon, StyledFloatingOverlay } from '../../sharedStyle';
import { SelectContainer, SelectButton, StyledArrowIcon } from '../../sharedStyle';
import { IconButton, InfiniteScroll, Spinner } from '../../../../../components';
import { GenericTextField } from '../../../TextField/Generic';

Expand All @@ -46,8 +47,6 @@ interface SharedSelectQueryFieldProps extends PaginationProps {
/// Triggered whenever the input value changes.
/// This is used to trigger the API call to get the new options
handleInputValueChange?: (value: string) => void;
/// render inPortal
inPortal?: boolean;

/// When true, The select icon will be replaced by a cross icon to clear the selected value.
canClear?: boolean;
Expand Down Expand Up @@ -100,7 +99,6 @@ export const GenericSelectQueryField = forwardRef<HTMLInputElement, GenericSelec
id,
placeholder = 'Search field',
hasDescription,
inPortal = false,
hasError,
children,
readOnly,
Expand Down Expand Up @@ -139,13 +137,24 @@ export const GenericSelectQueryField = forwardRef<HTMLInputElement, GenericSelec
middleware: [
offset(5),
size({
apply({ rects, elements }) {
apply({ availableHeight, elements, availableWidth }) {
const refWidth = elements.reference.getBoundingClientRect().width;
const floatingContentWidth = elements.floating.scrollWidth;

const width =
availableWidth > refWidth ? `${availableWidth}px` : `${Math.max(refWidth, floatingContentWidth)}px`;

Object.assign(elements.floating.style, {
width: `${rects.reference.width}px`,
maxHeight: '255px',
// Note: we cannot use the rects.reference.width here because if the referenced item is very small compared to the other options, there will be horizontal overflow.
// fit-content isn't the perfect solution either, because if there is no space available it might render outside the viewport.
width,
maxHeight: `${Math.max(150, availableHeight)}px`,
});
},
padding: 10,
}),
flip({
fallbackStrategy: 'bestFit',
fallbackPlacements: ['top', 'bottom'],
}),
],
});
Expand Down Expand Up @@ -312,14 +321,7 @@ export const GenericSelectQueryField = forwardRef<HTMLInputElement, GenericSelec
<StyledArrowIcon size={16} />
)}
</SelectButton>
{open &&
(!inPortal ? (
<StyledFloatingOverlay lockScroll style={{ zIndex: 1000 }}>
{renderSelect()}
</StyledFloatingOverlay>
) : (
<FloatingPortal>{renderSelect()}</FloatingPortal>
))}
{open && <FloatingPortal>{renderSelect()}</FloatingPortal>}
</SelectContext.Provider>
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ export function ItemWidget<T = unknown, S extends StrictRJSFSchema = RJSFSchema,
hasNextPage={hasNextPage}
isFetchingNextPage={isFetchingNextPage}
optionCount={data?.pages[0].meta.total}
inPortal={true}
fetchNextPage={fetchNextPage}
render={(selectedItems) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export function SelectWidget<T = unknown, S extends StrictRJSFSchema = RJSFSchem
readOnly={readonly}
value={value || defaultValue}
multiple={multiple}
inPortal={true}
hasDescription={!!schema.description}
onChange={onChange}
render={(selectedItems) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const CountrySelectField: FC<CountrySelectProps> = ({
size,
loading,
disabled,
inPortal,
readOnly,
canClear = true,
multiple,
Expand All @@ -45,7 +44,6 @@ export const CountrySelectField: FC<CountrySelectProps> = ({
required={required}
description={description}
size={size}
inPortal={inPortal}
render={(selectedItems) => {
if (selectedItems.length === 0) {
return <div>{multiple ? 'Select countries' : 'Select country'}</div>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const EventNameSelectField: FC<CustomSelectProps> = ({
loading,
description,
readOnly,
inPortal,
disabled,
size,
hint,
Expand All @@ -39,7 +38,6 @@ export const EventNameSelectField: FC<CustomSelectProps> = ({
required={required}
description={description}
size={size}
inPortal={inPortal}
render={(selectedItems) => {
if (selectedItems.length === 0) {
return <div>select event...</div>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export const GameServerSelectQueryField: FC<CustomSelectQueryProps & GameServerS
label = 'Game server',
control,
disabled,
inPortal,
description,
required,
filters,
Expand Down Expand Up @@ -77,7 +76,6 @@ export const GameServerSelectQueryField: FC<CustomSelectQueryProps & GameServerS
description={description}
size={size}
disabled={disabled}
inPortal={inPortal}
hint={hint}
name={selectName}
required={required}
Expand Down Expand Up @@ -109,7 +107,6 @@ export const GameServerSelectView: FC<GameServerSelectQueryViewProps> = ({
description,
size,
disabled,
inPortal,
hint,
required,
loading,
Expand Down Expand Up @@ -159,7 +156,6 @@ export const GameServerSelectView: FC<GameServerSelectQueryViewProps> = ({
description={description}
size={size}
disabled={disabled}
inPortal={inPortal}
hint={hint}
label={label}
required={required}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const ItemSelectQueryField: FC<ItemSelectQueryFieldProps> = ({
label,
loading,
disabled,
inPortal,
readOnly,
required,
description,
Expand Down Expand Up @@ -87,7 +86,6 @@ export const ItemSelectQueryField: FC<ItemSelectQueryFieldProps> = ({
description={description}
size={size}
disabled={disabled}
inPortal={inPortal}
hint={hint}
multiple={multiple}
placeholder={placeholder}
Expand Down Expand Up @@ -125,7 +123,6 @@ export const ItemSelectQueryView: FC<ItemSelectQueryViewProps> = ({
disabled,
placeholder,
multiple,
inPortal,
hint,
required,
gameServer,
Expand Down Expand Up @@ -164,7 +161,6 @@ export const ItemSelectQueryView: FC<ItemSelectQueryViewProps> = ({
loading={loading}
isLoadingData={isLoadingData}
disabled={disabled}
inPortal={inPortal}
readOnly={readOnly}
required={required}
multiple={multiple}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const ModuleSelectQueryField: FC<CustomSelectProps> = ({
loading,
description,
readOnly,
inPortal,
disabled,
size,
hint,
Expand Down Expand Up @@ -49,7 +48,6 @@ export const ModuleSelectQueryField: FC<CustomSelectProps> = ({
description={description}
size={size}
disabled={disabled}
inPortal={inPortal}
hint={hint}
name={selectName}
label={label}
Expand Down Expand Up @@ -78,7 +76,6 @@ export const ModuleSelectView: FC<ModuleSelectViewProps> = ({
description,
size,
disabled,
inPortal,
hint,
required,
loading,
Expand All @@ -101,7 +98,6 @@ export const ModuleSelectView: FC<ModuleSelectViewProps> = ({
hint={hint}
disabled={disabled}
size={size}
inPortal={inPortal}
required={required}
loading={loading}
canClear={canClear}
Expand Down
Loading

0 comments on commit 464fa85

Please sign in to comment.