Skip to content

Commit 9a5501b

Browse files
PierrePaulchesn0k
andauthored
Add support for SDC slots (#191)
Co-authored-by: chesn0k <[email protected]>
1 parent d0a6ed7 commit 9a5501b

File tree

6 files changed

+73
-14
lines changed

6 files changed

+73
-14
lines changed

src/Command/SingleDirectoryComponent.php

+30-3
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,25 @@ private function askQuestions(array &$vars): void {
8585
} while ($library !== NULL);
8686

8787
$vars['component_libraries'] = \array_filter($vars['component_libraries']);
88-
$vars['component_has_css'] = $ir->confirm('Needs CSS?');
89-
$vars['component_has_js'] = $ir->confirm('Needs JS?');
90-
if ($ir->confirm('Needs component props?')) {
88+
$vars['component_has_css'] = $ir->confirm('Need CSS?');
89+
$vars['component_has_js'] = $ir->confirm('Need JS?');
90+
if ($ir->confirm('Need component props?')) {
9191
$vars['component_props'] = [];
9292
do {
9393
$prop = $this->askProp($vars, $ir);
9494
$vars['component_props'][] = $prop;
9595
} while ($ir->confirm('Add another prop?'));
9696
}
9797
$vars['component_props'] = \array_filter($vars['component_props'] ?? []);
98+
99+
if ($ir->confirm('Need slots?')) {
100+
$vars['component_slots'] = [];
101+
do {
102+
$slot = $this->askSlot($vars, $ir);
103+
$vars['component_slots'][] = $slot;
104+
} while ($ir->confirm('Add another slot?'));
105+
}
106+
$vars['component_slots'] = \array_filter($vars['component_slots'] ?? []);
98107
}
99108

100109
/**
@@ -191,4 +200,22 @@ protected function askProp(array $vars, Interviewer $ir): array {
191200
return $prop;
192201
}
193202

203+
/**
204+
* Asks for multiple questions to define a slot.
205+
*
206+
* @psalm-param array{component_machine_name: mixed, ...<array-key, mixed>} $vars
207+
* The answers to the CLI questions.
208+
*
209+
* @return array
210+
* The slot data, if any.
211+
*/
212+
protected function askSlot(array $vars, Interviewer $ir): array {
213+
$slot = [];
214+
$slot['title'] = $ir->ask('Slot title', '', new Required());
215+
$default = Utils::human2machine($slot['title']);
216+
$slot['name'] = $ir->ask('Slot machine name', $default, new RequiredMachineName());
217+
$slot['description'] = $ir->ask('Slot description (optional)');
218+
return $slot;
219+
}
220+
194221
}

templates/_sdc/component.twig

+10
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,13 @@ props:
2626
examples: []
2727
{% endfor %}
2828
{% endif %}
29+
{% if component_slots|length > 0 %}
30+
slots:
31+
{% for slot in component_slots %}
32+
{{ slot.name }}:
33+
title: {{ slot.title }}
34+
{% if slot.description %}
35+
description: {{ slot.description }}
36+
{% endif %}
37+
{% endfor %}
38+
{% endif %}

templates/_sdc/template.twig

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<div {{ '{{ attributes }}' }}>
2-
{{ '{# ' }}Keep the outer div whenever possible it is a nice way to ensure the presence of "attributes" {{ '#}' }}
3-
{{ '{% ' }}block example_block {{ '%}' }}
4-
The contents of the example block
5-
{{ '{% ' }}endblock {{ '%}' }}
2+
{% if component_slots|length > 0 %}
3+
{% for slot in component_slots %}
4+
{{ '{% block ' }}{{ slot.name }}{{ ' %}' }}{{ '{% endblock %}' }}
5+
{% endfor %}
6+
{% endif %}
67
</div>

tests/functional/Generator/SingleDirectoryComponentTest.php

+23-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public function testGenerator(): void {
3535
'A text for CTA button.',
3636
'String',
3737
'No',
38+
'Yes',
39+
'My slot',
40+
'',
41+
'Some description for slot.',
42+
'No',
3843
];
3944
$this->execute(SingleDirectoryComponent::class, $input);
4045

@@ -64,13 +69,13 @@ public function testGenerator(): void {
6469
Library dependencies (optional). [Example: core/once]:
6570
6671
67-
Needs CSS? [Yes]:
72+
Need CSS? [Yes]:
6873
6974
70-
Needs JS? [Yes]:
75+
Need JS? [Yes]:
7176
7277
73-
Needs component props? [Yes]:
78+
Need component props? [Yes]:
7479
7580
7681
Prop title:
@@ -94,6 +99,21 @@ public function testGenerator(): void {
9499
Add another prop? [Yes]:
95100
96101
102+
Need slots? [Yes]:
103+
104+
105+
Slot title:
106+
107+
108+
Slot machine name [my_slot]:
109+
110+
111+
Slot description (optional):
112+
113+
114+
Add another slot? [Yes]:
115+
116+
97117
The following directories and files have been created or updated:
98118
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
99119
• components/bar/bar.component.yml

tests/functional/Generator/_sdc/components/bar/bar.component.yml

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ props:
1414
description: A text for CTA button.
1515
# @todo Add examples here.
1616
examples: []
17+
slots:
18+
my_slot:
19+
title: My slot
20+
description: Some description for slot.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
<div {{ attributes }}>
2-
{# Keep the outer div whenever possible it is a nice way to ensure the presence of "attributes" #}
3-
{% block example_block %}
4-
The contents of the example block
5-
{% endblock %}
2+
{% block my_slot %}{% endblock %}
63
</div>

0 commit comments

Comments
 (0)