Skip to content

Commit 667c4f6

Browse files
authored
feat(Picker): add allow-html prop (youzan#4278)
1 parent 93ef02c commit 667c4f6

7 files changed

Lines changed: 70 additions & 22 deletions

File tree

src/picker/PickerColumn.js

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default createComponent({
3232

3333
props: {
3434
valueKey: String,
35+
allowHtml: Boolean,
3536
className: String,
3637
itemHeight: Number,
3738
defaultIndex: Number,
@@ -226,20 +227,48 @@ export default createComponent({
226227
this.transitionEndTrigger();
227228
this.transitionEndTrigger = null;
228229
}
230+
},
231+
232+
genOptions() {
233+
const optionStyle = {
234+
height: `${this.itemHeight}px`
235+
};
236+
237+
return this.options.map((option, index) => {
238+
const text = this.getOptionText(option);
239+
const data = {
240+
style: optionStyle,
241+
class: [
242+
'van-ellipsis',
243+
bem('item', {
244+
disabled: isOptionDisabled(option),
245+
selected: index === this.currentIndex
246+
})
247+
],
248+
on: {
249+
click: () => {
250+
this.onClickItem(index);
251+
}
252+
}
253+
};
254+
255+
if (this.allowHtml) {
256+
data.domProps = {
257+
innerHTML: text
258+
};
259+
}
260+
261+
return <li {...data}>{this.allowHtml ? '' : text}</li>;
262+
});
229263
}
230264
},
231265

232266
render() {
233-
const { itemHeight } = this;
234267
const wrapperStyle = {
235268
transform: `translate3d(0, ${this.offset + this.baseOffset}px, 0)`,
236269
transitionDuration: `${this.duration}ms`,
237270
transitionProperty: this.duration ? 'all' : 'none',
238-
lineHeight: `${itemHeight}px`
239-
};
240-
241-
const optionStyle = {
242-
height: `${itemHeight}px`
271+
lineHeight: `${this.itemHeight}px`
243272
};
244273

245274
return (
@@ -256,22 +285,7 @@ export default createComponent({
256285
class={bem('wrapper')}
257286
onTransitionend={this.onTransitionEnd}
258287
>
259-
{this.options.map((option, index) => (
260-
<li
261-
style={optionStyle}
262-
class={[
263-
'van-ellipsis',
264-
bem('item', {
265-
disabled: isOptionDisabled(option),
266-
selected: index === this.currentIndex
267-
})
268-
]}
269-
domPropsInnerHTML={this.getOptionText(option)}
270-
onClick={() => {
271-
this.onClickItem(index);
272-
}}
273-
/>
274-
))}
288+
{this.genOptions()}
275289
</ul>
276290
</div>
277291
);

src/picker/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ When Picker columns data is acquired asynchronously, use `loading` prop to show
196196
| confirm-button-text | Text of confirm button | *string* | `Confirm` | - |
197197
| cancel-button-text | Text of cancel button | *string* | `Cancel` | - |
198198
| visible-item-count | Count of visible columns | *number* | `5` | - |
199+
| allow-html | Whether to allow HTML in option text | *boolean* | `true` | 2.1.8 |
199200
| default-index | Default value index of single column picker | *number* | `0` | - |
200201

201202
### Events

src/picker/README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ export default {
204204
| confirm-button-text | 确认按钮文字 | *string* | `确认` | - |
205205
| cancel-button-text | 取消按钮文字 | *string* | `取消` | - |
206206
| visible-item-count | 可见的选项个数 | *number* | `5` | - |
207+
| allow-html | 是否允许选项内容中渲染 HTML | *boolean* | `true` | 2.1.8 |
207208
| default-index | 单列选择器的默认选中项索引,<br>多列选择器请参考下方的 Columns 配置 | *number* | `0` | - |
208209

209210
### Events

src/picker/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ export default createComponent({
187187
{columns.map((item, index) => (
188188
<PickerColumn
189189
valueKey={this.valueKey}
190+
allowHtml={this.allowHtml}
190191
className={item.className}
191192
itemHeight={this.itemHeight}
192193
defaultIndex={item.defaultIndex || this.defaultIndex}

src/picker/shared.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export const pickerProps = {
1414
showToolbar: Boolean,
1515
cancelButtonText: String,
1616
confirmButtonText: String,
17+
allowHtml: {
18+
type: Boolean,
19+
default: true
20+
},
1721
visibleItemCount: {
1822
type: Number,
1923
default: 5

src/picker/test/__snapshots__/index.spec.js.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ exports[`column watch default index 2`] = `
2828
</div>
2929
`;
3030

31+
exports[`not allow html 1`] = `
32+
<div class="van-picker">
33+
<!---->
34+
<div class="van-picker__columns" style="height: 220px;">
35+
<div class="van-picker-column">
36+
<ul class="van-picker-column__wrapper" style="transform: translate3d(0, 88px, 0); transition-duration: 0ms; transition-property: none; line-height: 44px;">
37+
<li class="van-ellipsis van-picker-column__item van-picker-column__item--selected" style="height: 44px;">&lt;div&gt;option&lt;/div&gt;</li>
38+
</ul>
39+
</div>
40+
<div class="van-picker__mask" style="background-size: 100% 88px;"></div>
41+
<div class="van-hairline-unset--top-bottom van-picker__frame" style="height: 44px;"></div>
42+
</div>
43+
<!---->
44+
</div>
45+
`;
46+
3147
exports[`render title slot 1`] = `
3248
<div class="van-picker">
3349
<div class="van-hairline--top-bottom van-picker__toolbar">

src/picker/test/index.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,14 @@ test('toolbar-position prop', () => {
195195

196196
expect(wrapper).toMatchSnapshot();
197197
});
198+
199+
test('not allow html', () => {
200+
const wrapper = mount(Picker, {
201+
propsData: {
202+
allowHtml: false,
203+
columns: ['<div>option</div>']
204+
}
205+
});
206+
207+
expect(wrapper).toMatchSnapshot();
208+
});

0 commit comments

Comments
 (0)