Skip to content

Commit

Permalink
refactor: refactorng after review
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Sep 18, 2023
1 parent 029f3d9 commit 62b9d12
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 27 deletions.
16 changes: 5 additions & 11 deletions src/Form/FormControl.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import FormControlDecoratorGroup from './FormControlDecoratorGroup';

import { callAllHandlers, useHasValue } from './fieldUtils';

const DEFAULT_MASK_VALUE = '00-00-000';

const FormControl = React.forwardRef(({
as,
className,
Expand All @@ -21,7 +19,6 @@ const FormControl = React.forwardRef(({
autoResize,
onChange,
hasInputMask,
mask,
...props
}, ref) => {
const {
Expand Down Expand Up @@ -76,7 +73,7 @@ const FormControl = React.forwardRef(({
className={className}
>
<RBFormControl
as={hasInputMask ? IMaskInput : as}
as={hasInputMask?.length ? IMaskInput : as}
ref={resolvedRef}
size={size}
isInvalid={isInvalid}
Expand All @@ -85,7 +82,7 @@ const FormControl = React.forwardRef(({
'has-value': hasValue,
})}
onChange={handleOnChange}
mask={mask}
mask={hasInputMask}
{...controlProps}
/>
</FormControlDecoratorGroup>
Expand Down Expand Up @@ -128,10 +125,8 @@ FormControl.propTypes = {
isInvalid: PropTypes.bool,
/** Only for `as="textarea"`. Specifies whether the input can be resized according to the height of content. */
autoResize: PropTypes.bool,
/** Specifies whether to use an input mask for the input. */
hasInputMask: PropTypes.bool,
/** Specifies the input mask to be used if `hasInputMask` is set to `true`. */
mask: PropTypes.string,
/** Specifies what format to use for the input mask. */
hasInputMask: PropTypes.string,
};

FormControl.defaultProps = {
Expand All @@ -150,8 +145,7 @@ FormControl.defaultProps = {
isValid: undefined,
isInvalid: undefined,
autoResize: false,
hasInputMask: false,
mask: DEFAULT_MASK_VALUE,
hasInputMask: undefined,
};

export default FormControl;
21 changes: 7 additions & 14 deletions src/Form/form-control.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ See [react-imask](https://imask.js.org) for documentation on available props.
<h3>Phone</h3>
<Form.Group>
<Form.Control
hasInputMask
mask="+{1}(000)000-00-00"
hasInputMask="+{1}(000)000-00-00"
value={value}
onChange={handleChange}
leadingElement={<Icon src={FavoriteBorder} />}
Expand All @@ -193,8 +192,7 @@ See [react-imask](https://imask.js.org) for documentation on available props.
<h3>Credit card</h3>
<Form.Group>
<Form.Control
hasInputMask
mask="0000 0000 0000 0000"
hasInputMask="0000 0000 0000 0000"
value={value}
onChange={handleChange}
leadingElement={<Icon src={FavoriteBorder} />}
Expand All @@ -206,8 +204,7 @@ See [react-imask](https://imask.js.org) for documentation on available props.
<h3>Date</h3>
<Form.Group>
<Form.Control
hasInputMask
mask={Date}
hasInputMask={Date}
value={value}
onChange={handleChange}
leadingElement={<Icon src={FavoriteBorder} />}
Expand All @@ -219,8 +216,7 @@ See [react-imask](https://imask.js.org) for documentation on available props.
<h3>Secure password</h3>
<Form.Group>
<Form.Control
hasInputMask
mask="XXX-XX-0000"
hasInputMask="XXX-XX-0000"
value={value}
definitions={{
X: {
Expand All @@ -239,8 +235,7 @@ See [react-imask](https://imask.js.org) for documentation on available props.
<h3>OTP password</h3>
<Form.Group>
<Form.Control
hasInputMask
mask="G-00000"
hasInputMask="G-00000"
value={value}
onChange={handleChange}
leadingElement={<Icon src={FavoriteBorder} />}
Expand All @@ -253,8 +248,7 @@ See [react-imask](https://imask.js.org) for documentation on available props.
<h3>Course prise</h3>
<Form.Group>
<Form.Control
hasInputMask
mask="$num"
hasInputMask="$num"
blocks={{
num: {
// nested masks are available!
Expand Down Expand Up @@ -303,8 +297,7 @@ See [react-imask](https://imask.js.org) for documentation on available props.
return (
<Form.Group>
<Form.Control
hasInputMask
mask="+{1}(000)000-00-00"
hasInputMask="+{1}(000)000-00-00"
leadingElement={<Icon src={FavoriteBorder} />}
trailingElement={<Icon src={Verified} />}
floatingLabel="What is your phone number?"
Expand Down
3 changes: 1 addition & 2 deletions src/Form/tests/FormControl.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ function Component({ isClearValue }) {

return (
<FormControl
hasInputMask
mask="+{1}(000)000-00-00"
hasInputMask="+{1}(000)000-00-00"
value={inputValue}
onChange={isClearValue ? onChangeFunc : (e) => setInputValue(e.target.value)}
onAccept={isClearValue ? onChangeFunc : (value) => setInputValue(value)}
Expand Down

0 comments on commit 62b9d12

Please sign in to comment.