Skip to content

Commit

Permalink
feat(templates): modify Field class to support checkboxes
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Danzberger <[email protected]>
  • Loading branch information
elzody committed Aug 26, 2024
1 parent c6ec822 commit 8a7e847
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 33 deletions.
30 changes: 20 additions & 10 deletions apps/files/src/components/TemplateFiller.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
<form>
<h3>{{ t('files', 'Fill template fields') }}</h3>

<!-- We will support more than just text fields in the future -->
<div v-for="field in fields" :key="field.index">
<TemplateTextField v-if="field.type == 'rich-text'"
:field="field"
@input="trackInput" />
<component :is="getFieldComponent(field.type)" :field="field" @input="trackInput" />
</div>
</form>
</div>
Expand All @@ -29,11 +26,12 @@
</NcModal>
</template>

<script lang="ts">
<script>
import { defineComponent } from 'vue'
import { NcModal, NcButton, NcLoadingIcon } from '@nextcloud/vue'
import { translate as t } from '@nextcloud/l10n'
import TemplateTextField from './TemplateFiller/TemplateTextField.vue'
import TemplateRichTextField from './TemplateFiller/TemplateRichTextField.vue'
import TemplateCheckboxField from './TemplateFiller/TemplateCheckboxField.vue'
export default defineComponent({
name: 'TemplateFiller',
Expand All @@ -42,7 +40,8 @@ export default defineComponent({
NcModal,
NcButton,
NcLoadingIcon,
TemplateTextField,
TemplateRichTextField,
TemplateCheckboxField,
},
props: {
Expand All @@ -65,10 +64,21 @@ export default defineComponent({
methods: {
t,
trackInput([value, index]) {
this.localFields[index] = {
content: value,
trackInput([index, name, value]) {
if (!this.localFields[index]) {
this.localFields[index] = {}
}
this.localFields[index][name] = value
},
getFieldComponent(fieldType) {
const fieldComponentType = fieldType.split('-')
.map((str) => {
return str.charAt(0).toUpperCase() + str.slice(1)
})
.join('')
return `Template${fieldComponentType}Field`
},
async submit() {
this.loading = true
Expand Down
67 changes: 67 additions & 0 deletions apps/files/src/components/TemplateFiller/TemplateCheckboxField.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!--
- SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->

<template>
<div class="template-field__checkbox">
<label :for="fieldId">
{{ fieldLabel }}
</label>

<NcCheckboxRadioSwitch :id="fieldId"
:checked.sync="value"
type="switch"
@update:checked="$emit('input', [field.index, 'checked', value])" />
</div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { NcCheckboxRadioSwitch } from '@nextcloud/vue'
export default defineComponent({
name: 'TemplateCheckboxField',
components: {
NcCheckboxRadioSwitch,
},
props: {
field: {
type: Object,
default: () => {},
},
},
data() {
return {
value: this.field.checked ?? false,
}
},
computed: {
fieldLabel() {
const label = this.field.name ?? this.field.alias ?? 'Unknown field'
return label.charAt(0).toUpperCase() + label.slice(1)
},
fieldId() {
return 'checkbox-field' + this.field.index
},
},
})
</script>

<style lang="scss" scoped>
.template-field__checkbox {
margin: 20px 0;
display: flex;
align-items: center;
label {
font-weight: bold;
flex-grow: 1;
}
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
:label="fieldLabel"
:label-outside="true"
:placeholder="field.content"
@input="$emit('input', [value, field.index])" />
@input="$emit('input', [field.index, 'content', value])" />
</div>
</template>

Expand All @@ -24,7 +24,7 @@ import { defineComponent } from 'vue'
import { NcTextField } from '@nextcloud/vue'
export default defineComponent({
name: 'TemplateTextField',
name: 'TemplateRichTextField',
components: {
NcTextField,
Expand Down
45 changes: 24 additions & 21 deletions lib/public/Files/Template/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,39 @@
* @since 30.0.0
*/
class Field implements \JsonSerializable {
private string $index;
private string $content;
private FieldType $type;
private ?string $alias;
private ?int $id;
private ?string $tag;
public ?string $content = null;
public ?string $alias = null;
public ?string $tag = null;
public ?int $id = null;
public ?bool $checked = null;

/**
* @since 30.0.0
*/
public function __construct(string $index, string $content, FieldType $type, ?string $alias = null, ?int $id = null, ?string $tag = null) {
$this->index = $index;
$this->alias = $alias;
$this->type = $type;
$this->id = $id;
$this->tag = $tag;
$this->content = $content;
public function __construct(
private string $index,
private FieldType $type
) {
}

/**
* @since 30.0.0
*/
public function jsonSerialize(): array {
return [
'index' => $this->index,
'content' => $this->content,
'type' => $this->type->value,
'alias' => $this->alias,
'id' => $this->id,
'tag' => $this->tag,
];
$jsonProperties = [];

foreach (get_object_vars($this) as $propertyName => $propertyValue) {
if (is_null($propertyValue)) {
continue;
}

if ($propertyValue instanceof FieldType) {
$propertyValue = $propertyValue->value;
}

array_push($jsonProperties, [$propertyName => $propertyValue]);
}

return array_merge([], ...$jsonProperties);
}
}

0 comments on commit 8a7e847

Please sign in to comment.