Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
Discussion options

I have a form that contains these two fields, which I only use to display content:

          <Input name="email" label="E-mail" variant="standard" disabled />
          <Input name="cpf" label="CPF" variant="standard" disabled />

However when clikcing the submit button, the handleSubmit function still reads them:

 const handleSubmit: SubmitHandler = async (fieldValues) => {
        console.log(fieldValues);
  };

How do I make it that fields that are disabled will not be read?

I don't know if this is supposed to be the default behavior. If it is, maybe it's my Input component fault, that goes like this:

import { InputAdornment, TextFieldProps } from '@material-ui/core';
import { useField } from '@unform/core';
import { useEffect, useRef, FC, ReactNode } from 'react';
import InputMask from 'react-input-mask';
import TextField from './styles';

export type InputProps = TextFieldProps & {
  name: string;
  startAdornment?: ReactNode;
  variant: 'standard' | 'filled' | 'outlined';
  disabled?: boolean;
  mask?: string;
};

const Input: FC<InputProps> = ({
  name,
  label,
  required,
  placeholder,
  disabled,
  variant = 'standard',
  startAdornment,
  mask,
  ...rest
}) => {
  const ref = useRef();

  const { fieldName, registerField, defaultValue = '', error } = useField(name);

  useEffect(() => {
    registerField({
      name: fieldName,
      ref: ref.current,
      path: 'value'
    });
  }, [fieldName, registerField]);

  const startAdornmentComponent = startAdornment && (
    <InputAdornment position="start">{startAdornment}</InputAdornment>
  );

  const textFieldProps = {
    error: !!error,
    helperText: error,
    variant,
    label,
    disabled,
    placeholder: disabled ? '' : placeholder,
    required,
    inputProps: {
      ref,
      id: fieldName,
      defaultValue,
      disabled
    },
    InputProps: {
      startAdornment: startAdornmentComponent
      // style
    },
    ...rest
  };

  return (
    <InputMask disabled={disabled} mask={mask}>
      {() => <TextField {...textFieldProps} />}
    </InputMask>
  );
};
export default Input;
You must be logged in to vote

Replies: 1 comment

Comment options

I figured it out. All I had to do was not registerFieldif the Input component was disabled:

  useEffect(() => {
    if (!disabled) {
      registerField({
        name: fieldName,
        ref: ref.current,
        path: 'value'
      });
    }
  }, [fieldName, registerField, disabled]);
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.