Skip to content

Commit 0a1ebc9

Browse files
author
Sebastian Thulin
committed
Merge branch 'main' of github.com:helsingborg-stad/modularity-frontend-form
2 parents ed3cf02 + 6b7031c commit 0a1ebc9

File tree

7 files changed

+126
-8
lines changed

7 files changed

+126
-8
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Message implements FieldInterface {
2+
constructor(
3+
private field: HTMLElement,
4+
private name: string,
5+
private conditionValidator: ConditionValidatorInterface,
6+
private conditionsHandler: ConditionsHandlerInterface
7+
) {
8+
}
9+
10+
public init(conditionBuilder: ConditionBuilderInterface): void {
11+
this.conditionsHandler.init(this, conditionBuilder);
12+
this.conditionValidator.init(this);
13+
}
14+
15+
public getName(): string {
16+
return this.name;
17+
}
18+
19+
public getConditionsHandler(): ConditionsHandlerInterface {
20+
return this.conditionsHandler;
21+
}
22+
23+
public getConditionValidator(): ConditionValidatorInterface {
24+
return this.conditionValidator;
25+
}
26+
27+
public getField(): HTMLElement {
28+
return this.field;
29+
}
30+
}
31+
32+
export default Message;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class MessageConditionsHandler implements ConditionsHandlerInterface {
2+
private parent: FieldInterface|null = null;
3+
private conditions: ConditionInterface[] = [];
4+
private isDisabled: boolean = false;
5+
6+
constructor(private unstructuredConditions: any) {
7+
}
8+
9+
public init(parent: FieldInterface, conditionsBuilder: ConditionBuilderInterface): void {
10+
this.parent = parent;
11+
this.conditions = conditionsBuilder.build(this.unstructuredConditions);
12+
}
13+
14+
private updateDisabled(disabled: boolean): void {
15+
if (this.parent && this.isDisabled !== disabled) {
16+
this.isDisabled = disabled;
17+
18+
this.parent.getField().classList.toggle('u-display--none', disabled);
19+
}
20+
}
21+
22+
public validate(): void {
23+
let isValid: boolean = false;
24+
console.log(this.getConditions());
25+
for (const condition of this.getConditions()) {
26+
if (condition.validate()) {
27+
isValid = true;
28+
break;
29+
}
30+
}
31+
32+
this.updateDisabled(!isValid);
33+
}
34+
35+
public dispatchUpdateEvent(): void {
36+
return;
37+
}
38+
39+
public getIsDisabled(): boolean {
40+
return this.isDisabled;
41+
}
42+
43+
public getConditions(): ConditionInterface[] {
44+
return this.conditions;
45+
}
46+
47+
public addValueChangeListener(field: FieldInterface): void {
48+
return;
49+
}
50+
51+
private setValueChangeListener(): void {
52+
return;
53+
}
54+
}
55+
56+
export default MessageConditionsHandler;

source/js/fields/field/radio/radioConditionValidator.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class RadioConditionValidator implements ConditionValidatorInterface {
77

88
public validate(condition: Condition): boolean {
99
const selected = this.parent?.getSelectedChoice() ?? '';
10-
console.log(condition, 'selected', selected);
1110

1211
switch (condition.operator) {
1312
case '==':

source/js/fields/fieldBuilder.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import SelectConditionValidator from "./field/select/selectConditionValidator";
1313
import Radio from "./field/radio/radio";
1414
import RadioConditionValidator from "./field/radio/radioConditionValidator";
1515
import RadioConditionsHandler from "./field/radio/radioConditionsHandler";
16+
import Message from "./field/message/message";
17+
import MessageConditionsHandler from "./field/message/messageConditionHandler";
1618

1719
class FieldBuilder implements FieldBuilderInterface {
1820
private name: string = 'data-js-field-name';
@@ -32,12 +34,15 @@ class FieldBuilder implements FieldBuilderInterface {
3234
case 'url':
3335
case 'date':
3436
case 'time':
37+
case 'number':
3538
return this.buildText(field);
3639
case 'select':
3740
return this.buildSelect(field);
3841
case 'radio':
3942
case 'trueFalse':
4043
return this.buildRadio(field);
44+
case 'message':
45+
return this.buildMessage(field);
4146
}
4247

4348
return this.buildNullField(field, type);
@@ -53,6 +58,15 @@ class FieldBuilder implements FieldBuilderInterface {
5358
);
5459
}
5560

61+
private buildMessage(field: HTMLElement): FieldInterface {
62+
return new Message(
63+
field,
64+
this.getFieldName(field),
65+
new NullFieldConditionValidator(),
66+
new MessageConditionsHandler(this.getFieldCondition(field))
67+
)
68+
}
69+
5670
private buildRadio(field: HTMLElement): FieldInterface {
5771
const choices = field.querySelectorAll('input[type="radio"]') as NodeListOf<HTMLInputElement>;
5872

@@ -90,8 +104,15 @@ class FieldBuilder implements FieldBuilderInterface {
90104
}
91105

92106
private buildText(field: HTMLElement): FieldInterface {
93-
const input = field.querySelector('input:is([type="text"], [type="email"], [type="url"], [type="date"], [type="time"])') as HTMLInputElement;
94-
console.log(field);
107+
const input = field.querySelector(`input:is(
108+
[type="text"],
109+
[type="email"],
110+
[type="url"],
111+
[type="date"],
112+
[type="time"],
113+
[type="number"]
114+
)`) as HTMLInputElement;
115+
95116
if (!input) {
96117
console.error('Text field is not an input element with type "text", "email" or "url", "date" or "time"');
97118
return this.buildNullField(field, 'text');

source/php/Module/FormatSteps.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ private function fieldMapper(array $field)
6868
case 'url':
6969
return $this->mapUrl($field);
7070
case 'textarea':
71-
case 'message':
7271
return $this->mapTextarea($field);
72+
case 'message':
73+
return $this->mapBasic($field, 'message');
7374
case 'radio':
7475
return $this->mapRadio($field);
7576
case 'number':
@@ -125,10 +126,12 @@ private function mapNumber(array $field): array
125126
{
126127
// TODO: Append ex. SEK?
127128
$mapped = $this->mapBasic($field, 'number');
128-
$mapped['placeholder'] = $field['placeholder'] ?? '';
129-
$mapped['value'] = $field['default_value'] ?? '';
130-
$mapped['min'] = $field['min'] ?? null;
131-
$mapped['max'] = $field['max'] ?? null;
129+
130+
$mapped['placeholder'] = $field['placeholder'] ?? '';
131+
$mapped['value'] = $field['default_value'] ?? '';
132+
$mapped['moveAttributesListToFieldAttributes'] = false;
133+
$mapped['attributeList']['min'] = $field['min'] ?? null;
134+
$mapped['attributeList']['max'] = $field['max'] ?? null;
132135

133136
return $mapped;
134137
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@typography([
2+
'attributeList' => $field['attributeList'],
3+
])
4+
HELLO
5+
@endtypography
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@field($field)
2+
@endfield

0 commit comments

Comments
 (0)