Skip to content

Commit

Permalink
refactor(password-input): migrate to TypeScript (#1870)
Browse files Browse the repository at this point in the history
* refactor(password-input): migrate to TypeScript

* fix(password-input): set `autoComplete` as optional
  • Loading branch information
adnasa authored Apr 28, 2021
1 parent d8530a8 commit 0f28003
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 112 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-jars-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-uikit/password-input': patch
---

Migrate `<PasswordInput />` to TypeScript
42 changes: 20 additions & 22 deletions packages/components/inputs/password-input/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ import PasswordInput from '@commercetools-uikit/password-input';
const Example = () => (
<PasswordInput
value="foo"
onChange={
(/** event */) => {
/** alert(event.target.value) */
}
}
onChange={(event) => {
alert(event.target.value);
}}
/>
);

Expand All @@ -49,23 +47,23 @@ export default Example;

## Properties

| Props | Type | Required | Default | Description |
| ---------------------- | -------------------------------------------------------------------------------------------------- | :------: | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | `string` | | | Used as HTML id property. An id is auto-generated when it is not specified. |
| `name` | `string` | | | Used as HTML name of the input component. property |
| `value` | `string` || | Value of the input component. |
| `onChange` | `custom` | | | Called with an event containing the new value. Required when input is not read only. Parent should pass it back as value.&#xA;<br />&#xA;Signature: `(event) => void` |
| `onBlur` | `func` | | | Called when input is blurred&#xA;<br />&#xA;Signature: `(event) => void` |
| `onFocus` | `func` | | | Called when input is focused&#xA;<br />&#xA;Signature: `(event) => void` |
| `isAutofocussed` | `bool` | | | Focus the input on initial render |
| `isDisabled` | `bool` | | `false` | Indicates that the input cannot be modified (e.g not authorized, or changes currently saving). |
| `isReadOnly` | `bool` | | `false` | Indicates that the field is displaying read-only content |
| `hasError` | `bool` | | | Indicates that the input has an error |
| `hasWarning` | `bool` | | | Indicates that the input has a warning due to e.g invalid values |
| `isPasswordVisible` | `bool` | | `false` | Indicates whether we show the password or not |
| `placeholder` | `string` | | | Placeholder text for the input |
| `horizontalConstraint` | `enum`<br/>Possible values:<br/>`3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 'scale', 'auto'` | | `'scale'` | Horizontal size limit of the input fields. |
| `autoComplete` | `enum`<br/>Possible values:<br/>`'on', 'off', 'current-password', 'new-password'` | | | Password autocomplete mode |
| Props | Type | Required | Default | Description |
| ---------------------- | ----------------------------------------------------------------------------------------------------- | :------: | --------- | ------------------------------------------------------------------------------------------------------------------------- |
| `id` | `string` | | | Used as HTML id property. An id is auto-generated when it is not specified. |
| `name` | `string` | | | Used as HTML name of the input component. property |
| `value` | `string` || | Value of the input component. |
| `placeholder` | `string` | | | Placeholder text for the input |
| `onChange` | `ChangeEventHandler` | | | Called with an event containing the new value. Required when input is not read only. Parent should pass it back as value. |
| `onBlur` | `FocusEventHandler` | | | Called when input is blurred. |
| `onFocus` | `FocusEventHandler` | | | Called when input is focused. |
| `isAutofocussed` | `boolean` | | | Focus the input on initial render. |
| `isDisabled` | `boolean` | | `false` | Indicates that the input cannot be modified (e.g not authorized, or changes currently saving). |
| `isReadOnly` | `boolean` | | `false` | Indicates that the field is displaying read-only content |
| `hasError` | `boolean` | | | Indicates that the input has an error |
| `hasWarning` | `boolean` | | | Indicates that the input has a warning due to e.g invalid values |
| `isPasswordVisible` | `boolean` | | `false` | Indicates whether we show the password or not |
| `horizontalConstraint` | `union`<br/>Possible values:<br/>`, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 'scale', 'auto'` | | `'scale'` | Horizontal size limit of the input fields. |
| `autoComplete` | `union`<br/>Possible values:<br/>`'on' , 'off' , 'current-password' , 'new-password'` | | | Password autocomplete mode |

## `data-*` props

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import PasswordInput from '@commercetools-uikit/password-input';
const Example = () => (
<PasswordInput
value="foo"
onChange={
(/** event */) => {
/** alert(event.target.value) */
}
}
onChange={(event) => {
alert(event.target.value);
}}
/>
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,138 +1,136 @@
import React from 'react';
import PropTypes from 'prop-types';
import requiredIf from 'react-required-if';
import React, { ChangeEventHandler, FocusEventHandler } from 'react';
import { useTheme } from '@emotion/react';
import { filterDataAttributes } from '@commercetools-uikit/utils';
import { filterDataAttributes, warning } from '@commercetools-uikit/utils';
import Constraints from '@commercetools-uikit/constraints';
import { getInputStyles } from '@commercetools-uikit/input-utils';

const PasswordInput = (props) => {
const theme = useTheme();
return (
<Constraints.Horizontal max={props.horizontalConstraint}>
<input
id={props.id}
name={props.name}
type={props.isPasswordVisible ? 'text' : 'password'}
value={props.value}
onChange={props.onChange}
onBlur={props.onBlur}
onFocus={props.onFocus}
disabled={props.isDisabled}
placeholder={props.placeholder}
autoComplete={props.autoComplete}
css={getInputStyles(props, theme)}
readOnly={props.isReadOnly}
autoFocus={props.isAutofocussed}
{...filterDataAttributes(props)}
/* ARIA */
aria-readonly={props.isReadOnly}
contentEditable={!props.isReadOnly}
/>
</Constraints.Horizontal>
);
};

PasswordInput.displayName = 'PasswordInput';

PasswordInput.propTypes = {
type TPasswordInputProps = {
/**
* Used as HTML id property. An id is auto-generated when it is not specified.
*/
id: PropTypes.string,
id?: string;
/**
* Used as HTML name of the input component. property
*/
name: PropTypes.string,
name?: string;
/**
* Value of the input component.
*/
value: PropTypes.string.isRequired,
value: string;
/**
* Placeholder text for the input
*/
placeholder?: string;
/**
* Called with an event containing the new value. Required when input is not read only. Parent should pass it back as value.
* <br />
* Signature: `(event) => void`
*/
onChange: requiredIf(PropTypes.func, (props) => !props.isReadOnly),
onChange?: ChangeEventHandler;
//requiredIf(PropTypes.func, (props) => !props.isReadOnly),
/**
* Called when input is blurred
* <br />
* Signature: `(event) => void`
* Called when input is blurred.
*/
onBlur: PropTypes.func,
onBlur?: FocusEventHandler;
/**
* Called when input is focused
* <br />
* Signature: `(event) => void`
*
* Called when input is focused.
*/
onFocus: PropTypes.func,
onFocus?: FocusEventHandler;
/**
* Focus the input on initial render
* Focus the input on initial render.
*/
isAutofocussed: PropTypes.bool,
isAutofocussed?: boolean;
/**
* Indicates that the input cannot be modified (e.g not authorized, or changes currently saving).
*/
isDisabled: PropTypes.bool,
isDisabled?: boolean;
/**
* Indicates that the field is displaying read-only content
*/
isReadOnly: PropTypes.bool,
isReadOnly?: boolean;
/**
* Indicates that the input has an error
*/
hasError: PropTypes.bool,
hasError?: boolean;
/**
* Indicates that the input has a warning due to e.g invalid values
*/
hasWarning: PropTypes.bool,
hasWarning?: boolean;
/**
* Indicates whether we show the password or not
*/
isPasswordVisible: PropTypes.bool,
/**
* Placeholder text for the input
*/
placeholder: PropTypes.string,
isPasswordVisible?: boolean;
/**
* Horizontal size limit of the input fields.
*/
horizontalConstraint: PropTypes.oneOf([
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
'scale',
'auto',
]),
horizontalConstraint?:
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| 13
| 14
| 15
| 16
| 'scale'
| 'auto';
/**
* Password autocomplete mode
*/
autoComplete: PropTypes.oneOf([
'on',
'off',
'current-password',
'new-password',
]),
autoComplete?: 'on' | 'off' | 'current-password' | 'new-password';
};

PasswordInput.defaultProps = {
const defaultProps: Pick<
TPasswordInputProps,
'horizontalConstraint' | 'isDisabled' | 'isReadOnly' | 'isPasswordVisible'
> = {
horizontalConstraint: 'scale',
isDisabled: false,
isReadOnly: false,
isPasswordVisible: false,
};

PasswordInput.isEmpty = (value) => !value || value.trim().length === 0;
const PasswordInput = (props: TPasswordInputProps) => {
const theme = useTheme();
if (!props.isReadOnly) {
warning(
Boolean(props.onChange),
'PasswordInput: `onChange` is required when is not read only.'
);
}
return (
<Constraints.Horizontal max={props.horizontalConstraint}>
<input
id={props.id}
name={props.name}
type={props.isPasswordVisible ? 'text' : 'password'}
value={props.value}
onChange={props.onChange}
onBlur={props.onBlur}
onFocus={props.onFocus}
disabled={props.isDisabled}
placeholder={props.placeholder}
autoComplete={props.autoComplete}
css={getInputStyles(props, theme)}
readOnly={props.isReadOnly}
autoFocus={props.isAutofocussed}
{...filterDataAttributes(props)}
/* ARIA */
aria-readonly={props.isReadOnly}
contentEditable={!props.isReadOnly}
/>
</Constraints.Horizontal>
);
};

PasswordInput.displayName = 'PasswordInput';
PasswordInput.defaultProps = defaultProps;
PasswordInput.isEmpty = (value: TPasswordInputProps['value']) =>
!value || value.trim().length === 0;

export default PasswordInput;

1 comment on commit 0f28003

@vercel
Copy link

@vercel vercel bot commented on 0f28003 Apr 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.