Skip to content

Commit

Permalink
feat: 新增Slider字段控件
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Heng committed Aug 12, 2023
1 parent 89aa968 commit 1ad6db2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
37 changes: 37 additions & 0 deletions main/src/form/fields/Slider.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script setup lang="ts">
import { computed, ref, toRefs, watch } from 'vue';
import { FormValue, SliderConfig } from '../types';
const props = withDefaults(
defineProps<{
config: SliderConfig;
prop: string;
model?: FormValue;
}>(),
{
model: () => ({}),
}
);
const emits = defineEmits<{
(event: 'change', prop: string, value: number): void;
}>();
const { model, prop, config } = toRefs(props);
const value = ref<number>(prop?.value && model?.value?.[prop?.value]);
const min = computed<number>(() => config.value.min || 0);
const max = computed<number>(() => config.value.max || 1);
const step = computed<number>(() => config.value.step || 0.01);
watch(
() => value.value,
(v) => emits('change', prop.value, v)
);
</script>

<template>
<div class="w-full h-full">
<input v-model="value" type="range" class="block w-full h-full cursor-pointer" :min="min" :max="max" :step="step" />
</div>
</template>
2 changes: 2 additions & 0 deletions main/src/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Plugin } from 'vue';

import ColorPicker from './fields/ColorPicker.vue';
import RadioGroup from './fields/RadioGroup.vue';
import Slider from './fields/Slider.vue';

export { default as Form } from './Form.vue';
export * from './types';
Expand All @@ -10,6 +11,7 @@ export const formPlugin: Plugin = {
install(app) {
app.component('RadioGroup', RadioGroup);
app.component('ColorPicker', ColorPicker);
app.component('Slider', Slider);
},
};

Expand Down
7 changes: 7 additions & 0 deletions main/src/form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export interface ColorPickerConfig extends FormItem {
type: 'ColorPicker';
}

export interface SliderConfig extends FormItem {
type: 'Slider';
max?: number;
min?: number;
step?: number;
}

export type ChildConfig = FormItem | RadioGroupConfig | ColorPickerConfig;

export type FormConfig = ChildConfig[];
9 changes: 6 additions & 3 deletions main/src/view/canvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ const formConfig: FormConfig = [
label: '颜色',
type: 'ColorPicker',
},
{
name: 'range',
label: '滑块',
type: 'Slider',
},
];
const formValue = {
node: 1,
};
const formValue = {};
const handleFormChange = (value: any) => {
console.log('新的form', value);
Expand Down

0 comments on commit 1ad6db2

Please sign in to comment.