Skip to content

Commit

Permalink
Do not throw error if input value was cleared symbol by symbol (#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrKovalenko authored Jul 5, 2019
1 parent 634bf87 commit 890af28
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
26 changes: 20 additions & 6 deletions e2e/integration/DatePicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,27 @@ describe('DatePicker', () => {
cy.get(`input${ids.clearable}`).should('have.value', '');
});

it('Should not accept invalid date format', () => {
cy.get(ids.maskedKeyboard)
.clear()
.type('01');
cy.get(`${ids.maskedKeyboard}-helper-text`).should('have.text', 'Invalid Date Format');
context('Masked input', () => {
it('Should not accept invalid date format', () => {
cy.get(ids.maskedKeyboard)
.clear()
.type('01');
cy.get(`${ids.maskedKeyboard}-helper-text`).should('have.text', 'Invalid Date Format');

cy.get(ids.maskedKeyboard).clear();
});

it('Should clear mask input when removing all text', () => {
cy.get(ids.maskedKeyboard).clear();

cy.get(`${ids.maskedKeyboard}-helper-text`).should('not.be.visible');
});

it('Should clear mask input when removing symbols one by one', () => {
cy.get(ids.maskedKeyboard).type('1{backspace}');

cy.get(ids.maskedKeyboard).clear();
cy.get(`${ids.maskedKeyboard}-helper-text`).should('not.be.visible');
});
});

it('Should accept date entered from keyboard', () => {
Expand Down
9 changes: 7 additions & 2 deletions lib/src/_shared/KeyboardDateInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { makeMaskFromFormat, maskedDateFormatter } from '../_helpers/text-field-
export interface KeyboardDateInputProps
extends ExtendMui<BaseTextFieldProps, 'variant' | 'onError' | 'onChange' | 'value'> {
format: string;
onChange: (value: string) => void;
onChange: (value: string | null) => void;
onClick?: () => void;
validationError?: React.ReactNode;
inputValue: string;
Expand Down Expand Up @@ -79,10 +79,15 @@ const KeyboardDateInput: React.FunctionComponent<KeyboardDateInputProps> = ({
const position =
InputAdornmentProps && InputAdornmentProps.position ? InputAdornmentProps.position : 'end';

const handleChange = (text: string) => {
const finalString = text === '' || text === inputMask ? null : text;
onChange(finalString);
};

return (
<Rifm
value={inputValue}
onChange={onChange}
onChange={handleChange}
refuse={refuse}
format={rifmFormatter || formatter}
>
Expand Down
6 changes: 3 additions & 3 deletions lib/src/_shared/hooks/useKeyboardPickerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export function useKeyboardPickerState(props: BaseKeyboardPickerProps, options:
...innerInputProps, // reuse validation and open/close logic
format: wrapperProps.format,
inputValue: inputValue || innerInputValue,
onChange: (value: string) => {
setInnerInputValue(value);
const date = value === '' ? null : utils.parse(value, wrapperProps.format);
onChange: (value: string | null) => {
setInnerInputValue(value || '');
const date = value === null ? null : utils.parse(value, wrapperProps.format);

onChange(date, value);
},
Expand Down

0 comments on commit 890af28

Please sign in to comment.