reactjs - React:如何在为输入字段使用自定义组件时显示特定通知?
问题描述
我正在创建一个具有多个输入字段的简单组件。此字段正在使用自定义组件。该组件的功能之一是允许我在输入字段下方显示标题。该标题接受两种值,第一种是字段描述,另一种是错误消息。我的问题是如果后端返回一个错误消息,比如请输入一个值,由于我的输入字段组件,此消息将显示在所有输入字段上。请帮助我根据输入字段显示错误消息。
标题属性是将显示错误消息或字段标题的属性。
输入字段 import InputField from '../ui/InputField';
<InputField
variant='input'
type='text'
placeholder='Title'
name='title'
border={
error && error.titleError
? 'failed'
: 'transparent'
}
value={itemInfo.title}
maxLength='32'
error={error}
chkInputLength={true}
caption='Give your item short and clear name.'
onChange={handleOnChange}
/>
输入字段组件
const InputField = forwardRef(
(
{
variant,
value,
name,
placeholder,
type,
border,
error,
maxLength,
chkInputLength,
rows,
onChange,
caption,
},
ref
) => (
<>
{variant && variant === 'input' ? (
type && type === 'text' ? (
<input
autoComplete='nope'
type={type}
value={value}
name={name}
className={`bg-customBlack-light hover:bg-customBlackLighter-light w-full text-xs p-4 mb-2 rounded-full border border-${border} outline-none transition duration-250 ease-in-out`}
placeholder={placeholder}
maxLength={maxLength}
onChange={onChange}
ref={ref}
/>
) : type === 'number' ? (
<>
<div className='absolute top-0 left-0 mt-3.5 pl-5 flex items-center pointer-events-none'>
<span className='text-gray-500 sm:text-sm'>₱</span>
</div>
<input
autoComplete='nope'
type={type}
value={value}
name={name}
className={`bg-customBlack-light hover:bg-customBlackLighter-light w-2/5 text-xs pt-4 pl-8 pb-4 pr-4 mb-2 rounded-full border border-${border} outline-none transition duration-250 ease-in-out`}
placeholder={placeholder}
maxLength={maxLength}
onChange={onChange}
ref={ref}
/>
</>
) : (
''
)
) : variant === 'textarea' ? (
<textarea
type={type}
value={value}
name={name}
className={`bg-customBlack-light hover:bg-customBlackLighter-light w-full text-xs p-4 rounded-30px border border-${border} outline-none resize-none transition duration-250 ease-in-out`}
placeholder={placeholder}
rows={rows}
maxLength={maxLength}
onChange={onChange}
/>
) : (
<input
autoComplete='nope'
type={type}
value={value}
name={name}
className={`bg-customBlack-light hover:bg-customBlackLighter-light w-full text-xs p-4 mb-2 rounded-full border border-${border} outline-none transition duration-250 ease-in-out`}
placeholder={placeholder}
maxLength={maxLength}
onChange={onChange}
ref={ref}
/>
)}
{chkInputLength && (
<label htmlFor={name} className='text-toolTipSubTitle flex mr-4'>
{error && error.msg ? (
<span className='text-xxs text-customRed fadeIn'>{error.msg}</span>
) : (
<span className='text-toolTipSubTitle text-customBlack ml-4'>
{caption}
</span>
)}
<span className='text-customBlack font-semibold absolute right-0'>
{value && maxLength
? Math.abs(value.length - maxLength)
: maxLength}
</span>
</label>
)}
</>
)
);
解决方案
推荐阅读
- odata - SAPUI5:删除后合并而不是发布
- pandas - 通过对 pandas 中的给定字典进行特定计算来创建新列
- javascript - 在Javascript中获取元素的X,Y位置
- json - 如何在不消耗大量内存的情况下将一个大的 json 文件序列化为 Http 响应体?
- macos - 在 macOS 10.15.7 上的 Facetime 中拨打电话时无法移动小窗口
- android - 如何设置 Android MaterialCalendars 各种组件的样式
- dart - Dart 在构造函数中将 this 作为参数传递
- python - scipy.optimize.minimize 在 x0 处停止
- java - 使用 java 配置 IMAP 服务器
- c - strftime 没有给出 struct tm 的年份