Skip to content

Commit

Permalink
create useVanillaCell function and use it instead of useVanillaContro…
Browse files Browse the repository at this point in the history
…l at Cells. adjusted DispatchCell props.
  • Loading branch information
davewwww committed Feb 15, 2024
1 parent e108203 commit 0677ac8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 35 deletions.
19 changes: 9 additions & 10 deletions packages/vue-vanilla/src/cells/IntegerCell.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<template>
<input
:id="control.id + '-input'"
:id="cell.id + '-input'"
type="number"
:step="1"
:class="styles.control.input"
:value="control.data"
:disabled="!control.enabled"
:value="cell.data"
:disabled="!cell.enabled"
:autofocus="appliedOptions.focus"
:placeholder="appliedOptions.placeholder"
@change="onChange"
Expand All @@ -16,20 +16,19 @@

<script setup lang="ts">
import {
ControlElement,
CellProps,
isIntegerControl,
type RankedTester,
rankWith,
} from '@jsonforms/core';
import { rendererProps, useJsonFormsControl } from '@jsonforms/vue';
import { useVanillaControl } from '../util';
import { useJsonFormsCell } from '@jsonforms/vue';
import { useVanillaCell } from '../util';
const props = defineProps(rendererProps<ControlElement>());
const input = useVanillaControl(useJsonFormsControl(props), (target) =>
const props = defineProps<CellProps>();
const input = useVanillaCell(useJsonFormsCell(props), (target) =>
target.value === '' ? undefined : parseInt(target.value, 10)
);
const { styles, control, appliedOptions, onChange, isFocused } = input;
const { styles, cell, appliedOptions, onChange, isFocused } = input;
defineOptions({
tester: rankWith(1, isIntegerControl) as RankedTester,
Expand Down
19 changes: 9 additions & 10 deletions packages/vue-vanilla/src/cells/NumberCell.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<template>
<input
:id="control.id + '-input'"
:id="cell.id + '-input'"
type="number"
:step="step"
:class="styles.control.input"
:value="control.data"
:disabled="!control.enabled"
:value="cell.data"
:disabled="!cell.enabled"
:autofocus="appliedOptions.focus"
:placeholder="appliedOptions.placeholder"
@change="onChange"
Expand All @@ -16,21 +16,20 @@

<script setup lang="ts">
import {
ControlElement,
CellProps,
isNumberControl,
type RankedTester,
rankWith,
} from '@jsonforms/core';
import { rendererProps, useJsonFormsControl } from '@jsonforms/vue';
import { useVanillaControl } from '../util';
import { useJsonFormsCell } from '@jsonforms/vue';
import { useVanillaCell } from '../util';
import { computed } from 'vue';
const props = defineProps(rendererProps<ControlElement>());
const input = useVanillaControl(useJsonFormsControl(props), (target) =>
const props = defineProps<CellProps>();
const input = useVanillaCell(useJsonFormsCell(props), (target) =>
target.value === '' ? undefined : Number(target.value)
);
const { styles, control, appliedOptions, onChange, isFocused } = input;
const { styles, cell, appliedOptions, onChange, isFocused } = input;
const step = computed(() => {
const options: any = appliedOptions;
Expand Down
20 changes: 10 additions & 10 deletions packages/vue-vanilla/src/cells/TextCell.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<template>
<input
:id="control.id + '-input'"
:id="cell.id + '-input'"
:class="styles.control.input"
:value="control.data"
:disabled="!control.enabled"
:value="cell.data"
:disabled="!cell.enabled"
:autofocus="appliedOptions.focus"
:placeholder="appliedOptions.placeholder"
@change="onChange"
Expand All @@ -14,21 +14,21 @@

<script setup lang="ts">
import {
ControlElement,
CellProps,
isStringControl,
type RankedTester,
rankWith,
} from '@jsonforms/core';
import { rendererProps, useJsonFormsControl } from '@jsonforms/vue';
import { useVanillaControl } from '../util';
import { useJsonFormsCell } from '@jsonforms/vue';
import { useVanillaCell } from '../util';
const props = defineProps(rendererProps<ControlElement>());
const props = defineProps<CellProps>();
const input = useVanillaControl(
useJsonFormsControl(props),
const input = useVanillaCell(
useJsonFormsCell(props),
(target) => target.value || undefined
);
const { styles, control, appliedOptions, onChange, isFocused } = input;
const { styles, cell, appliedOptions, onChange, isFocused } = input;
defineOptions({
tester: rankWith(1, isStringControl) as RankedTester,
Expand Down
8 changes: 3 additions & 5 deletions packages/vue-vanilla/src/controls/InputControlRenderer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
:applied-options="appliedOptions"
>
<DispatchCell
:id="control.id + '-input'"
:schema="schema"
:uischema="uischema"
:path="path"
:enabled="enabled"
v-bind="control"
:id="controlWrapper.id"
:handle-change="handleChange"
/>
</control-wrapper>
</template>
Expand Down
29 changes: 29 additions & 0 deletions packages/vue-vanilla/src/util/composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@ import {
Resolve,
} from '@jsonforms/core';

/**
* Adds styles, isFocused, appliedOptions and onChange
*/
export const useVanillaCell = <I extends { cell: any; handleChange: any }>(
input: I,
adaptTarget: (target: any) => any = (v) => v.value
) => {
const appliedOptions = computed(() =>
merge(
{},
cloneDeep(input.cell.value.config),
cloneDeep(input.cell.value.uischema.options)
)
);

const isFocused = ref(false);
const onChange = (event: Event) => {
input.handleChange(input.cell.value.path, adaptTarget(event.target));
};

return {
...input,
styles: useStyles(input.cell.value.uischema),
isFocused,
appliedOptions,
onChange,
};
};

/**
* Adds styles, isFocused, appliedOptions and onChange
*/
Expand Down

0 comments on commit 0677ac8

Please sign in to comment.