Skip to content

Commit

Permalink
feature: add preload support
Browse files Browse the repository at this point in the history
feature: add preload support
  • Loading branch information
lukas-frey authored Jun 27, 2024
2 parents 9107853 + 3116d88 commit dbd3ddd
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Forms/IconPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,43 @@ public function getSearchResults(string $search): array
}

/**
* Enact the preload logic (if, and only if, the user wants to preload the icons)
*/
protected function doPreload(): void
{
if (!$this->isPreloaded()) {
return;
}

// To actually preload the icons, we trigger a search on the empty string.
// `str_contains` will return true for any haystack if the needle is the empty string.
// This is exactly how we know we get all the icons AND respect the user-land
// configuration applied to this field instance.
$options = $this->getSearchResults('');

// To avoid recursively and needlessly loading the icons each time
// anything requests the options or uses the `doPreload` method,
// we set the `preload` option to false right before setting the
// resolved/computed icons.
$this->preload(false);

// We delegate back to the parent's `options` method as a setter
// to keep our own as a throwing-method in user-land.
// This sets the icons on the back-end and front-end.
// It also make it work as soon as the component is mounted,
// which means there's no need for user interaction to get the
// full list of options loaded directly.
parent::options($options);
}

public function getOptions(): array
{
$this->doPreload();
return parent::getOptions();
}


/**
* Marks the calling method as not allowed (whether because it's not supported or because it's meaningless when using this field)
* @throws \BadMethodCallException Always
*/
Expand Down

0 comments on commit dbd3ddd

Please sign in to comment.