-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(password-input): migrate to TypeScript (#1870)
* refactor(password-input): migrate to TypeScript * fix(password-input): set `autoComplete` as optional
- Loading branch information
Showing
7 changed files
with
111 additions
and
112 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
'@commercetools-uikit/password-input': patch | ||
--- | ||
|
||
Migrate `<PasswordInput />` to TypeScript |
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
File renamed without changes.
File renamed without changes.
168 changes: 83 additions & 85 deletions
168
...puts/password-input/src/password-input.js → ...uts/password-input/src/password-input.tsx
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 |
---|---|---|
@@ -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; |
File renamed without changes.
0f28003
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: