Skip to content

Commit 32b8b74

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

7 files changed

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

56+
if (is_array($definition->getOptionsValue('cssClasses'))
57+
&& [] !== $definition->getOptionsValue('cssClasses')
58+
) {
59+
$form['#attributes']['class'] ??= [];
60+
$form['#attributes']['class'] = [
61+
...$form['#attributes']['class'],
62+
...$definition->getOptionsValue('cssClasses'),
63+
];
64+
}
65+
5666
return $form;
5767
}
5868
}

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ 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+
return $this->uiSchema->options->{$key} ?? $default;
59+
}
60+
5361
/**
5462
* {@inheritDoc}
5563
*/

src/JsonForms/Definition/Custom/MarkupDefinition.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ 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+
return $this->markupSchema->options->{$key} ?? $default;
61+
}
62+
5563
/**
5664
* {@inheritDoc}
5765
*/

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)