feat: fancycheckbox script setup (#2462)
continuous-integration/drone/push Build is passing Details

Co-authored-by: Dominik Pschenitschni <mail@celement.de>
Reviewed-on: #2462
Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:
Dominik Pschenitschni 2022-10-03 20:19:35 +00:00 committed by konrad
parent 9022954257
commit 06c1a54886
1 changed files with 32 additions and 36 deletions

View File

@ -4,8 +4,9 @@
:checked="checked" :checked="checked"
:disabled="disabled || undefined" :disabled="disabled || undefined"
:id="checkBoxId" :id="checkBoxId"
@change="(event) => updateData(event.target.checked)" @change="(event: Event) => updateData((event.target as HTMLInputElement).checked)"
type="checkbox"/> type="checkbox"
/>
<label :for="checkBoxId" class="check"> <label :for="checkBoxId" class="check">
<svg height="18px" viewBox="0 0 18 18" width="18px"> <svg height="18px" viewBox="0 0 18 18" width="18px">
<path <path
@ -19,47 +20,42 @@
</div> </div>
</template> </template>
<script lang="ts"> <script setup lang="ts">
import {defineComponent} from 'vue' import {ref, toRef, watch} from 'vue'
import {createRandomID} from '@/helpers/randomId' import {createRandomID} from '@/helpers/randomId'
export default defineComponent({ const checked = ref(false)
name: 'fancycheckbox', const checkBoxId = `fancycheckbox_${createRandomID()}`
data() {
return {
checked: false,
checkBoxId: `fancycheckbox_${createRandomID()}`,
}
},
props: {
modelValue: {
required: false,
},
disabled: {
type: Boolean,
required: false,
default: false,
},
},
emits: ['update:modelValue', 'change'],
watch: {
modelValue: {
handler(modelValue) {
this.checked = modelValue
}, const props = defineProps({
immediate: true, modelValue: {
}, type: Boolean,
required: false,
}, },
methods: { disabled: {
updateData(checked: boolean) { type: Boolean,
this.checked = checked required: false,
this.$emit('update:modelValue', checked) default: false,
this.$emit('change', checked)
},
}, },
}) })
const emit = defineEmits(['update:modelValue', 'change'])
const modelValue = toRef(props, 'modelValue')
watch(
modelValue,
newValue => {
checked.value = newValue
},
{immediate: true},
)
function updateData(newChecked: boolean) {
checked.value = newChecked
emit('update:modelValue', newChecked)
emit('change', newChecked)
}
</script> </script>