Skip to content

Commit

Permalink
feat(fields): add unit prop to NumberInput
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Sep 25, 2024
1 parent d129438 commit 5773587
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 28 deletions.
112 changes: 85 additions & 27 deletions src/fields/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type NumberInputProps = Omit<InputProps, 'as' | 'defaultValue' | 'id' | '
name: string
onChange?: (nextValue: number | undefined) => Promisable<void>
readOnly?: boolean | undefined
unit?: string | undefined
value?: number | undefined
}
export function NumberInput({
Expand All @@ -53,6 +54,7 @@ export function NumberInput({
onFocus,
readOnly = false,
style,
unit,
value,
...originalProps
}: NumberInputProps) {
Expand Down Expand Up @@ -105,44 +107,55 @@ export function NumberInput({

useFieldUndefineEffect(isUndefinedWhenDisabled && !!disabled, onChange)

const commonInputProps = {
$areArrowsHidden: areArrowsHidden,
$isDisabled: disabled,
$isLight: isLight,
$isReadOnly: readOnly,
$isTransparent: isTransparent,
disabled,
id: name,
onBlur: handleBlur,
onChange: handleChange,
onFocus: handleFocus,
readOnly,
ref: inputRef,
type: 'number',
value: value ?? '',
...originalProps
}

return (
<Field className={controlledClassname} style={style}>
<Label $idDisabled={disabled} $isHidden={isLabelHidden} $isRequired={isRequired} htmlFor={name}>
{label}
</Label>

<StyledInput
key={key}
ref={inputRef}
$areArrowsHidden={areArrowsHidden}
$hasError={hasError}
$isDisabled={disabled}
$isLight={isLight}
$isReadOnly={readOnly}
$isTransparent={isTransparent}
disabled={disabled}
id={name}
onBlur={handleBlur}
onChange={handleChange}
onFocus={handleFocus}
readOnly={readOnly}
type="number"
value={value ?? ''}
{...originalProps}
/>
{!unit && <InputWithoutUnit key={key} {...commonInputProps} />}
{!!unit && (
<InputBoxWithUnit
key={key}
$hasError={hasError}
$isDisabled={disabled}
$isLight={isLight}
$isReadOnly={readOnly}
$isTransparent={isTransparent}
>
<InputWithUnit {...commonInputProps} />
{unit}
</InputBoxWithUnit>
)}

{!isErrorMessageHidden && hasError && <FieldError>{controlledError}</FieldError>}
</Field>
)
}

const StyledInput = styled(Input as any)<
const BaseInput = styled(Input)<
CommonFieldStyleProps & {
$areArrowsHidden: boolean
}
>`
background-color: ${getFieldBackgroundColorFactory()};
border: solid 1px ${getFieldBorderColorFactoryForState('default')};
border-radius: 0;
color: ${p => p.theme.color.gunMetal};
${p => p.$isReadOnly && `cursor: default;`}
Expand All @@ -169,22 +182,67 @@ const StyledInput = styled(Input as any)<
}
&:hover {
background-color: ${getFieldBackgroundColorFactory()};
border: solid 1px ${getFieldBorderColorFactoryForState('hover')} !important;
&::placeholder {
color: ${getFieldPlaceholderColorFactoryForState('hover')};
}
}
&:active,
&:focus {
background-color: ${getFieldBackgroundColorFactory()};
border: solid 1px ${getFieldBorderColorFactoryForState('focus')} !important;
outline: 0;
&::placeholder {
color: ${getFieldPlaceholderColorFactoryForState('focus')};
}
}
`

const InputWithoutUnit = styled(BaseInput)<
CommonFieldStyleProps & {
$areArrowsHidden: boolean
}
>`
background-color: ${getFieldBackgroundColorFactory()};
border: solid 1px ${getFieldBorderColorFactoryForState('default')};
&:hover {
background-color: ${getFieldBackgroundColorFactory()};
border: solid 1px ${getFieldBorderColorFactoryForState('hover')} !important;
}
&:active,
&:focus {
background-color: ${getFieldBackgroundColorFactory()};
border: solid 1px ${getFieldBorderColorFactoryForState('focus')} !important;
}
`

const InputBoxWithUnit = styled.div<CommonFieldStyleProps>`
align-items: center;
background-color: ${getFieldBackgroundColorFactory()};
border: solid 1px ${getFieldBorderColorFactoryForState('default')};
color: ${p => p.theme.color.slateGray};
display: flex;
font-size: 13px;
line-height: 19px;
padding-right: 8px;
user-select: none;
width: 100%;
&:hover {
background-color: ${getFieldBackgroundColorFactory()};
border: solid 1px ${getFieldBorderColorFactoryForState('hover')} !important;
}
&:active,
&:focus {
background-color: ${getFieldBackgroundColorFactory()};
border: solid 1px ${getFieldBorderColorFactoryForState('focus')} !important;
}
`

const InputWithUnit = styled(BaseInput)`
background-color: transparent;
border: none;
flex-grow: 1;
`
4 changes: 3 additions & 1 deletion stories/fields/NumberInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const meta: Meta<NumberInputProps> = {
isUndefinedWhenDisabled: ARG_TYPE.OPTIONAL_BOOLEAN,
onChange: ARG_TYPE.NO_CONTROL_INPUT,
readOnly: ARG_TYPE.OPTIONAL_BOOLEAN,
unit: ARG_TYPE.OPTIONAL_STRING,
value: ARG_TYPE.OPTIONAL_NUMBER
},

Expand All @@ -41,7 +42,8 @@ const meta: Meta<NumberInputProps> = {
label: 'A number input',
name: 'myNumberInput',
placeholder: 'Pick a number',
readOnly: false
readOnly: false,
unit: ''
},

decorators: [
Expand Down

0 comments on commit 5773587

Please sign in to comment.