130 lines
3.7 KiB
JavaScript
130 lines
3.7 KiB
JavaScript
/**
|
||
* uView u-input 组件补丁
|
||
* 在 isSelectLike(readonly/disabled)模式下用 view 代替原生 input 显示文字
|
||
* 使下拉选择框超长文字可以自动换行完整显示
|
||
*
|
||
* 通过 package.json 的 postinstall 自动执行
|
||
*/
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
const filePath = path.join('node_modules', 'uview-plus', 'components', 'u-input', 'u-input.vue');
|
||
|
||
if (!fs.existsSync(filePath)) {
|
||
console.log('[patch-u-input] u-input.vue not found, skipping');
|
||
process.exit(0);
|
||
}
|
||
|
||
let content = fs.readFileSync(filePath, 'utf8');
|
||
let patched = false;
|
||
|
||
// === 补丁1:模板中添加 v-if/v-else ===
|
||
if (!content.includes('u-input__content__field-wrapper__field--selectlike')) {
|
||
const oldTemplate = ` <input
|
||
class="u-input__content__field-wrapper__field"
|
||
:style="[inputStyle]"
|
||
:type="type"`;
|
||
|
||
const newTemplate = ` <!-- 选择型(readonly/disabled)用 view 显示文字,支持自动换行 -->
|
||
<view
|
||
v-if="isSelectLike"
|
||
class="u-input__content__field-wrapper__field u-input__content__field-wrapper__field--selectlike"
|
||
:style="[inputStyle]"
|
||
>
|
||
<text v-if="innerValue" class="u-input__content__field-wrapper__field__text">{{ innerValue }}</text>
|
||
<text v-else class="u-input__content__field-wrapper__field__placeholder">{{ placeholder }}</text>
|
||
</view>
|
||
<!-- 普通输入模式保持原生 input -->
|
||
<input
|
||
v-else
|
||
class="u-input__content__field-wrapper__field"
|
||
:style="[inputStyle]"
|
||
:type="type"`;
|
||
|
||
if (content.includes(oldTemplate)) {
|
||
content = content.replace(oldTemplate, newTemplate);
|
||
patched = true;
|
||
}
|
||
}
|
||
|
||
// === 补丁2:CSS - 修改 .u-input--selectlike 块,添加高度自适应 ===
|
||
if (patched && content.includes('&--selectlike {\n .u-input__content,')) {
|
||
const oldSelectlike = ` &--selectlike {
|
||
.u-input__content,
|
||
.u-input__content__field-wrapper,
|
||
.u-input__content__field-wrapper__field {
|
||
pointer-events: none;
|
||
}
|
||
}`;
|
||
|
||
const newSelectlike = ` &--selectlike {
|
||
height: auto !important;
|
||
min-height: 68rpx;
|
||
|
||
.u-input__content,
|
||
.u-input__content__field-wrapper,
|
||
.u-input__content__field-wrapper__field {
|
||
pointer-events: none;
|
||
}
|
||
}`;
|
||
|
||
if (content.includes(oldSelectlike)) {
|
||
content = content.replace(oldSelectlike, newSelectlike);
|
||
}
|
||
}
|
||
|
||
// === 补丁3:CSS - 在 &__field 中添加 selectlike 子样式 ===
|
||
if (patched && !content.includes('&__text {')) {
|
||
const oldField = ` &__field {
|
||
line-height: 26px;
|
||
text-align: left;
|
||
color: $u-main-color;
|
||
height: 24px;
|
||
font-size: 15px;
|
||
flex: 1;
|
||
}
|
||
}`;
|
||
|
||
const newField = ` &__field {
|
||
line-height: 26px;
|
||
text-align: left;
|
||
color: $u-main-color;
|
||
height: 24px;
|
||
font-size: 15px;
|
||
flex: 1;
|
||
|
||
&--selectlike {
|
||
min-height: 24px;
|
||
height: auto !important;
|
||
line-height: 26px;
|
||
word-break: break-all;
|
||
white-space: normal;
|
||
width: 100%;
|
||
}
|
||
|
||
&__text {
|
||
color: $u-main-color;
|
||
font-size: 15px;
|
||
line-height: 26px;
|
||
}
|
||
|
||
&__placeholder {
|
||
color: $u-light-color;
|
||
font-size: 15px;
|
||
line-height: 26px;
|
||
}
|
||
}
|
||
}`;
|
||
|
||
if (content.includes(oldField)) {
|
||
content = content.replace(oldField, newField);
|
||
}
|
||
}
|
||
|
||
if (patched) {
|
||
fs.writeFileSync(filePath, content, 'utf8');
|
||
console.log('[patch-u-input] Patched successfully');
|
||
} else {
|
||
console.log('[patch-u-input] Already patched or pattern not found, skipping');
|
||
}
|