Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit fd6fa9c

Browse files
committed
Allow fixed position custom select menu
1 parent b588d3a commit fd6fa9c

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

dist/form-components.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/form-components.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"/form-components.js":"/form-components.js?id=f717ced4eb4aba34bb40"}
1+
{"/form-components.js":"/form-components.js?id=d6867b5730fb6129fc03"}

resources/js/components/custom-select.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,12 @@ export default function customSelect(state) {
459459
return;
460460
}
461461

462+
if (this.fixedPosition) {
463+
this.positionFixedMenu();
464+
465+
return;
466+
}
467+
462468
this.$refs.container.classList.remove('custom-menu-top');
463469

464470
// give a little bit of breathing room at the bottom of the screen.
@@ -472,5 +478,31 @@ export default function customSelect(state) {
472478
this.$refs.container.classList.add('custom-menu-top');
473479
}
474480
},
481+
482+
positionFixedMenu() {
483+
// So we can accurately determine where it should be placed...
484+
this.$refs.container.style.position = 'absolute';
485+
this.$refs.container.style.top = null;
486+
487+
const { width, left: buttonLeft, top: buttonTop } = this.$refs.button.getBoundingClientRect();
488+
489+
// give a little bit of breathing room at the bottom of the screen.
490+
const tolerance = 10;
491+
const menuHeight = this.$refs.menu.offsetHeight;
492+
const largestHeight = window.innerHeight - menuHeight - tolerance;
493+
494+
const { top } = this.$refs.menu.getBoundingClientRect();
495+
496+
if (top > largestHeight) {
497+
const menuTop = buttonTop - menuHeight - tolerance;
498+
this.$refs.container.style.top = `${menuTop}px`;
499+
} else {
500+
this.$refs.container.style.top = `${top}px`;
501+
}
502+
503+
this.$refs.container.style.position = 'fixed';
504+
this.$refs.container.style.width = `${width}px`;
505+
this.$refs.container.style.left = `${buttonLeft}px`;
506+
},
475507
};
476508
}

src/Components/Inputs/CustomSelect.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CustomSelect extends Select
1919
public bool $filterable;
2020
public $clearIcon;
2121
public bool $disabled;
22+
public bool $fixedPosition;
2223

2324
public function __construct(
2425
string $name = '',
@@ -38,7 +39,8 @@ public function __construct(
3839
string $textKey = 'text',
3940
bool $filterable = false,
4041
string $clearIcon = null,
41-
bool $disabled = false
42+
bool $disabled = false,
43+
bool $fixedPosition = false
4244
) {
4345
parent::__construct(
4446
$name,
@@ -64,6 +66,7 @@ public function __construct(
6466
$this->filterable = $filterable;
6567
$this->clearIcon = $clearIcon ?? config('form-components.components.custom-select.clear_icon');
6668
$this->disabled = $disabled;
69+
$this->fixedPosition = $fixedPosition;
6770
}
6871

6972
public function buttonClass(): string
@@ -96,6 +99,7 @@ public function config(): array
9699
'filterable' => $this->filterable,
97100
'placeholder' => $this->placeholder,
98101
'selectId' => Str::random(8),
102+
'fixedPosition' => $this->fixedPosition,
99103
];
100104
}
101105

src/Components/Inputs/CustomSelectOption.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected function extractDataFromOption($option): array
6363

6464
if ($option instanceof Model) {
6565
return [
66-
'value' => $option->{$this->valueKey},
66+
'value' => (string) $option->{$this->valueKey},
6767
'text' => $option->{$this->textKey},
6868
];
6969
}
@@ -73,7 +73,7 @@ protected function extractDataFromOption($option): array
7373
}
7474

7575
return [
76-
'value' => $option,
76+
'value' => (string) $option,
7777
'text' => $option,
7878
];
7979
}
@@ -85,13 +85,13 @@ protected function extractDataFromArray($option): array
8585
$key = array_key_first($option);
8686

8787
return [
88-
'value' => $key,
88+
'value' => (string) $key,
8989
'text' => $option[$key],
9090
];
9191
}
9292

9393
return [
94-
'value' => $option[$this->valueKey] ?? '',
94+
'value' => (string) ($option[$this->valueKey] ?? ''),
9595
'text' => $option[$this->textKey] ?? '',
9696
];
9797
}

0 commit comments

Comments
 (0)