Fixed #1027 - Knob: Allow a label function for greater flexibility

pull/5569/head
Eli Morris-Heft 2024-04-11 17:52:30 -04:00
parent 8365a30f5b
commit 2bde6fc165
No known key found for this signature in database
7 changed files with 34 additions and 15 deletions

View File

@ -73,9 +73,9 @@ const KnobProps = [
},
{
name: 'valueTemplate',
type: 'string',
default: '{value}',
description: 'Template string of the value.'
type: 'function | string',
default: 'val => val',
description: 'Controls how the knob is labeled.'
},
{
name: 'tabindex',

View File

@ -55,8 +55,8 @@ export default {
default: true
},
valueTemplate: {
type: String,
default: '{value}'
type: [String, Function],
default: () => (val) => val
},
tabindex: {
type: Number,

View File

@ -176,9 +176,9 @@ export interface KnobProps {
showValue?: boolean | undefined;
/**
* Template string of the value.
* @defaultValue '{value}'
* @defaultValue 'val => val'
*/
valueTemplate?: string | undefined;
valueTemplate?: (val: number) => string | string | undefined;
/**
* Index of the element in tabbing order.
* @defaultValue 0

View File

@ -44,4 +44,16 @@ describe('Knob.vue', () => {
expect(wrapper.emitted()['update:modelValue'][0]).toEqual([30]);
});
it('should work with string valueTemplate', async () => {
await wrapper.setProps({ valueTemplate: '{value}%' });
expect(wrapper.find('.p-knob-text').text()).toBe('20%');
});
it('should work with function valueTemplate', async () => {
await wrapper.setProps({ valueTemplate: (val) => val * 10 });
expect(wrapper.find('.p-knob-text').text()).toBe('200');
});
});

View File

@ -215,7 +215,11 @@ export default {
return this.valueRadians > this.zeroRadians ? 0 : 1;
},
valueToDisplay() {
return this.valueTemplate.replace(/{value}/g, this.modelValue);
if (typeof this.valueTemplate === 'string') {
return this.valueTemplate.replace(/{value}/g, this.modelValue);
} else {
return this.valueTemplate(this.modelValue);
}
}
}
};

View File

@ -33423,9 +33423,8 @@
"name": "valueTemplate",
"optional": true,
"readonly": false,
"type": "string",
"default": "'{value}'",
"description": "Template string of the value."
"type": "Function",
"default": ""
},
{
"name": "tabindex",

View File

@ -1,9 +1,10 @@
<template>
<DocSectionText v-bind="$attrs">
<p>Label is a string template that can be customized with the <i>valueTemplate</i> property having <i>60</i> as the placeholder .</p>
<p>The label can be customized with the <i>valueTemplate</i> property using either a template string or a function.</p>
</DocSectionText>
<div class="card flex justify-content-center">
<div class="card flex justify-content-center gap-4">
<Knob v-model="value" valueTemplate="{value}%" />
<Knob v-model="value" :valueTemplate="(val) => val / 100" />
</div>
<DocSectionCode :code="code" />
</template>
@ -16,11 +17,13 @@ export default {
code: {
basic: `
<Knob v-model="value" valueTemplate="{value}%" />
<Knob v-model="value" :valueTemplate="val => val / 100" />
`,
options: `
<template>
<div class="card flex justify-content-center">
<div class="card flex justify-content-center gap-4">
<Knob v-model="value" valueTemplate="{value}%" />
<Knob v-model="value" :valueTemplate="val => val / 100" />
</div>
</template>
@ -36,8 +39,9 @@ export default {
`,
composition: `
<template>
<div class="card flex justify-content-center">
<div class="card flex justify-content-center gap-4">
<Knob v-model="value" valueTemplate="{value}%" />
<Knob v-model="value" :valueTemplate="val => val / 100" />
</div>
</template>