反应条件属性

发布于 2025-01-10 16:05:39 字数 2985 浏览 1 评论 0原文

我想设置一个有条件的属性,但无法让它工作...我想仅在relativeFieldName === 'zipcode' 时放置占位符。非常感谢任何帮助! :)

import React from 'react';
import _get from 'lodash.get';
import { bool, string } from 'prop-types';
import { ErrorMessage, Field } from 'formik';
import { _replace } from '../../../utils';
import { formikDataShape } from '../../../utils/propTypes';
function TextInput({
  id,
  name,
  type,
  label,
  width,
  helpText,
  required,
  isHidden,
  className,
  formikData,
  placeholder,
  ...rest
}) {
  const {
    setFieldValue,
    formSectionId,
    setFieldTouched,
    formSectionErrors,
    formSectionValues,
    formSectionTouched,
  } = formikData;
  const inputId = id || name;
  const relativeFieldName = _replace(name, formSectionId).replace('.', '');
  const hasFieldError = !!_get(formSectionErrors, relativeFieldName);
  const value = _get(formSectionValues, relativeFieldName, '') || '';
  const hasFieldTouched = !!_get(formSectionTouched, relativeFieldName);
  const hasError = hasFieldError && hasFieldTouched;
  return (
    <div className={`mt-2 form-control ${isHidden ? 'hidden' : ''}`}>
      <div className="flex items-center justify-between">
        {label && (
          <label htmlFor={inputId} className="md:text-sm">
            {label}
            {required && <sup> *</sup>}
          </label>
        )}
      </div>
      <Field
        name={name}
        id={inputId}
        value={value}
        type={type || 'text'}
        placeholder={placeholder}
        onChange={(event) => {
          const newValue = event.target.value;
          setFieldTouched(name, newValue);
          setFieldValue(name, newValue);
        }}
        className={`form-input max-w-md ${
          hasError ? 'border-dashed border-red-500' : ''
        } ${className} ${width || 'w-full'}`}
        {...rest}
      />
      <div
        className={`feedback text-sm md:text-xs mt-2 ${
          hasError ? 'text-red-500' : 'text-green-500'
        }`}
      >
        <ErrorMessage name={name}>
          {(msg) => msg.replace('%1', label)}
        </ErrorMessage>
      </div>
      <div className="text-xs">{helpText}</div>
    </div>
  );
}
TextInput.propTypes = {
  id: string,
  type: string,
  label: string,
  width: string,
  required: bool,
  isHidden: bool,
  helpText: string,
  className: string,
  placeholder: string,
  name: string.isRequired,
  formikData: formikDataShape.isRequired,
};
TextInput.defaultProps = {
  id: '',
  label: '',
  width: '',
  helpText: '',
  type: 'text',
  className: '',
  required: false,
  placeholder: '',
  isHidden: false,
};
export default TextInput;

顺便说一句:此代码用于 Magento Hyva checkout React。代码位于 magento2/app/code/Hyva/Checkout/reactapp/src/components/common/Form/TextInput.jsx

我已将其更改为(并且有效): placeholder={name === 'shipping_address.zipcode' ?占位符:''} 感谢您让我走上正确的方向! :)

I would like to make an attribute conditional but can't get it to work...I would like to place the placeholder ONLY when the relativeFieldName === 'zipcode'. Any help is highly appreciated! :)

import React from 'react';
import _get from 'lodash.get';
import { bool, string } from 'prop-types';
import { ErrorMessage, Field } from 'formik';
import { _replace } from '../../../utils';
import { formikDataShape } from '../../../utils/propTypes';
function TextInput({
  id,
  name,
  type,
  label,
  width,
  helpText,
  required,
  isHidden,
  className,
  formikData,
  placeholder,
  ...rest
}) {
  const {
    setFieldValue,
    formSectionId,
    setFieldTouched,
    formSectionErrors,
    formSectionValues,
    formSectionTouched,
  } = formikData;
  const inputId = id || name;
  const relativeFieldName = _replace(name, formSectionId).replace('.', '');
  const hasFieldError = !!_get(formSectionErrors, relativeFieldName);
  const value = _get(formSectionValues, relativeFieldName, '') || '';
  const hasFieldTouched = !!_get(formSectionTouched, relativeFieldName);
  const hasError = hasFieldError && hasFieldTouched;
  return (
    <div className={`mt-2 form-control ${isHidden ? 'hidden' : ''}`}>
      <div className="flex items-center justify-between">
        {label && (
          <label htmlFor={inputId} className="md:text-sm">
            {label}
            {required && <sup> *</sup>}
          </label>
        )}
      </div>
      <Field
        name={name}
        id={inputId}
        value={value}
        type={type || 'text'}
        placeholder={placeholder}
        onChange={(event) => {
          const newValue = event.target.value;
          setFieldTouched(name, newValue);
          setFieldValue(name, newValue);
        }}
        className={`form-input max-w-md ${
          hasError ? 'border-dashed border-red-500' : ''
        } ${className} ${width || 'w-full'}`}
        {...rest}
      />
      <div
        className={`feedback text-sm md:text-xs mt-2 ${
          hasError ? 'text-red-500' : 'text-green-500'
        }`}
      >
        <ErrorMessage name={name}>
          {(msg) => msg.replace('%1', label)}
        </ErrorMessage>
      </div>
      <div className="text-xs">{helpText}</div>
    </div>
  );
}
TextInput.propTypes = {
  id: string,
  type: string,
  label: string,
  width: string,
  required: bool,
  isHidden: bool,
  helpText: string,
  className: string,
  placeholder: string,
  name: string.isRequired,
  formikData: formikDataShape.isRequired,
};
TextInput.defaultProps = {
  id: '',
  label: '',
  width: '',
  helpText: '',
  type: 'text',
  className: '',
  required: false,
  placeholder: '',
  isHidden: false,
};
export default TextInput;

btw: this code is used in Magento Hyva checkout React. The code is in magento2/app/code/Hyva/Checkout/reactapp/src/components/common/Form/TextInput.jsx

I've changed it to (and works): placeholder={name === 'shipping_address.zipcode'? placeholder : ''}
Thanks for putting me in the right direction! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2025-01-17 16:05:39
<Field 
       ...
        placeholder={relativeFieldName === 'zipcode'? placeholder : undefined}
       ...
      />
<Field 
       ...
        placeholder={relativeFieldName === 'zipcode'? placeholder : undefined}
       ...
      />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文