Skip to content

Commit 0f75fa3

Browse files
committed
fix: unify readOnly prop handling
1 parent f55263b commit 0f75fa3

File tree

6 files changed

+18
-11
lines changed

6 files changed

+18
-11
lines changed

packages/components/input/Input.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React, { useState, useRef, useImperativeHandle, useEffect } from 'react';
2-
import classNames from 'classnames';
32
import {
43
BrowseIcon as TdBrowseIcon,
54
BrowseOffIcon as TdBrowseOffIcon,
65
CloseCircleFilledIcon as TdCloseCircleFilledIcon,
76
} from 'tdesign-icons-react';
7+
import classNames from 'classnames';
88
import { isFunction } from 'lodash-es';
99
import useLayoutEffect from '../hooks/useLayoutEffect';
1010
import forwardRefWithStatics from '../_util/forwardRefWithStatics';
@@ -82,6 +82,8 @@ const Input = forwardRefWithStatics(
8282
allowInput,
8383
allowInputOverMax,
8484
name,
85+
readOnly,
86+
readonly,
8587
format,
8688
onClick,
8789
onClear,
@@ -101,7 +103,7 @@ const Input = forwardRefWithStatics(
101103
onChange: onChangeFromProps,
102104
...restProps
103105
} = props;
104-
const readOnly = props.readOnly || props.readonly;
106+
const readOnlyProp = readOnly || readonly;
105107

106108
const [value, onChange] = useControlled(props, 'value', onChangeFromProps);
107109
const { limitNumber, getValueByLimitNumber, tStatus } = useLengthLimit({
@@ -126,7 +128,7 @@ const Input = forwardRefWithStatics(
126128
const [composingValue, setComposingValue] = useState<string>('');
127129

128130
// 组件内部 input 原生控件是否处于 readonly 状态,当整个组件 readonly 时,或者处于不可输入时
129-
const isInnerInputReadonly = readOnly || !allowInput;
131+
const isInnerInputReadonly = readOnlyProp || !allowInput;
130132
const isValueEnabled = value && !disabled;
131133
const alwaysShowClearIcon = inputConfig?.clearTrigger === 'always';
132134
const isShowClearIcon =
@@ -248,7 +250,7 @@ const Input = forwardRefWithStatics(
248250
const renderInputNode = (
249251
<div
250252
className={classNames(inputClass, `${classPrefix}-input`, {
251-
[`${classPrefix}-is-readonly`]: readOnly,
253+
[`${classPrefix}-is-readonly`]: readOnlyProp,
252254
[`${classPrefix}-is-disabled`]: disabled,
253255
[`${classPrefix}-is-focused`]: isFocused,
254256
[`${classPrefix}-size-s`]: size === 'small',

packages/components/range-input/RangeInput.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ const RangeInput = React.forwardRef<RangeInputInstanceFunctions, RangeInputProps
5757
suffixIcon,
5858
clearable,
5959
showClearIconOnEmpty,
60+
readOnly,
61+
readonly,
6062
onClick,
6163
onEnter,
6264
onClear,
@@ -67,7 +69,7 @@ const RangeInput = React.forwardRef<RangeInputInstanceFunctions, RangeInputProps
6769
onChange: onChangeFromProps,
6870
...restProps
6971
} = props;
70-
const readOnly = props.readOnly || props.readonly;
72+
const readOnlyProp = readOnly || readonly;
7173

7274
const name = `${classPrefix}-range-input`;
7375

@@ -189,7 +191,7 @@ const RangeInput = React.forwardRef<RangeInputInstanceFunctions, RangeInputProps
189191
})}
190192
placeholder={firstPlaceholder}
191193
disabled={disabled}
192-
readOnly={readOnly}
194+
readOnly={readOnlyProp}
193195
format={firstFormat}
194196
value={firstValue}
195197
onClick={({ e }) => onClick?.({ e, position: 'first' })}
@@ -211,7 +213,7 @@ const RangeInput = React.forwardRef<RangeInputInstanceFunctions, RangeInputProps
211213
})}
212214
placeholder={secondPlaceholder}
213215
disabled={disabled}
214-
readOnly={readOnly}
216+
readOnly={readOnlyProp}
215217
format={secondFormat}
216218
value={secondValue}
217219
onClick={({ e }) => onClick?.({ e, position: 'second' })}

packages/components/select-input/useMultiple.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export default function useMultiple(props: SelectInputProps) {
118118
{...props.tagInputProps}
119119
inputProps={{
120120
...props.inputProps,
121+
readOnly: !props.allowInput || readOnly,
121122
readonly: !props.allowInput || readOnly,
122123
inputClass: classNames(props.tagInputProps?.className, {
123124
[`${classPrefix}-input--focused`]: p.popupVisible,

packages/components/tag-input/TagInput.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ const TagInput = forwardRef<InputRef, TagInputProps>((originalProps, ref) => {
162162
} as React.CSSProperties)
163163
: {};
164164

165+
console.log('>>', inputProps?.readOnly, inputProps?.readonly);
165166
return (
166167
<TInput
167168
ref={tagInputRef as React.RefObject<InputRef>}
@@ -173,7 +174,7 @@ const TagInput = forwardRef<InputRef, TagInputProps>((originalProps, ref) => {
173174
onWheel={onWheel}
174175
size={size}
175176
borderless={borderless}
176-
readonly={readOnly}
177+
readOnly={readOnly}
177178
disabled={disabled}
178179
label={renderLabel({ displayNode, label })}
179180
className={classnames(classes)}
@@ -187,7 +188,8 @@ const TagInput = forwardRef<InputRef, TagInputProps>((originalProps, ref) => {
187188
suffix={suffix}
188189
prefixIcon={prefixIcon}
189190
suffixIcon={suffixIconNode}
190-
showInput={!inputProps?.readOnly || !inputProps?.readonly || !tagValue || !tagValue?.length}
191+
// showInput={!inputProps?.readOnly || !inputProps?.readonly || !tagValue || !tagValue?.length}
192+
showInput={!inputProps?.readonly || !tagValue || !tagValue?.length}
191193
keepWrapperWidth={!autoWidth}
192194
onPaste={onPaste}
193195
onClick={onInnerClick}

test/snap/__snapshots__/csr.test.jsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150694,7 +150694,7 @@ exports[`ssr snapshot test > ssr test packages/components/textarea/_example/even
150694150694

150695150695
exports[`ssr snapshot test > ssr test packages/components/textarea/_example/maxlength.tsx 1`] = `"<div style="width:100%;gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容,超出限制无法输入" style="height:auto;min-height:auto" class="t-textarea__inner"></textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--normal">这里可以放一些提示文字</div><span class="t-textarea__limit">0<!-- -->/20</span></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容,超出限制可以输入" style="height:auto;min-height:auto" class="t-textarea__inner"></textarea><div class="t-textarea__info_wrapper t-textarea__info_wrapper_align"><span class="t-textarea__limit">0<!-- -->/20</span></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容,一个中文汉字表示两个字符长度,超出限制无法输入" style="height:auto;min-height:auto" class="t-textarea__inner"></textarea><div class="t-textarea__info_wrapper t-textarea__info_wrapper_align"><span class="t-textarea__limit">0<!-- -->/20</span></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容,一个中文汉字表示两个字符长度,超出限制可以输入" style="height:auto;min-height:auto" class="t-textarea__inner"></textarea><div class="t-textarea__info_wrapper t-textarea__info_wrapper_align"><span class="t-textarea__limit">0<!-- -->/20</span></div></div></div></div>"`;
150696150696

150697-
exports[`ssr snapshot test > ssr test packages/components/textarea/_example/type.tsx 1`] = `"<div style="width:100%;gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容" style="height:auto;min-height:auto" class="t-textarea__inner" readonly="">只读状态</textarea></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容" style="height:auto;min-height:auto" class="t-textarea__inner t-is-disabled" disabled="">禁用状态</textarea></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="normal" style="height:auto;min-height:auto" class="t-textarea__inner">普通状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--normal">正常提示</div></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="success" style="height:auto;min-height:auto" class="t-textarea__inner t-is-success">成功状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--success">成功提示</div></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="warning" style="height:auto;min-height:auto" class="t-textarea__inner t-is-warning">警告状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--warning">警告提示</div></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="error" style="height:auto;min-height:auto" class="t-textarea__inner t-is-error">错误状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--error">错误提示</div></div></div></div></div>"`;
150697+
exports[`ssr snapshot test > ssr test packages/components/textarea/_example/type.tsx 1`] = `"<div style="width:100%;gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容" readonly="" style="height:auto;min-height:auto" class="t-textarea__inner">只读状态</textarea></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容" style="height:auto;min-height:auto" class="t-textarea__inner t-is-disabled" disabled="">禁用状态</textarea></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="normal" style="height:auto;min-height:auto" class="t-textarea__inner">普通状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--normal">正常提示</div></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="success" style="height:auto;min-height:auto" class="t-textarea__inner t-is-success">成功状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--success">成功提示</div></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="warning" style="height:auto;min-height:auto" class="t-textarea__inner t-is-warning">警告状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--warning">警告提示</div></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="error" style="height:auto;min-height:auto" class="t-textarea__inner t-is-error">错误状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--error">错误提示</div></div></div></div></div>"`;
150698150698

150699150699
exports[`ssr snapshot test > ssr test packages/components/time-picker/_example/disabled.tsx 1`] = `"<div style="gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><h3>禁用整个选择器</h3></div><div class="t-space-item"><div class="t-time-picker"><div class="t-time-picker__group t-select-input t-select-input--empty"><div class="t-input__wrap" spellcheck="false"><div class="t-input t-is-disabled t-align-left t-input--suffix"><input placeholder="选择时间" type="text" class="t-input__inner" readonly="" disabled="" value=""/><span class="t-input__suffix t-input__suffix-icon"><svg fill="none" viewBox="0 0 24 24" width="1em" height="1em" class="t-icon t-icon-time" style="fill:none"><g id="time"><path id="fill1" fill="transparent" d="M2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22C6.47715 22 2 17.5228 2 12Z"></path><path id="stroke1" stroke="currentColor" d="M2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22C6.47715 22 2 17.5228 2 12Z" stroke-linecap="square" stroke-width="2"></path><path id="stroke2" stroke="currentColor" d="M12 6.5L12 12L15 15" stroke-linecap="square" stroke-width="2"></path></g></svg></span></div></div></div></div></div><div class="t-space-item"><h3>禁用指定时间</h3></div><div class="t-space-item"><div class="t-time-picker"><div class="t-time-picker__group t-select-input t-select-input--empty"><div class="t-input__wrap" spellcheck="false"><div class="t-input t-align-left t-input--suffix"><input placeholder="选择时间" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__suffix t-input__suffix-icon"><svg fill="none" viewBox="0 0 24 24" width="1em" height="1em" class="t-icon t-icon-time" style="fill:none"><g id="time"><path id="fill1" fill="transparent" d="M2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22C6.47715 22 2 17.5228 2 12Z"></path><path id="stroke1" stroke="currentColor" d="M2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22C6.47715 22 2 17.5228 2 12Z" stroke-linecap="square" stroke-width="2"></path><path id="stroke2" stroke="currentColor" d="M12 6.5L12 12L15 15" stroke-linecap="square" stroke-width="2"></path></g></svg></span></div></div></div></div></div></div>"`;
150700150700

test/snap/__snapshots__/ssr.test.jsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ exports[`ssr snapshot test > ssr test packages/components/textarea/_example/even
11181118

11191119
exports[`ssr snapshot test > ssr test packages/components/textarea/_example/maxlength.tsx 1`] = `"<div style="width:100%;gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容,超出限制无法输入" style="height:auto;min-height:auto" class="t-textarea__inner"></textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--normal">这里可以放一些提示文字</div><span class="t-textarea__limit">0<!-- -->/20</span></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容,超出限制可以输入" style="height:auto;min-height:auto" class="t-textarea__inner"></textarea><div class="t-textarea__info_wrapper t-textarea__info_wrapper_align"><span class="t-textarea__limit">0<!-- -->/20</span></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容,一个中文汉字表示两个字符长度,超出限制无法输入" style="height:auto;min-height:auto" class="t-textarea__inner"></textarea><div class="t-textarea__info_wrapper t-textarea__info_wrapper_align"><span class="t-textarea__limit">0<!-- -->/20</span></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容,一个中文汉字表示两个字符长度,超出限制可以输入" style="height:auto;min-height:auto" class="t-textarea__inner"></textarea><div class="t-textarea__info_wrapper t-textarea__info_wrapper_align"><span class="t-textarea__limit">0<!-- -->/20</span></div></div></div></div>"`;
11201120

1121-
exports[`ssr snapshot test > ssr test packages/components/textarea/_example/type.tsx 1`] = `"<div style="width:100%;gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容" style="height:auto;min-height:auto" class="t-textarea__inner" readonly="">只读状态</textarea></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容" style="height:auto;min-height:auto" class="t-textarea__inner t-is-disabled" disabled="">禁用状态</textarea></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="normal" style="height:auto;min-height:auto" class="t-textarea__inner">普通状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--normal">正常提示</div></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="success" style="height:auto;min-height:auto" class="t-textarea__inner t-is-success">成功状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--success">成功提示</div></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="warning" style="height:auto;min-height:auto" class="t-textarea__inner t-is-warning">警告状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--warning">警告提示</div></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="error" style="height:auto;min-height:auto" class="t-textarea__inner t-is-error">错误状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--error">错误提示</div></div></div></div></div>"`;
1121+
exports[`ssr snapshot test > ssr test packages/components/textarea/_example/type.tsx 1`] = `"<div style="width:100%;gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容" readonly="" style="height:auto;min-height:auto" class="t-textarea__inner">只读状态</textarea></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="请输入内容" style="height:auto;min-height:auto" class="t-textarea__inner t-is-disabled" disabled="">禁用状态</textarea></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="normal" style="height:auto;min-height:auto" class="t-textarea__inner">普通状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--normal">正常提示</div></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="success" style="height:auto;min-height:auto" class="t-textarea__inner t-is-success">成功状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--success">成功提示</div></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="warning" style="height:auto;min-height:auto" class="t-textarea__inner t-is-warning">警告状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--warning">警告提示</div></div></div></div><div class="t-space-item"><div class="t-textarea"><textarea placeholder="error" style="height:auto;min-height:auto" class="t-textarea__inner t-is-error">错误状态</textarea><div class="t-textarea__info_wrapper"><div class="t-textarea__tips t-textarea__tips--error">错误提示</div></div></div></div></div>"`;
11221122

11231123
exports[`ssr snapshot test > ssr test packages/components/time-picker/_example/disabled.tsx 1`] = `"<div style="gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><h3>禁用整个选择器</h3></div><div class="t-space-item"><div class="t-time-picker"><div class="t-time-picker__group t-select-input t-select-input--empty"><div class="t-input__wrap" spellcheck="false"><div class="t-input t-is-disabled t-align-left t-input--suffix"><input placeholder="选择时间" type="text" class="t-input__inner" readonly="" disabled="" value=""/><span class="t-input__suffix t-input__suffix-icon"><svg fill="none" viewBox="0 0 24 24" width="1em" height="1em" class="t-icon t-icon-time" style="fill:none"><g id="time"><path id="fill1" fill="transparent" d="M2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22C6.47715 22 2 17.5228 2 12Z"></path><path id="stroke1" stroke="currentColor" d="M2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22C6.47715 22 2 17.5228 2 12Z" stroke-linecap="square" stroke-width="2"></path><path id="stroke2" stroke="currentColor" d="M12 6.5L12 12L15 15" stroke-linecap="square" stroke-width="2"></path></g></svg></span></div></div></div></div></div><div class="t-space-item"><h3>禁用指定时间</h3></div><div class="t-space-item"><div class="t-time-picker"><div class="t-time-picker__group t-select-input t-select-input--empty"><div class="t-input__wrap" spellcheck="false"><div class="t-input t-align-left t-input--suffix"><input placeholder="选择时间" type="text" class="t-input__inner" readonly="" value=""/><span class="t-input__suffix t-input__suffix-icon"><svg fill="none" viewBox="0 0 24 24" width="1em" height="1em" class="t-icon t-icon-time" style="fill:none"><g id="time"><path id="fill1" fill="transparent" d="M2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22C6.47715 22 2 17.5228 2 12Z"></path><path id="stroke1" stroke="currentColor" d="M2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22C6.47715 22 2 17.5228 2 12Z" stroke-linecap="square" stroke-width="2"></path><path id="stroke2" stroke="currentColor" d="M12 6.5L12 12L15 15" stroke-linecap="square" stroke-width="2"></path></g></svg></span></div></div></div></div></div></div>"`;
11241124

0 commit comments

Comments
 (0)