mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-29 21:06:33 +08:00
feat(Picker): add allow-html prop (#4278)
This commit is contained in:
parent
93ef02c409
commit
667c4f692c
@ -32,6 +32,7 @@ export default createComponent({
|
|||||||
|
|
||||||
props: {
|
props: {
|
||||||
valueKey: String,
|
valueKey: String,
|
||||||
|
allowHtml: Boolean,
|
||||||
className: String,
|
className: String,
|
||||||
itemHeight: Number,
|
itemHeight: Number,
|
||||||
defaultIndex: Number,
|
defaultIndex: Number,
|
||||||
@ -226,20 +227,48 @@ export default createComponent({
|
|||||||
this.transitionEndTrigger();
|
this.transitionEndTrigger();
|
||||||
this.transitionEndTrigger = null;
|
this.transitionEndTrigger = null;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
genOptions() {
|
||||||
|
const optionStyle = {
|
||||||
|
height: `${this.itemHeight}px`
|
||||||
|
};
|
||||||
|
|
||||||
|
return this.options.map((option, index) => {
|
||||||
|
const text = this.getOptionText(option);
|
||||||
|
const data = {
|
||||||
|
style: optionStyle,
|
||||||
|
class: [
|
||||||
|
'van-ellipsis',
|
||||||
|
bem('item', {
|
||||||
|
disabled: isOptionDisabled(option),
|
||||||
|
selected: index === this.currentIndex
|
||||||
|
})
|
||||||
|
],
|
||||||
|
on: {
|
||||||
|
click: () => {
|
||||||
|
this.onClickItem(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.allowHtml) {
|
||||||
|
data.domProps = {
|
||||||
|
innerHTML: text
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return <li {...data}>{this.allowHtml ? '' : text}</li>;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { itemHeight } = this;
|
|
||||||
const wrapperStyle = {
|
const wrapperStyle = {
|
||||||
transform: `translate3d(0, ${this.offset + this.baseOffset}px, 0)`,
|
transform: `translate3d(0, ${this.offset + this.baseOffset}px, 0)`,
|
||||||
transitionDuration: `${this.duration}ms`,
|
transitionDuration: `${this.duration}ms`,
|
||||||
transitionProperty: this.duration ? 'all' : 'none',
|
transitionProperty: this.duration ? 'all' : 'none',
|
||||||
lineHeight: `${itemHeight}px`
|
lineHeight: `${this.itemHeight}px`
|
||||||
};
|
|
||||||
|
|
||||||
const optionStyle = {
|
|
||||||
height: `${itemHeight}px`
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -256,22 +285,7 @@ export default createComponent({
|
|||||||
class={bem('wrapper')}
|
class={bem('wrapper')}
|
||||||
onTransitionend={this.onTransitionEnd}
|
onTransitionend={this.onTransitionEnd}
|
||||||
>
|
>
|
||||||
{this.options.map((option, index) => (
|
{this.genOptions()}
|
||||||
<li
|
|
||||||
style={optionStyle}
|
|
||||||
class={[
|
|
||||||
'van-ellipsis',
|
|
||||||
bem('item', {
|
|
||||||
disabled: isOptionDisabled(option),
|
|
||||||
selected: index === this.currentIndex
|
|
||||||
})
|
|
||||||
]}
|
|
||||||
domPropsInnerHTML={this.getOptionText(option)}
|
|
||||||
onClick={() => {
|
|
||||||
this.onClickItem(index);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
@ -196,6 +196,7 @@ When Picker columns data is acquired asynchronously, use `loading` prop to show
|
|||||||
| confirm-button-text | Text of confirm button | *string* | `Confirm` | - |
|
| confirm-button-text | Text of confirm button | *string* | `Confirm` | - |
|
||||||
| cancel-button-text | Text of cancel button | *string* | `Cancel` | - |
|
| cancel-button-text | Text of cancel button | *string* | `Cancel` | - |
|
||||||
| visible-item-count | Count of visible columns | *number* | `5` | - |
|
| visible-item-count | Count of visible columns | *number* | `5` | - |
|
||||||
|
| allow-html | Whether to allow HTML in option text | *boolean* | `true` | 2.1.8 |
|
||||||
| default-index | Default value index of single column picker | *number* | `0` | - |
|
| default-index | Default value index of single column picker | *number* | `0` | - |
|
||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
@ -204,6 +204,7 @@ export default {
|
|||||||
| confirm-button-text | 确认按钮文字 | *string* | `确认` | - |
|
| confirm-button-text | 确认按钮文字 | *string* | `确认` | - |
|
||||||
| cancel-button-text | 取消按钮文字 | *string* | `取消` | - |
|
| cancel-button-text | 取消按钮文字 | *string* | `取消` | - |
|
||||||
| visible-item-count | 可见的选项个数 | *number* | `5` | - |
|
| visible-item-count | 可见的选项个数 | *number* | `5` | - |
|
||||||
|
| allow-html | 是否允许选项内容中渲染 HTML | *boolean* | `true` | 2.1.8 |
|
||||||
| default-index | 单列选择器的默认选中项索引,<br>多列选择器请参考下方的 Columns 配置 | *number* | `0` | - |
|
| default-index | 单列选择器的默认选中项索引,<br>多列选择器请参考下方的 Columns 配置 | *number* | `0` | - |
|
||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
@ -187,6 +187,7 @@ export default createComponent({
|
|||||||
{columns.map((item, index) => (
|
{columns.map((item, index) => (
|
||||||
<PickerColumn
|
<PickerColumn
|
||||||
valueKey={this.valueKey}
|
valueKey={this.valueKey}
|
||||||
|
allowHtml={this.allowHtml}
|
||||||
className={item.className}
|
className={item.className}
|
||||||
itemHeight={this.itemHeight}
|
itemHeight={this.itemHeight}
|
||||||
defaultIndex={item.defaultIndex || this.defaultIndex}
|
defaultIndex={item.defaultIndex || this.defaultIndex}
|
||||||
|
@ -14,6 +14,10 @@ export const pickerProps = {
|
|||||||
showToolbar: Boolean,
|
showToolbar: Boolean,
|
||||||
cancelButtonText: String,
|
cancelButtonText: String,
|
||||||
confirmButtonText: String,
|
confirmButtonText: String,
|
||||||
|
allowHtml: {
|
||||||
|
type: Boolean,
|
||||||
|
default: true
|
||||||
|
},
|
||||||
visibleItemCount: {
|
visibleItemCount: {
|
||||||
type: Number,
|
type: Number,
|
||||||
default: 5
|
default: 5
|
||||||
|
@ -28,6 +28,22 @@ exports[`column watch default index 2`] = `
|
|||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`not allow html 1`] = `
|
||||||
|
<div class="van-picker">
|
||||||
|
<!---->
|
||||||
|
<div class="van-picker__columns" style="height: 220px;">
|
||||||
|
<div class="van-picker-column">
|
||||||
|
<ul class="van-picker-column__wrapper" style="transform: translate3d(0, 88px, 0); transition-duration: 0ms; transition-property: none; line-height: 44px;">
|
||||||
|
<li class="van-ellipsis van-picker-column__item van-picker-column__item--selected" style="height: 44px;"><div>option</div></li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<div class="van-picker__mask" style="background-size: 100% 88px;"></div>
|
||||||
|
<div class="van-hairline-unset--top-bottom van-picker__frame" style="height: 44px;"></div>
|
||||||
|
</div>
|
||||||
|
<!---->
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`render title slot 1`] = `
|
exports[`render title slot 1`] = `
|
||||||
<div class="van-picker">
|
<div class="van-picker">
|
||||||
<div class="van-hairline--top-bottom van-picker__toolbar">
|
<div class="van-hairline--top-bottom van-picker__toolbar">
|
||||||
|
@ -195,3 +195,14 @@ test('toolbar-position prop', () => {
|
|||||||
|
|
||||||
expect(wrapper).toMatchSnapshot();
|
expect(wrapper).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('not allow html', () => {
|
||||||
|
const wrapper = mount(Picker, {
|
||||||
|
propsData: {
|
||||||
|
allowHtml: false,
|
||||||
|
columns: ['<div>option</div>']
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user