Skip to content
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

[beam] add debounce to list item input #190

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions beam/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"test:ui": "vitest --ui"
},
"dependencies": {
"@vueuse/core": "^11.1.0",
"mqtt": "^5.10.1",
"onscan.js": "^1.5.2",
"vue": "^3.5.6"
Expand Down
23 changes: 14 additions & 9 deletions beam/src/components/ItemCount.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<span
:contenteditable="editable"
:class="{ 'beam--alert': !isCountComplete }"
@input="handleInput"
@click="handleInput">
@click.stop.prevent="validate"
@input.stop.prevent="debouncedValidate"
@paste="validate">
{{ count }}
</span>
<span>/{{ denominator }}</span>
Expand All @@ -13,25 +14,29 @@
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useDebounceFn } from '@vueuse/core'
import { computed, type HTMLAttributes } from 'vue'

const count = defineModel<number>({ required: true })
const {
denominator,
uom = '',
debounce = 300,
editable = true,
uom = '',
} = defineProps<{
denominator: number
debounce?: number
editable?: HTMLAttributes['contenteditable']
uom?: string
editable?: boolean
}>()

const isCountComplete = computed(() => count.value === denominator)

const handleInput = (event: InputEvent | MouseEvent) => {
event.preventDefault()
event.stopPropagation()
const newValue = Number((event.target as HTMLElement).innerHTML) || 0
const validate = (payload: ClipboardEvent | InputEvent | MouseEvent) => {
const newValue = Number((payload.target as HTMLElement).innerHTML) || 0
count.value = Math.min(newValue, denominator)
}

const debouncedRequest = useDebounceFn((payload: InputEvent) => validate(payload), debounce)
const debouncedValidate = async (payload: InputEvent) => await debouncedRequest(payload)
</script>
19 changes: 5 additions & 14 deletions beam/src/components/ListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
<ItemCount
v-if="listItem.count"
v-model="listItem.count.count"
:debounce="listItem.debounce"
:denominator="listItem.count.of"
:uom="listItem.count.uom"
:editable="true" />
:editable="true"
:uom="listItem.count.uom" />
<ItemCheck v-if="listItem.hasOwnProperty('checked')" v-model="listItem.checked" />
</li>
</template>
Expand All @@ -20,19 +21,9 @@ import { ref } from 'vue'

import ItemCount from '@/components/ItemCount.vue'
import ItemCheck from '@/components/ItemCheck.vue'
import type { ListViewItem } from '@/types'

const { item } = defineProps<{
item: {
label: string
description: string
count?: {
count: number
of: number
uom: string
}
checked?: boolean
}
}>()
const { item } = defineProps<{ item: ListViewItem }>()

const listItem = ref(item)
</script>
17 changes: 2 additions & 15 deletions beam/src/components/ListView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,9 @@
import { onMounted, onUnmounted } from 'vue'

import ListItem from '@/components/ListItem.vue'
import type { ListViewItem } from '@/types'

defineProps<{
items: {
label: string
description: string
count?: {
count: number
of: number
uom: string
}
checked?: boolean
linkComponent?: string
route?: string
}[]
}>()

defineProps<{ items: ListViewItem[] }>()
const emit = defineEmits<{ scrollbottom: [] }>()

onMounted(() => {
Expand Down
13 changes: 13 additions & 0 deletions beam/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type ListViewItem = {
description: string
label: string
checked?: boolean
debounce?: number
linkComponent?: string
route?: string
count?: {
count: number
of: number
uom: string
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"packageName": "@stonecrop/beam",
"comment": "added toast notifications",
"type": "none"
"type": "patch"
}
],
"packageName": "@stonecrop/beam"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@stonecrop/beam",
"comment": "add debounce to list item input",
"type": "patch"
}
],
"packageName": "@stonecrop/beam"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"packageName": "@stonecrop/themes",
"comment": "added warning color variable",
"type": "none"
"type": "patch"
}
],
"packageName": "@stonecrop/themes"
Expand Down
3 changes: 3 additions & 0 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/beam/default.story.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<BeamHeading> WO#2024-01-00001 <span class="beam--normal">Ambrosia Pie</span> </BeamHeading>
</template>
<template #right>
<ItemCount denominator="10" model-value="5" />
<ItemCount denominator="20" model-value="5" />
</template>
</SplitColumn>
<BeamProgress :complete="workOrder.complete" progress-message="In Transit" />
Expand Down
Loading