Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextInput: refresh adornment API #770

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions devbox/apps/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ function App() {
<form>
<TextInput placeholder="Enter something" pattern="[A-Za-z]+" />
</form>
<TextInput
css="width: 80px"
adornment={{
end: 'ETH',
endWidth: 55,
endPadding: 8,
}}
/>
<TextInput
css="width: 80px"
adornment={{
start: '$',
startWidth: 36,
startPadding: 8,
}}
/>
<TextInput />
<TextInput adornment={{ start: <IconBlank /> }} />
<TextInput adornment={{ end: <IconBlank /> }} />
<TextInput
css="width: 80px"
adornment="ETH"
Copy link
Contributor

Choose a reason for hiding this comment

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

For these older examples, perhaps we can either remove them or update them to use the new API?

Expand Down
40 changes: 20 additions & 20 deletions src/components/Input/SearchInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ const SearchInput = React.forwardRef(({ onChange, ...props }, ref) => {
return (
<TextInput
ref={_ref}
adornment={
(props.value || '') === EMPTY ? (
<IconSearch
css={`
color: ${theme.surfaceIcon};
`}
/>
) : (
<ButtonIcon
onClick={handleClearClick}
label="Clear search input"
css={`
color: ${theme.surfaceIcon};
`}
>
<IconCross />
</ButtonIcon>
)
}
adornmentPosition="end"
adornment={{
end:
(props.value || '') === EMPTY ? (
<IconSearch
css={`
color: ${theme.surfaceIcon};
`}
/>
) : (
<ButtonIcon
onClick={handleClearClick}
label="Clear search input"
css={`
color: ${theme.surfaceIcon};
`}
>
<IconCross />
</ButtonIcon>
),
}}
onChange={handleChange}
{...props}
/>
Expand Down
134 changes: 97 additions & 37 deletions src/components/Input/TextInput.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback } from 'react'
import PropTypes from 'prop-types'
import { useTheme } from '../../theme'
import { warnOnce } from '../../utils'
import { warn, warnOnce } from '../../utils'
import { textStyle, GU, RADIUS } from '../../style'

// Simple text input
Expand Down Expand Up @@ -84,24 +84,80 @@ TextInput.defaultProps = {
type: 'text',
}

const Adornment = React.memo(({ adornment, position, padding }) => {
const theme = useTheme()
return (
<div
css={`
position: absolute;
top: 0;
bottom: 0;
height: 100%;
${position === 'end' ? 'right' : 'left'}: ${padding}px;
Evalir marked this conversation as resolved.
Show resolved Hide resolved
display: flex;
align-items: center;
justify-content: center;
color: ${theme.surfaceContentSecondary};
`}
>
{adornment}
</div>
)
})

Adornment.propTypes = {
adornment: PropTypes.node,
position: PropTypes.oneOf(['start', 'end']),
padding: PropTypes.number,
}

Adornment.defaultProps = {
padding: 4,
}

// Text input wrapped to allow adornments
const WrapperTextInput = React.forwardRef(
(
{
adornment,
adornmentPosition,
adornmentSettings: {
width: adornmentWidth = 36,
padding: adornmentPadding = 4,
},
...props
},
ref
) => {
const theme = useTheme()
({ adornment, adornmentPosition, adornmentSettings, ...props }, ref) => {
if (adornmentPosition) {
warn(
'TextInput: The "adornmentPosition" prop is deprecated. Please use the "adornment" prop instead.'
)
}
if (adornmentSettings) {
warn(
'TextInput: The "adornmentSettings" props is deprecated. Please use the "adornment" prop instead.'
)
}

if (!adornment) {
return <TextInput ref={ref} {...props} />
}

let adornmentConfig = adornment

const usingDeprecatedAPI =
React.isValidElement(adornment) ||
typeof adornment === 'string' ||
(typeof adornment === 'object' && adornment.constructor === Array)

if (usingDeprecatedAPI) {
const { adornmentPosition = 'start', adornmentSettings = {} } = props
adornmentConfig = {
[adornmentPosition]: adornment,
[`${adornmentPosition}Padding`]: adornmentSettings.padding,
[`${adornmentPosition}Width`]: adornmentSettings.width,
}
}

const {
start,
startPadding,
startWidth = 36,
end,
endPadding,
endWidth = 36,
} = adornmentConfig

return (
<div
css={`
Expand All @@ -113,37 +169,43 @@ const WrapperTextInput = React.forwardRef(
<TextInput
ref={ref}
css={`
${adornmentPosition === 'end'
? 'padding-right'
: 'padding-left'}: ${adornmentWidth - adornmentPadding * 2}px;
${start && `padding-left: ${startWidth}`}
${end && `padding-right: ${endWidth}`}
`}
{...props}
/>
<div
css={`
position: absolute;
top: 0;
bottom: 0;
height: 100%;
${adornmentPosition === 'end'
? 'right'
: 'left'}: ${adornmentPadding}px;
display: flex;
align-items: center;
justify-content: center;
color: ${theme.surfaceContentSecondary};
`}
>
{adornment}
</div>
{start && (
<Adornment
adornment={start}
padding={startPadding}
position="start"
/>
)}
{end && (
<Adornment adornment={end} padding={endPadding} position="end" />
)}
</div>
)
}
)

WrapperTextInput.propTypes = {
...TextInput.propTypes,
adornment: PropTypes.node,
adornment: PropTypes.oneOf([
PropTypes.shape({
start: PropTypes.node,
startWidth: PropTypes.number,
startPadding: PropTypes.number,
end: PropTypes.node,
endWidth: PropTypes.number,
endPadding: PropTypes.number,
}),

// deprecated
PropTypes.node,
]),

// deprecated
adornmentPosition: PropTypes.oneOf(['start', 'end']),
adornmentSettings: PropTypes.shape({
width: PropTypes.number,
Evalir marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -154,8 +216,6 @@ WrapperTextInput.propTypes = {
WrapperTextInput.defaultProps = {
...TextInput.defaultProps,
adornment: null,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure, but should we add null to the list of allowed types for adornment? Not sure how PropTypes.oneOf() handles this, but I could see it erroring.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sadly, React doesn't seem to have null as an available propType. :(

Copy link
Contributor

Choose a reason for hiding this comment

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

Prop types interpret null and undefined as the same thing, so it shouldn’t be a problem as long as isRequired is not used. 👍

To combine a value with a type, the trick is to use PropType.oneOf() with only the value, making it a standard PropType type:

PropTypes.oneOfType([
  PropTypes.oneOf([null]),
  PropTypes.number,
])

This is what we are doing for the aragonUI’s _null prop type.

adornmentPosition: 'start',
adornmentSettings: {},
}

// <input type=number> (only for compat)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we also update this component's documentation with the new changes? We can remove (or mark) the deprecated props :).

Expand Down
35 changes: 17 additions & 18 deletions src/components/TextCopy/TextCopy.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,24 @@ const TextCopy = React.memo(
)}
<TextInput
ref={inputRef}
adornment={
<ButtonIcon
onClick={handleCopy}
label="Copy"
css={`
width: ${HEIGHT - 2}px;
height: ${HEIGHT - 2}px;
border-radius: 0;
color: ${theme.surfaceIcon};
`}
>
<IconCopy />
</ButtonIcon>
}
adornmentPosition="end"
adornmentSettings={{
adornment={{
end: (
<ButtonIcon
onClick={handleCopy}
label="Copy"
css={`
width: ${HEIGHT - 2}px;
height: ${HEIGHT - 2}px;
border-radius: 0;
color: ${theme.surfaceIcon};
`}
>
<IconCopy />
</ButtonIcon>
),
// Keep the button square
width: HEIGHT - 2,
padding: 0,
endWidth: HEIGHT - 2,
endPadding: 0,
}}
autofocus={autofocus}
onFocus={handleFocus}
Expand Down