Skip to content

Commit

Permalink
Fix camelCase props not working on links
Browse files Browse the repository at this point in the history
  • Loading branch information
thepaperpilot committed May 21, 2023
1 parent e0f1296 commit 6ad08c4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/features/boards/BoardLink.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<line
class="link"
v-bind="link"
v-bind="linkProps"
:class="{ pulsing: link.pulsing }"
:x1="startPosition.x"
:y1="startPosition.y"
Expand All @@ -12,6 +12,7 @@

<script setup lang="ts">
import type { BoardNode, BoardNodeLink } from "features/boards/board";
import { kebabifyObject } from "util/vue";
import { computed, toRefs, unref } from "vue";
const _props = defineProps<{
Expand Down Expand Up @@ -49,6 +50,8 @@ const endPosition = computed(() => {
}
return position;
});
const linkProps = computed(() => kebabifyObject(_props.link as unknown as Record<string, unknown>));
</script>

<style scoped>
Expand Down
5 changes: 4 additions & 1 deletion src/features/links/Link.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<line
stroke-width="15px"
stroke="white"
v-bind="link"
v-bind="linkProps"
:x1="startPosition.x"
:y1="startPosition.y"
:x2="endPosition.x"
Expand All @@ -13,6 +13,7 @@
<script setup lang="ts">
import type { Link } from "features/links/links";
import type { FeatureNode } from "game/layers";
import { kebabifyObject } from "util/vue";
import { computed, toRefs } from "vue";
const _props = defineProps<{
Expand Down Expand Up @@ -54,4 +55,6 @@ const endPosition = computed(() => {
}
return position;
});
const linkProps = computed(() => kebabifyObject(_props.link as unknown as Record<string, unknown>));
</script>
5 changes: 5 additions & 0 deletions src/util/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export function camelToTitle(camel: string): string {
return title;
}

export function camelToKebab(camel: string) {
// Split off first character so function works on upper camel (pascal) case
return (camel[0] + camel.slice(1).replace(/[A-Z]/g, c => `-${c}`)).toLowerCase();
}

export function isFunction<T, S extends ReadonlyArray<unknown>, R>(
functionOrValue: ((...args: S) => T) | R
): functionOrValue is (...args: S) => T {
Expand Down
8 changes: 8 additions & 0 deletions src/util/vue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
unref,
watchEffect
} from "vue";
import { camelToKebab } from "./common";

export function coerceComponent(
component: CoercableComponent,
Expand Down Expand Up @@ -241,3 +242,10 @@ export function trackHover(element: VueFeature): Ref<boolean> {

return isHovered;
}

export function kebabifyObject(object: Record<string, unknown>) {
return Object.keys(object).reduce((acc, curr) => {
acc[camelToKebab(curr)] = object[curr];
return acc;
}, {} as Record<string, unknown>);
}

0 comments on commit 6ad08c4

Please sign in to comment.