Merge pull request #5569 from dispatchrabbi/issue-1027

Fixed #1027 - Knob: Allow a label function for greater flexibility
pull/5617/head
Tuğçe Küçükoğlu 2024-04-19 12:12:51 +03:00 committed by GitHub
commit e747e01e3d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 34 additions and 15 deletions

View File

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

View File

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

View File

@ -176,9 +176,9 @@ export interface KnobProps {
showValue?: boolean | undefined; showValue?: boolean | undefined;
/** /**
* Template string of the value. * 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. * Index of the element in tabbing order.
* @defaultValue 0 * @defaultValue 0

View File

@ -44,4 +44,16 @@ describe('Knob.vue', () => {
expect(wrapper.emitted()['update:modelValue'][0]).toEqual([30]); 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; return this.valueRadians > this.zeroRadians ? 0 : 1;
}, },
valueToDisplay() { 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", "name": "valueTemplate",
"optional": true, "optional": true,
"readonly": false, "readonly": false,
"type": "string", "type": "Function",
"default": "'{value}'", "default": ""
"description": "Template string of the value."
}, },
{ {
"name": "tabindex", "name": "tabindex",

View File

@ -1,9 +1,10 @@
<template> <template>
<DocSectionText v-bind="$attrs"> <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> </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="{value}%" />
<Knob v-model="value" :valueTemplate="(val) => val / 100" />
</div> </div>
<DocSectionCode :code="code" /> <DocSectionCode :code="code" />
</template> </template>
@ -16,11 +17,13 @@ export default {
code: { code: {
basic: ` basic: `
<Knob v-model="value" valueTemplate="{value}%" /> <Knob v-model="value" valueTemplate="{value}%" />
<Knob v-model="value" :valueTemplate="val => val / 100" />
`, `,
options: ` options: `
<template> <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="{value}%" />
<Knob v-model="value" :valueTemplate="val => val / 100" />
</div> </div>
</template> </template>
@ -36,8 +39,9 @@ export default {
`, `,
composition: ` composition: `
<template> <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="{value}%" />
<Knob v-model="value" :valueTemplate="val => val / 100" />
</div> </div>
</template> </template>