Browse Source

Allow global attributes to get merged in.

mscherer 3 years ago
parent
commit
3723f96839

+ 13 - 0
docs/Helper/Icon.md

@@ -46,6 +46,19 @@ In this case make sure to use an array instead of just the class string:
 ],
 ],
 ```
 ```
 
 
+You can also set a global attributes config that would be merged in with every icon:
+```php
+'Icon' => [
+    'sets' => [
+        ...
+    ],
+    'attributes' => [
+        'data-custom' => 'some-default',
+        ...
+    ],
+],
+```
+
 Don't forget to also set up the necessary stylesheets (CSS files) and alike.
 Don't forget to also set up the necessary stylesheets (CSS files) and alike.
 
 
 ## Usage
 ## Usage

+ 6 - 0
src/View/Icon/IconCollection.php

@@ -46,6 +46,9 @@ class IconCollection {
 
 
 			/** @var class-string<\Tools\View\Icon\IconInterface> $className */
 			/** @var class-string<\Tools\View\Icon\IconInterface> $className */
 			$className = $setConfig['class'];
 			$className = $setConfig['class'];
+			if (isset($config['attributes']) && isset($setConfig['attributes'])) {
+				$setConfig['attributes'] += $config['attributes'];
+			}
 			$setConfig += $config;
 			$setConfig += $config;
 			$this->iconSets[$set] = new $className($setConfig);
 			$this->iconSets[$set] = new $className($setConfig);
 		}
 		}
@@ -115,6 +118,9 @@ class IconCollection {
 			}
 			}
 		}
 		}
 
 
+		$attributes += $options['attributes'] ?? [];
+		unset($options['attributes']);
+
 		return $this->iconSets[$set]->render($icon, $options, $attributes);
 		return $this->iconSets[$set]->render($icon, $options, $attributes);
 	}
 	}
 
 

+ 42 - 1
tests/TestCase/View/Icon/IconCollectionTest.php

@@ -12,7 +12,48 @@ class IconCollectionTest extends TestCase {
 	/**
 	/**
 	 * @return void
 	 * @return void
 	 */
 	 */
-	public function testCollect(): void {
+	public function testRender(): void {
+		$config = [
+			'sets' => [
+				'feather' => [
+					'class' => FeatherIcon::class,
+				],
+			],
+			'separator' => ':',
+		];
+		$result = (new IconCollection($config))->render('foo');
+
+		$this->assertSame('<span data-feather="foo" title="Foo"></span>', $result);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testRenderNamespaced(): void {
+		$config = [
+			'sets' => [
+				'feather' => [
+					'class' => FeatherIcon::class,
+				],
+				'material' => [
+					'class' => MaterialIcon::class,
+					'namespace' => 'material-symbols',
+				],
+			],
+			'separator' => ':',
+			'attributes' => [
+				'data-default' => 'some-default',
+			],
+		];
+		$result = (new IconCollection($config))->render('material:foo');
+
+		$this->assertSame('<span class="material-symbols" title="Foo" data-default="some-default">foo</span>', $result);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testNames(): void {
 		$config = [
 		$config = [
 			'sets' => [
 			'sets' => [
 				'feather' => [
 				'feather' => [