mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(Form): add disabled and readonly prop (#7830)
* feat(Form): add disabled and readonly prop * chore(Field): 使用 getProp 方法 * fix(Field): 将 computed 改为直接取值
This commit is contained in:
parent
568ba3d815
commit
149cde1ce1
@ -40,8 +40,14 @@ export default createComponent({
|
|||||||
rows: [Number, String],
|
rows: [Number, String],
|
||||||
name: String,
|
name: String,
|
||||||
rules: Array,
|
rules: Array,
|
||||||
disabled: Boolean,
|
disabled: {
|
||||||
readonly: Boolean,
|
type: Boolean,
|
||||||
|
default: null,
|
||||||
|
},
|
||||||
|
readonly: {
|
||||||
|
type: Boolean,
|
||||||
|
default: null,
|
||||||
|
},
|
||||||
autosize: [Boolean, Object],
|
autosize: [Boolean, Object],
|
||||||
leftIcon: String,
|
leftIcon: String,
|
||||||
rightIcon: String,
|
rightIcon: String,
|
||||||
@ -103,8 +109,21 @@ export default createComponent({
|
|||||||
const inputRef = ref();
|
const inputRef = ref();
|
||||||
const childFieldValue = ref();
|
const childFieldValue = ref();
|
||||||
|
|
||||||
|
const { parent: form } = useParent(FORM_KEY);
|
||||||
|
|
||||||
|
const getProp = (key) => {
|
||||||
|
if (isDef(props[key])) {
|
||||||
|
return props[key];
|
||||||
|
}
|
||||||
|
if (form && isDef(form.props[key])) {
|
||||||
|
return form.props[key];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const showClear = computed(() => {
|
const showClear = computed(() => {
|
||||||
if (props.clearable && !props.readonly) {
|
const readonly = getProp('readonly');
|
||||||
|
|
||||||
|
if (props.clearable && !readonly) {
|
||||||
const hasValue = isDef(props.modelValue) && props.modelValue !== '';
|
const hasValue = isDef(props.modelValue) && props.modelValue !== '';
|
||||||
const trigger =
|
const trigger =
|
||||||
props.clearTrigger === 'always' ||
|
props.clearTrigger === 'always' ||
|
||||||
@ -199,8 +218,6 @@ export default createComponent({
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const { parent: form } = useParent(FORM_KEY);
|
|
||||||
|
|
||||||
const validateWithTrigger = (trigger) => {
|
const validateWithTrigger = (trigger) => {
|
||||||
if (form && props.rules) {
|
if (form && props.rules) {
|
||||||
const defaultTrigger = form.props.validateTrigger === trigger;
|
const defaultTrigger = form.props.validateTrigger === trigger;
|
||||||
@ -272,7 +289,8 @@ export default createComponent({
|
|||||||
emit('focus', event);
|
emit('focus', event);
|
||||||
|
|
||||||
// readonly not work in lagacy mobile safari
|
// readonly not work in lagacy mobile safari
|
||||||
if (props.readonly) {
|
const readonly = getProp('readonly');
|
||||||
|
if (readonly) {
|
||||||
blur();
|
blur();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -312,15 +330,6 @@ export default createComponent({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const getProp = (key) => {
|
|
||||||
if (isDef(props[key])) {
|
|
||||||
return props[key];
|
|
||||||
}
|
|
||||||
if (form && isDef(form.props[key])) {
|
|
||||||
return form.props[key];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const labelStyle = computed(() => {
|
const labelStyle = computed(() => {
|
||||||
const labelWidth = getProp('labelWidth');
|
const labelWidth = getProp('labelWidth');
|
||||||
if (labelWidth) {
|
if (labelWidth) {
|
||||||
@ -384,6 +393,8 @@ export default createComponent({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const renderInput = () => {
|
const renderInput = () => {
|
||||||
|
const disabled = getProp('disabled');
|
||||||
|
const readonly = getProp('readonly');
|
||||||
const inputAlign = getProp('inputAlign');
|
const inputAlign = getProp('inputAlign');
|
||||||
|
|
||||||
if (slots.input) {
|
if (slots.input) {
|
||||||
@ -403,8 +414,8 @@ export default createComponent({
|
|||||||
rows: props.rows,
|
rows: props.rows,
|
||||||
class: bem('control', inputAlign),
|
class: bem('control', inputAlign),
|
||||||
value: props.modelValue,
|
value: props.modelValue,
|
||||||
disabled: props.disabled,
|
disabled,
|
||||||
readonly: props.readonly,
|
readonly,
|
||||||
placeholder: props.placeholder,
|
placeholder: props.placeholder,
|
||||||
onBlur,
|
onBlur,
|
||||||
onFocus,
|
onFocus,
|
||||||
@ -539,6 +550,7 @@ export default createComponent({
|
|||||||
});
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
const disabled = getProp('disabled');
|
||||||
const labelAlign = getProp('labelAlign');
|
const labelAlign = getProp('labelAlign');
|
||||||
const Label = renderLabel();
|
const Label = renderLabel();
|
||||||
const LeftIcon = renderLeftIcon();
|
const LeftIcon = renderLeftIcon();
|
||||||
@ -554,7 +566,7 @@ export default createComponent({
|
|||||||
icon={props.leftIcon}
|
icon={props.leftIcon}
|
||||||
class={bem({
|
class={bem({
|
||||||
error: showError.value,
|
error: showError.value,
|
||||||
disabled: props.disabled,
|
disabled,
|
||||||
[`label-${labelAlign}`]: labelAlign,
|
[`label-${labelAlign}`]: labelAlign,
|
||||||
'min-height': props.type === 'textarea' && !props.autosize,
|
'min-height': props.type === 'textarea' && !props.autosize,
|
||||||
})}
|
})}
|
||||||
|
@ -471,6 +471,8 @@ export default {
|
|||||||
|
|
||||||
| Attribute | Description | Type | Default |
|
| Attribute | Description | Type | Default |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
|
| disabled | Whether to disable field | _boolean_ | `false` |
|
||||||
|
| readonly | Whether to be readonly | _boolean_ | `false` |
|
||||||
| label-width | Field label width | _number \| string_ | `6.2em` |
|
| label-width | Field label width | _number \| string_ | `6.2em` |
|
||||||
| label-align | Field label align, can be set to `center` `right` | _string_ | `left` |
|
| label-align | Field label align, can be set to `center` `right` | _string_ | `left` |
|
||||||
| input-align | Field input align, can be set to `center` `right` | _string_ | `left` |
|
| input-align | Field input align, can be set to `center` `right` | _string_ | `left` |
|
||||||
|
@ -504,6 +504,8 @@ export default {
|
|||||||
|
|
||||||
| 参数 | 说明 | 类型 | 默认值 |
|
| 参数 | 说明 | 类型 | 默认值 |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
|
| disabled | 是否禁用输入框 | _boolean_ | `false` |
|
||||||
|
| readonly | 是否只读 | _boolean_ | `false` |
|
||||||
| label-width | 表单项 label 宽度,默认单位为`px` | _number \| string_ | `6.2em` |
|
| label-width | 表单项 label 宽度,默认单位为`px` | _number \| string_ | `6.2em` |
|
||||||
| label-align | 表单项 label 对齐方式,可选值为 `center` `right` | _string_ | `left` |
|
| label-align | 表单项 label 对齐方式,可选值为 `center` `right` | _string_ | `left` |
|
||||||
| input-align | 输入框对齐方式,可选值为 `center` `right` | _string_ | `left` |
|
| input-align | 输入框对齐方式,可选值为 `center` `right` | _string_ | `left` |
|
||||||
|
@ -7,6 +7,8 @@ const [createComponent, bem] = createNamespace('form');
|
|||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
props: {
|
props: {
|
||||||
|
disabled: Boolean,
|
||||||
|
readonly: Boolean,
|
||||||
colon: Boolean,
|
colon: Boolean,
|
||||||
labelWidth: [Number, String],
|
labelWidth: [Number, String],
|
||||||
labelAlign: String,
|
labelAlign: String,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user