This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Form): Add <PhoneInput /> and <DateInput />; Clean up <Select /> (…
…#34) - Adds new `<PhoneInput />` and `<DateInput />` components for easy input masking - Fixes some existing styling issues with `<Select />`
- Loading branch information
Showing
14 changed files
with
634 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import DateFormatter from 'cleave.js/src/shortcuts/DateFormatter'; | ||
import Input from './Input'; | ||
import { createEasyInput } from './EasyInput'; | ||
import { getNextCursorPosition } from '../utils'; | ||
|
||
export const getRawMaxLength = pattern => { | ||
const formatter = new DateFormatter(pattern); | ||
const blocks = formatter.getBlocks(); | ||
return blocks.reduce((sum, block) => sum + block, 0); | ||
}; | ||
|
||
const formatDate = (pattern, delimiter, dateString = '') => { | ||
const formatter = new DateFormatter(pattern); | ||
|
||
// Process our date string, bounding values between 1 and 31, and prepending 0s for | ||
// for single digit blocks that can't have 2 numbers, e.g. 5 | ||
let tmpDate = formatter.getValidatedDate(`${dateString}`); | ||
|
||
// Blocks look something like [2, 2, 4], telling us how long each chunk should be | ||
return formatter.getBlocks().reduce((str, blockLength, index, blockArr) => { | ||
const block = tmpDate.substring(0, blockLength); | ||
if (!block) { | ||
return str; | ||
} | ||
|
||
tmpDate = tmpDate.substring(blockLength); | ||
|
||
// Append the delimiter if our block is complete and we're not at the last block | ||
const shouldAppendDelimiter = block.length === blockLength && index < blockArr.length - 1; | ||
|
||
return `${str}${block}${shouldAppendDelimiter ? delimiter : ''}`; | ||
}, ''); | ||
}; | ||
|
||
function DateInput({ delimiter, pattern, forwardedRef, value: propValue, onKeyDown, onChange, ...inputProps }) { | ||
const format = value => formatDate(pattern, delimiter, value); | ||
const [currentValue, setValue] = useState(format(propValue)); | ||
const inputRef = forwardedRef || useRef(); | ||
|
||
useEffect(() => { | ||
if (propValue !== currentValue) { | ||
setValue(format(propValue)); | ||
} | ||
}, [propValue]); | ||
|
||
const handleKeyDown = event => { | ||
const isLetterLike = /^\w{1}$/.test(event.key); | ||
if (isLetterLike && currentValue.replace(/\D/g, '').length >= getRawMaxLength(pattern)) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
} | ||
|
||
if (onKeyDown) { | ||
onKeyDown(event); | ||
} | ||
}; | ||
|
||
const handleChange = (name, newValue, event) => { | ||
const nextValue = newValue.length < currentValue.length ? newValue.trim() : format(newValue); | ||
const nextCursorPosition = getNextCursorPosition(event.target.selectionStart, currentValue, nextValue); | ||
|
||
setValue(nextValue); | ||
setTimeout(() => { | ||
inputRef.current.setSelectionRange(nextCursorPosition, nextCursorPosition); | ||
}); | ||
|
||
if (onChange) { | ||
onChange(name, nextValue, event); | ||
} | ||
}; | ||
|
||
return ( | ||
<Input | ||
forwardedRef={inputRef} | ||
value={currentValue} | ||
onKeyDown={handleKeyDown} | ||
onChange={handleChange} | ||
{...inputProps} | ||
/> | ||
); | ||
} | ||
|
||
DateInput.propTypes = { | ||
...Input.propTypes, | ||
pattern: PropTypes.arrayOf(PropTypes.string), | ||
delimiter: PropTypes.string, | ||
}; | ||
|
||
DateInput.defaultProps = { | ||
pattern: ['m', 'd', 'Y'], | ||
delimiter: '/', | ||
}; | ||
|
||
export default createEasyInput(DateInput); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import React from 'react'; | ||
import { renderWithTheme, fireEvent, act } from '../../test/utils'; | ||
import DateInput, { getRawMaxLength } from './DateInput'; | ||
import ThemeProvider from '../ThemeProvider'; | ||
|
||
describe('<DateInput />', () => { | ||
const renderInput = props => { | ||
const utils = renderWithTheme(<DateInput placeholder="Input" {...props} />); | ||
return { | ||
...utils, | ||
input: utils.getByPlaceholderText('Input'), | ||
}; | ||
}; | ||
|
||
const updateInputValue = (input, value) => { | ||
act(() => { | ||
fireEvent.change(input, { target: { value } }); | ||
}); | ||
}; | ||
|
||
test('#getMaxDateLength', () => { | ||
expect(getRawMaxLength(['m', 'd', 'Y'])).toEqual(8); | ||
expect(getRawMaxLength(['m', 'd', 'y'])).toEqual(6); | ||
}); | ||
|
||
test('snapshot', () => { | ||
const { asFragment } = renderInput(); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
test('initally sets the value', () => { | ||
const value = '12/05/1992'; | ||
const { input } = renderInput({ value }); | ||
|
||
expect(input.value).toEqual(value); | ||
}); | ||
|
||
test("prefixes month and day block with a 0 when second number isn't possible", () => { | ||
const { input } = renderInput(); | ||
|
||
updateInputValue(input, '4'); | ||
expect(input.value).toEqual('04/'); | ||
|
||
updateInputValue(input, '04/4'); | ||
expect(input.value).toEqual('04/04/'); | ||
}); | ||
|
||
test("doesn't prefix when second number is possible", () => { | ||
const { input } = renderInput(); | ||
|
||
updateInputValue(input, '1'); | ||
expect(input.value).toEqual('1'); | ||
|
||
updateInputValue(input, '12/1'); | ||
expect(input.value).toEqual('12/1'); | ||
}); | ||
|
||
test('truncates a date string that is too long', () => { | ||
const { input } = renderInput({ value: '12/05/19922222' }); | ||
|
||
expect(input.value).toEqual('12/05/1992'); | ||
}); | ||
|
||
test('can remove trailing slash when backspacing', () => { | ||
const { input } = renderInput({ value: '12/05/' }); | ||
|
||
fireEvent.keyDown(input, { key: 'Backspace' }); | ||
updateInputValue(input, '12/05'); | ||
expect(input.value).toEqual('12/05'); | ||
}); | ||
|
||
test('takes custom delimter and pattern', () => { | ||
const { input } = renderInput({ value: '2000-5', pattern: ['Y', 'm', 'd'], delimiter: '-' }); | ||
|
||
expect(input.value).toEqual('2000-05-'); | ||
}); | ||
|
||
test('updates internally when value prop changes', () => { | ||
const { input, rerender } = renderInput({ value: '01/05' }); | ||
rerender( | ||
<ThemeProvider> | ||
<DateInput placeholder="input" value="01/09/1990" /> | ||
</ThemeProvider> | ||
); | ||
expect(input.value).toEqual('01/09/1990'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.