Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When clearing via "Backspace" the mask stays and is send as part of the value #1118

Closed
solenoo opened this issue Jun 14, 2019 · 2 comments · Fixed by #1160
Closed

When clearing via "Backspace" the mask stays and is send as part of the value #1118

solenoo opened this issue Jun 14, 2019 · 2 comments · Fixed by #1160
Labels
bug 🐛 Something isn't working important Need work

Comments

@solenoo
Copy link

solenoo commented Jun 14, 2019

Environment

Tech Version
@material-ui/pickers v3.1.0
material-ui v4.1.1
React v16.8.6
Browser Chrome: v74.0.3729.169
Peer library moment

Steps to reproduce

  1. Use KeyboardDatePicker
  2. Start typing a date
  3. Start clearing date via Backspace on the keyboard

Expected behavior

When I don't have a value, the mask shouldn't stay. It should just go to initial state, which is no value.

Actual behavior

The mask stays and my input is marked as invalid. Even if I use Ctrl + A + Backspace I still get an Invalid field

Live example

You can see it in your docks

@dmtrKovalenko dmtrKovalenko added bug 🐛 Something isn't working important Need work labels Jun 16, 2019
@DZapdos
Copy link

DZapdos commented Jun 28, 2019

In addition, or possibly as a workaround can we get a prop that allows the visual mask to be optional?

This would also fix the issue of having the maskChar appear in the input field when typing, which can be annoying when you want to edit an incomplete entry and the cursor is placed at the end of the string, including the mask, and you have to move the cursor to before the mask characters before being able to type.

@DZapdos
Copy link

DZapdos commented Jun 28, 2019

The way i'm currently working around this is by using onBlur to set the value to null when only the mask is showing.

const maskChar = '_';
const dateFormat = 'dd-MM-yyyy';

export const InlineDatePicker: React.FC<any> = props => {
  const {
    id,
    value,
    onChange,
    label,
    ...restInput
  } = props;

  return (
    <KeyboardDatePicker
      id={id}
      variant="inline"
      margin="normal"
      label={label}
      value={value}
      {...restInput}
      format={dateFormat}
      maskChar={maskChar}
      onBlur={e => handleBlur(e, props)}
    />
  );
};

const handleBlur = (e, props) => {
  const displayValue: string = e.target.value;

  const maskOnly = maskChar.repeat(dateFormat.length);

  // if the field is completely empty, only the mask is showing
  if (displayValue === maskOnly) {
    if (props.onBlur) {
      props.onBlur();
    }

    props.onChange(null);
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working important Need work
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants