-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(templates): checkbox field type #47290
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<!-- | ||
- SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
- SPDX-License-Identifier: AGPL-3.0-or-later | ||
--> | ||
|
||
<template> | ||
<div class="template-field__checkbox"> | ||
<NcCheckboxRadioSwitch :id="fieldId" | ||
:checked.sync="value" | ||
type="switch" | ||
@update:checked="input"> | ||
{{ fieldLabel }} | ||
</NcCheckboxRadioSwitch> | ||
</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 | ||
}, | ||
}, | ||
|
||
methods: { | ||
input() { | ||
this.$emit('input', { | ||
index: this.field.index, | ||
property: 'checked', | ||
value: this.value, | ||
}) | ||
}, | ||
}, | ||
}) | ||
</script> | ||
|
||
<style lang="scss" scoped> | ||
.template-field__checkbox { | ||
margin: 20px 0; | ||
} | ||
</style> |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3235-3235.js.license |
This file was deleted.
This file was deleted.
This file was deleted.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCP\Files\Template; | ||
|
||
use OCP\Files\Template\Fields\CheckBoxField; | ||
use OCP\Files\Template\Fields\RichTextField; | ||
|
||
/** | ||
* @since 30.0.0 | ||
*/ | ||
class FieldFactory { | ||
/** | ||
* @since 30.0.0 | ||
*/ | ||
public static function createField( | ||
string $index, | ||
FieldType $type | ||
): Field { | ||
return match ($type) { | ||
FieldType::RichText => new RichTextField($index, $type), | ||
FieldType::CheckBox => new CheckBoxField($index, $type), | ||
default => throw new InvalidFieldTypeException(), | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCP\Files\Template\Fields; | ||
|
||
use OCP\Files\Template\Field; | ||
use OCP\Files\Template\FieldType; | ||
|
||
/** | ||
* @since 30.0.0 | ||
*/ | ||
class CheckBoxField extends Field { | ||
private bool $checked = false; | ||
|
||
/** | ||
* @since 30.0.0 | ||
*/ | ||
public function __construct(string $index, FieldType $type) { | ||
parent::__construct($index, $type); | ||
} | ||
|
||
/** | ||
* @since 30.0.0 | ||
*/ | ||
public function setValue(mixed $value): void { | ||
if (!is_bool($value)) { | ||
throw new \Exception('Invalid value for checkbox field type'); | ||
} | ||
|
||
$this->checked = $value; | ||
} | ||
|
||
/** | ||
* @since 30.0.0 | ||
*/ | ||
public function jsonSerialize(): array { | ||
$jsonProperties = parent::jsonSerialize(); | ||
|
||
return array_merge($jsonProperties, ['checked' => $this->checked]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCP\Files\Template\Fields; | ||
|
||
use OCP\Files\Template\Field; | ||
use OCP\Files\Template\FieldType; | ||
|
||
/** | ||
* @since 30.0.0 | ||
*/ | ||
class RichTextField extends Field { | ||
private string $content = ''; | ||
|
||
/** | ||
* @since 30.0.0 | ||
*/ | ||
public function __construct(string $index, FieldType $type) { | ||
parent::__construct($index, $type); | ||
} | ||
|
||
/** | ||
* @since 30.0.0 | ||
*/ | ||
public function setValue(mixed $value): void { | ||
if (!is_string($value)) { | ||
throw new \Exception('Invalid value for rich-text field type'); | ||
} | ||
|
||
$this->content = $value; | ||
} | ||
|
||
/** | ||
* @since 30.0.0 | ||
*/ | ||
public function jsonSerialize(): array { | ||
$jsonProperties = parent::jsonSerialize(); | ||
|
||
return array_merge($jsonProperties, ['content' => $this->content]); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why reverting to JS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't using TypeScript in this file, but if it makes it more consistent with the rest of the files I can leave it as TS.