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,66 +47,57 @@ 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,
defaultColors: DEFAULT_COLORS,
colorListID: createRandomID(),
}
},
props: {
modelValue: {
type: String,
required: true,
},
menuPosition: {
type: String,
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 props = defineProps({
return modelValue: {
} type: String,
required: true,
if (this.lastChangeTimeout !== null) { },
clearTimeout(this.lastChangeTimeout) menuPosition: {
} type: String,
default: 'top',
this.lastChangeTimeout = setTimeout(() => {
this.$emit('update:modelValue', this.color)
}, 500)
},
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 = ''
this.color = ''
this.update(true)
},
}, },
}) })
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
}
if (lastChangeTimeout.value !== null) {
clearTimeout(lastChangeTimeout.value)
}
lastChangeTimeout.value = setTimeout(() => {
emit('update:modelValue', color.value)
}, 500)
}
function 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 = ''
color.value = ''
update(true)
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>