123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <template>
- <view class="u-textarea" :class="textareaClass" :style="[textareaStyle]">
- <textarea
- class="u-textarea__field"
- :value="innerValue"
- :style="{ height: $u.addUnit(height) }"
- :placeholder="placeholder"
- :placeholder-style="$u.addStyle(placeholderStyle, 'string')"
- :placeholder-class="placeholderClass"
- :disabled="disabled"
- :focus="focus"
- :autoHeight="autoHeight"
- :fixed="fixed"
- :cursorSpacing="cursorSpacing"
- :cursor="cursor"
- :showConfirmBar="showConfirmBar"
- :selectionStart="selectionStart"
- :selectionEnd="selectionEnd"
- :adjustPosition="adjustPosition"
- :disableDefaultPadding="disableDefaultPadding"
- :holdKeyboard="holdKeyboard"
- :maxlength="maxlength"
- :confirmType="confirmType"
- @focus="onFocus"
- @blur="onBlur"
- @linechange="onLinechange"
- @input="onInput"
- @confirm="onConfirm"
- @keyboardheightchange="onKeyboardheightchange"
- ></textarea>
- <text
- class="u-textarea__count"
- :style="{
- 'background-color': disabled ? 'transparent' : '#fff',
- }"
- v-if="count"
- >{{ innerValue.length }}/{{ maxlength }}</text
- >
- </view>
- </template>
- <script>
- import props from "./props.js";
- export default {
- name: "u-textarea",
- mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
- data() {
- return {
-
- innerValue: "",
-
- focused: false,
-
- firstChange: true,
-
- changeFromInner: false,
-
- innerFormatter: value => value
- }
- },
- watch: {
- value: {
- immediate: true,
- handler(newVal, oldVal) {
- this.innerValue = newVal;
-
-
- if (
- this.firstChange === false &&
- this.changeFromInner === false
- ) {
- this.valueChange();
- }
-
- this.firstChange = false;
-
- this.changeFromInner = false;
- },
- },
- },
- computed: {
-
- textareaClass() {
- let classes = [],
- { border, disabled, shape } = this;
- border === "surround" &&
- (classes = classes.concat(["u-border", "u-textarea--radius"]));
- border === "bottom" &&
- (classes = classes.concat([
- "u-border-bottom",
- "u-textarea--no-radius",
- ]));
- disabled && classes.push("u-textarea--disabled");
- return classes.join(" ");
- },
-
- textareaStyle() {
- const style = {};
-
-
- if (uni.$u.os() === "android") {
- style.paddingTop = "6px";
- style.paddingLeft = "9px";
- style.paddingBottom = "3px";
- style.paddingRight = "6px";
- }
-
- return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle));
- },
- },
- methods: {
-
- setFormatter(e) {
- this.innerFormatter = e
- },
- onFocus(e) {
- this.$emit("focus", e);
- },
- onBlur(e) {
- this.$emit("blur", e);
-
- uni.$u.formValidate(this, "blur");
- },
- onLinechange(e) {
- this.$emit("linechange", e);
- },
- onInput(e) {
- let { value = "" } = e.detail || {};
-
- const formatter = this.formatter || this.innerFormatter
- const formatValue = formatter(value)
-
- this.innerValue = value
- this.$nextTick(() => {
- this.innerValue = formatValue;
- this.valueChange();
- })
- },
-
- valueChange() {
- const value = this.innerValue;
- this.$nextTick(() => {
- this.$emit("input", value);
-
- this.changeFromInner = true;
- this.$emit("change", value);
-
- uni.$u.formValidate(this, "change");
- });
- },
- onConfirm(e) {
- this.$emit("confirm", e);
- },
- onKeyboardheightchange(e) {
- this.$emit("keyboardheightchange", e);
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- @import "../../libs/css/components.scss";
- .u-textarea {
- border-radius: 4px;
- background-color: #fff;
- position: relative;
- @include flex;
- flex: 1;
- padding: 9px;
- &--radius {
- border-radius: 4px;
- }
- &--no-radius {
- border-radius: 0;
- }
- &--disabled {
- background-color: #f5f7fa;
- }
- &__field {
- flex: 1;
- font-size: 15px;
- color: $u-content-color;
- width: 100%;
- }
- &__count {
- position: absolute;
- right: 5px;
- bottom: 2px;
- font-size: 12px;
- color: $u-tips-color;
- background-color: #ffffff;
- padding: 1px 4px;
- }
- }
- </style>
|