Merge pull request #5569 from dispatchrabbi/issue-1027
Fixed #1027 - Knob: Allow a label function for greater flexibilitypull/5617/head
commit
e747e01e3d
|
@ -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',
|
||||
|
|
|
@ -55,8 +55,8 @@ export default {
|
|||
default: true
|
||||
},
|
||||
valueTemplate: {
|
||||
type: String,
|
||||
default: '{value}'
|
||||
type: [String, Function],
|
||||
default: () => (val) => val
|
||||
},
|
||||
tabindex: {
|
||||
type: Number,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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>
|
||||
|
||||
|
|
Loading…
Reference in New Issue