feat: colorPicker script setup (#2457)

Co-authored-by: Dominik Pschenitschni <mail@celement.de>
Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/2457
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-02 10:00:19 +00:00 committed by konrad
parent 0620b8f0b3
commit b08dd58552

View file

@ -34,9 +34,8 @@
</div> </div>
</template> </template>
<script lang="ts"> <script setup lang="ts">
import {defineComponent} from 'vue' import {computed, ref, toRef, watch} from 'vue'
import {createRandomID} from '@/helpers/randomId' import {createRandomID} from '@/helpers/randomId'
const DEFAULT_COLORS = [ const DEFAULT_COLORS = [
@ -48,17 +47,12 @@ const DEFAULT_COLORS = [
'#00db60', '#00db60',
] ]
export default defineComponent({ const color = ref('')
name: 'colorPicker', const lastChangeTimeout = ref<ReturnType<typeof setTimeout> | null>(null)
data() { const defaultColors = ref(DEFAULT_COLORS)
return { const colorListID = ref(createRandomID())
color: '',
lastChangeTimeout: null, const props = defineProps({
defaultColors: DEFAULT_COLORS,
colorListID: createRandomID(),
}
},
props: {
modelValue: { modelValue: {
type: String, type: String,
required: true, required: true,
@ -67,47 +61,43 @@ export default defineComponent({
type: String, type: String,
default: 'top', default: 'top',
}, },
}, })
emits: ['update:modelValue'],
watch: {
modelValue: {
handler(modelValue) {
this.color = modelValue
},
immediate: true,
},
color() {
this.update()
},
},
computed: {
isEmpty() {
return this.color === '#000000' || this.color === ''
},
},
methods: {
update(force = false) {
if(this.isEmpty && !force) { const emit = defineEmits(['update:modelValue'])
const modelValue = toRef(props, 'modelValue')
watch(
modelValue,
(newValue) => {
color.value = newValue
},
{immediate: true},
)
watch(color, () => update())
const isEmpty = computed(() => color.value === '#000000' || color.value === '')
function update(force = false) {
if(isEmpty.value && !force) {
return return
} }
if (this.lastChangeTimeout !== null) { if (lastChangeTimeout.value !== null) {
clearTimeout(this.lastChangeTimeout) clearTimeout(lastChangeTimeout.value)
} }
this.lastChangeTimeout = setTimeout(() => { lastChangeTimeout.value = setTimeout(() => {
this.$emit('update:modelValue', this.color) emit('update:modelValue', color.value)
}, 500) }, 500)
}, }
reset() {
function reset() {
// FIXME: I havn't found a way to make it clear to the user the color war reset. // FIXME: I havn't found a way to make it clear to the user the color war reset.
// Not sure if verte is capable of this - it does not show the change when setting this.color = '' // Not sure if verte is capable of this - it does not show the change when setting this.color = ''
this.color = '' color.value = ''
this.update(true) update(true)
}, }
},
})
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>