Skip to content

Commit 0f9c7f5

Browse files
author
Dominic Tubach
committed
Allow to define CSS classes for any element
1 parent fbaa2f6 commit 0f9c7f5

7 files changed

Lines changed: 54 additions & 14 deletions

File tree

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ be achieved with this CSS selector:
6161
```css
6262
:not(.json-forms-description-tooltip)
6363
```
64+
### Custom CSS Classes
65+
66+
For any control and layout it is possible to specify CSS classes in the UI
67+
schema that will be added to the generated HTML code. The classes can be set at
68+
the property `cssClasses` in the `options` property, e.g.:
69+
70+
```json
71+
{
72+
"type": "VerticalLayout",
73+
"elements": [
74+
{
75+
"type": "Control",
76+
"scope": "#/properties/name",
77+
"options": {
78+
"cssClasses": ["my-name-control-class"]
79+
}
80+
}
81+
],
82+
"options": {
83+
"cssClasses": ["my-layout-class"]
84+
}
85+
}
86+
```
6487

6588
## Limitations
6689

src/Form/FormArrayFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public function createFormArray(DefinitionInterface $definition, FormStateInterf
5353
$formState->set(['form', $definition->getPropertyFormName()], $form);
5454
}
5555

56+
// @phpstan-ignore offsetAccess.nonOffsetAccessible
57+
$form['#attributes']['class'] =
58+
// @phpstan-ignore binaryOp.invalid
59+
($definition->getOptionsValue('cssClasses') ?? [])
60+
// @phpstan-ignore offsetAccess.nonOffsetAccessible
61+
+ ($form['#attributes']['class'] ?? []);
62+
5663
return $form;
5764
}
5865
}

src/JsonForms/Definition/Control/ControlDefinition.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,7 @@ public function getOptions(): ?\stdClass {
216216
return $this->controlSchema->options ?? NULL;
217217
}
218218

219-
/**
220-
* @param string $key
221-
* @param mixed $default
222-
*
223-
* @return mixed
224-
*/
225-
public function getOptionsValue(string $key, $default = NULL) {
219+
public function getOptionsValue(string $key, mixed $default = NULL): mixed {
226220
return $this->controlSchema->options->{$key} ?? $default;
227221
}
228222

src/JsonForms/Definition/Custom/CustomDefinition.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ public function __construct(
5050
$this->rootDefinition = $rootDefinition ?? $this;
5151
}
5252

53+
public function getOptions(): ?\stdClass {
54+
return $this->uiSchema->options ?? NULL;
55+
}
56+
57+
public function getOptionsValue(string $key, mixed $default = NULL): mixed {
58+
// @phpstan-ignore property.dynamicName
59+
return $this->uiSchema->options->{$key} ?? $default;
60+
}
61+
5362
/**
5463
* {@inheritDoc}
5564
*/

src/JsonForms/Definition/Custom/MarkupDefinition.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public function getContentMediaType(): string {
5252
return $this->markupSchema->contentMediaType;
5353
}
5454

55+
public function getOptions(): ?\stdClass {
56+
return $this->markupSchema->options ?? NULL;
57+
}
58+
59+
public function getOptionsValue(string $key, mixed $default = NULL): mixed {
60+
// @phpstan-ignore property.dynamicName
61+
return $this->markupSchema->options->{$key} ?? $default;
62+
}
63+
5564
/**
5665
* {@inheritDoc}
5766
*/

src/JsonForms/Definition/DefinitionInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
interface DefinitionInterface {
2525

26+
public function getOptions(): ?\stdClass;
27+
28+
public function getOptionsValue(string $key, mixed $default = NULL): mixed;
29+
2630
/**
2731
* @param mixed $default
2832
*

src/JsonForms/Definition/Layout/LayoutDefinition.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,7 @@ public function getOptions(): ?\stdClass {
9494
return $this->layoutSchema->options ?? NULL;
9595
}
9696

97-
/**
98-
* @param string $key
99-
* @param mixed $default
100-
*
101-
* @return mixed
102-
*/
103-
public function getOptionsValue(string $key, $default = NULL) {
97+
public function getOptionsValue(string $key, mixed $default = NULL): mixed {
10498
return $this->layoutSchema->options->{$key} ?? $default;
10599
}
106100

0 commit comments

Comments
 (0)