-
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(text-input): migrate to TypeScript (#1868)
- Loading branch information
Showing
7 changed files
with
103 additions
and
95 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/text-input': patch | ||
--- | ||
|
||
Migrate `<TextInput />` 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
File renamed without changes.
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.
156 changes: 80 additions & 76 deletions
156
...nents/inputs/text-input/src/text-input.js → ...ents/inputs/text-input/src/text-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,127 +1,131 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import requiredIf from 'react-required-if'; | ||
import React, { FocusEventHandler, ChangeEventHandler } 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 TextInput = (props) => { | ||
const theme = useTheme(); | ||
return ( | ||
<Constraints.Horizontal max={props.horizontalConstraint}> | ||
<input | ||
id={props.id} | ||
name={props.name} | ||
type="text" | ||
value={props.value} | ||
onChange={props.onChange} | ||
onBlur={props.onBlur} | ||
onFocus={props.onFocus} | ||
disabled={props.isDisabled} | ||
placeholder={props.placeholder} | ||
readOnly={props.isReadOnly} | ||
autoFocus={props.isAutofocussed} | ||
autoComplete={props.autoComplete} | ||
css={getInputStyles(props, theme)} | ||
// Allow to override the styles by passing a `className` prop. | ||
// Custom styles can also be passed using the `css` prop from emotion. | ||
// https://emotion.sh/docs/css-prop#style-precedence | ||
className={props.className} | ||
{...filterDataAttributes(props)} | ||
/* ARIA */ | ||
aria-readonly={props.isReadOnly} | ||
role="textbox" | ||
contentEditable={!props.isReadOnly} | ||
/> | ||
</Constraints.Horizontal> | ||
); | ||
}; | ||
|
||
TextInput.displayName = 'TextInput'; | ||
|
||
TextInput.propTypes = { | ||
type TTextInputProps = { | ||
/** | ||
* Used as HTML id property. An id is auto-generated when it is not specified. | ||
*/ | ||
id: PropTypes.string, | ||
id?: string; | ||
/** | ||
* Used as HTML autocomplete property | ||
*/ | ||
autoComplete: PropTypes.string, | ||
className: PropTypes.string, | ||
autoComplete?: string; | ||
/** | ||
* `className` forwarded to the underlying `<input />`. | ||
*/ | ||
className?: 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; | ||
/** | ||
* 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; | ||
/** | ||
* Called when input is blurred | ||
* Signature: `(event) => void` | ||
*/ | ||
onBlur: PropTypes.func, | ||
onBlur?: FocusEventHandler; | ||
/** | ||
* Called when input is focused | ||
* Signature: `(event) => void` | ||
*/ | ||
onFocus: PropTypes.func, | ||
onFocus?: FocusEventHandler; | ||
/** | ||
* 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 if the input has invalid values | ||
*/ | ||
hasError: PropTypes.bool, | ||
hasWarning: PropTypes.bool, | ||
hasError?: boolean; | ||
hasWarning?: boolean; | ||
/** | ||
* Placeholder text for the input | ||
*/ | ||
placeholder: PropTypes.string, | ||
placeholder?: string; | ||
/** | ||
* 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'; | ||
}; | ||
|
||
TextInput.defaultProps = { | ||
const defaultProps: Pick<TTextInputProps, 'horizontalConstraint'> = { | ||
horizontalConstraint: 'scale', | ||
}; | ||
|
||
TextInput.isEmpty = (value) => !value || value.trim().length === 0; | ||
const TextInput = (props: TTextInputProps) => { | ||
const theme = useTheme(); | ||
if (!props.isReadOnly) { | ||
warning( | ||
Boolean(props.onChange), | ||
'TextInput: `onChange` is required when is not read only.' | ||
); | ||
} | ||
return ( | ||
<Constraints.Horizontal max={props.horizontalConstraint}> | ||
<input | ||
id={props.id} | ||
name={props.name} | ||
type="text" | ||
value={props.value} | ||
onChange={props.onChange} | ||
onBlur={props.onBlur} | ||
onFocus={props.onFocus} | ||
disabled={props.isDisabled} | ||
placeholder={props.placeholder} | ||
readOnly={props.isReadOnly} | ||
autoFocus={props.isAutofocussed} | ||
autoComplete={props.autoComplete} | ||
css={getInputStyles(props, theme)} | ||
// Allow to override the styles by passing a `className` prop. | ||
// Custom styles can also be passed using the `css` prop from emotion. | ||
// https://emotion.sh/docs/css-prop#style-precedence | ||
className={props.className} | ||
{...filterDataAttributes(props)} | ||
/* ARIA */ | ||
aria-readonly={props.isReadOnly} | ||
role="textbox" | ||
contentEditable={!props.isReadOnly} | ||
/> | ||
</Constraints.Horizontal> | ||
); | ||
}; | ||
|
||
TextInput.displayName = 'TextInput'; | ||
TextInput.defaultProps = defaultProps; | ||
TextInput.isEmpty = (value: TTextInputProps['value']) => | ||
!value || value.trim().length === 0; | ||
|
||
export default TextInput; |
File renamed without changes.
7d56aa9
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: