Skip to content

Commit

Permalink
chore: add forwardref to input component (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddeottens authored Mar 11, 2024
1 parent b2d817b commit d8b1a10
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 155 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "13.7.1",
"version": "13.7.2",
"license": "MIT",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/components/form-row/FormRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const FormRow = <T extends FieldValues, K extends string>({
<Block width={'100%'}>
{label && (
<Block marginBottom={scale100}>
<LabelSmall display="flex" as="label" alignItems="center" gridGap={scale200} for={name as string}>
<LabelSmall display="flex" as="label" alignItems="center" gridGap={scale200} htmlFor={name as string}>
{getLabel()}
{toolTip && (
<StatefulTooltip
Expand Down
302 changes: 152 additions & 150 deletions src/components/input/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { forwardRef } from 'react';
import { Input as BaseInput, InputOverrides } from 'baseui/input';
import { useTheme } from '../../providers';
import {
Expand All @@ -15,110 +15,92 @@ import { DATA_TEST_ID } from '../../models';
import { Align, InputProps } from './types';
import { Skeleton } from '../skeleton';

export const Input = ({
value,
testId,
type,
uppercase,
noBorder = false,
success = false,
disabled,
width = '100%',
startEnhancer = null,
endEnhancer = null,
overrides,
showSkeleton = false,
autoComplete = 'off',
name,
align = Align.left,
...rest
}: InputProps) => {
const {
theme: {
current: {
sizing: { scale500, scale550, scale600 },
customSizing: { scale975 },
borders,
colors,
customColors,
export const Input = forwardRef(
({
value,
testId,
type,
uppercase,
noBorder = false,
success = false,
disabled,
width = '100%',
startEnhancer = null,
endEnhancer = null,
overrides,
showSkeleton = false,
autoComplete = 'off',
name,
align = Align.left,
...rest
}: InputProps) => {
const {
theme: {
current: {
sizing: { scale500, scale550, scale600 },
customSizing: { scale975 },
borders,
colors,
customColors,
},
},
},
} = useTheme();
const { border300 } = borders;
const { primaryB } = colors;
} = useTheme();
const { border300 } = borders;
const { primaryB } = colors;

// Password input requires the default padding
const rootPadding = () => {
if (type !== 'password') {
return { ...padding() };
}
};
const {
Root: RootOverrides,
StartEnhancer: StartEnhancerOverrides,
EndEnhancer: EndEnhancerOverrides,
MaskToggleButton: MaskToggleButtonOverrides,
Input: InputOverrides,
InputContainer: InputContainerOverrides,
...styleOverrides
} = overrides ?? {};
// Password input requires the default padding
const rootPadding = () => {
if (type !== 'password') {
return { ...padding() };
}
};
const {
Root: RootOverrides,
StartEnhancer: StartEnhancerOverrides,
EndEnhancer: EndEnhancerOverrides,
MaskToggleButton: MaskToggleButtonOverrides,
Input: InputOverrides,
InputContainer: InputContainerOverrides,
...styleOverrides
} = overrides ?? {};

const baseOverrides: InputOverrides = {
Input: {
style: (opts) => {
const { $disabled, $isFocused, $theme } = opts;
const backgroundColor = getInputBackgroundColor({ disabled: $disabled, customColors, colors });
const color = getInputTextColor({ isFocused: $isFocused, hasValue: !!value, customColors, colors });
return {
backgroundColor,
...border(),
...padding('0', endEnhancer ? scale500 : scale600, '0', startEnhancer ? scale500 : scale600),
textTransform: uppercase ? 'uppercase' : 'inherit',
textAlign: align,
color,
'::placeholder': {
color: customColors.dark4,
},
fontSize: $theme.typography.LabelSmall.fontSize,
...getStyleOverrides(opts, InputOverrides?.style),
};
},
props: {
[DATA_TEST_ID]: testId,
const baseOverrides: InputOverrides = {
Input: {
style: (opts) => {
const { $disabled, $isFocused, $theme } = opts;
const backgroundColor = getInputBackgroundColor({ disabled: $disabled, customColors, colors });
const color = getInputTextColor({ isFocused: $isFocused, hasValue: !!value, customColors, colors });
return {
backgroundColor,
...border(),
...padding('0', endEnhancer ? scale500 : scale600, '0', startEnhancer ? scale500 : scale600),
textTransform: uppercase ? 'uppercase' : 'inherit',
textAlign: align,
color,
'::placeholder': {
color: customColors.dark4,
},
fontSize: $theme.typography.LabelSmall.fontSize,
...getStyleOverrides(opts, InputOverrides?.style),
};
},
props: {
[DATA_TEST_ID]: testId,
},
},
},
InputContainer: {
style: (opts) => ({
...border(),
backgroundColor: primaryB,
...getStyleOverrides(opts, InputContainerOverrides?.style),
}),
},
Root: {
style: (opts) => {
const { $error, $isFocused } = opts;
return {
height: scale975,
width,
...border(
!noBorder
? {
...border300,
borderColor: getInputBorderColor({
error: $error,
success,
isFocused: $isFocused,
customColors,
colors,
}),
}
: undefined,
),
...borderRadius(borders.radius200),
InputContainer: {
style: (opts) => ({
...border(),
backgroundColor: primaryB,
...margin('0'),
...rootPadding(),
':hover': {
...getStyleOverrides(opts, InputContainerOverrides?.style),
}),
},
Root: {
style: (opts) => {
const { $error, $isFocused } = opts;
return {
height: scale975,
width,
...border(
!noBorder
? {
Expand All @@ -129,62 +111,82 @@ export const Input = ({
isFocused: $isFocused,
customColors,
colors,
hover: true,
disabled,
}),
}
: undefined,
),
},
...getStyleOverrides(opts, RootOverrides?.style),
};
...borderRadius(borders.radius200),
backgroundColor: primaryB,
...margin('0'),
...rootPadding(),
':hover': {
...border(
!noBorder
? {
...border300,
borderColor: getInputBorderColor({
error: $error,
success,
isFocused: $isFocused,
customColors,
colors,
hover: true,
disabled,
}),
}
: undefined,
),
},
...getStyleOverrides(opts, RootOverrides?.style),
};
},
},
},
StartEnhancer: {
style: (opts) => {
const { $disabled } = opts;
return {
backgroundColor: getInputBackgroundColor({ disabled: $disabled, customColors, colors }),
...padding('0', '0', '0', scale600),
...getStyleOverrides(opts, StartEnhancerOverrides?.style),
};
StartEnhancer: {
style: (opts) => {
const { $disabled } = opts;
return {
backgroundColor: getInputBackgroundColor({ disabled: $disabled, customColors, colors }),
...padding('0', '0', '0', scale600),
...getStyleOverrides(opts, StartEnhancerOverrides?.style),
};
},
},
},
EndEnhancer: {
style: (opts) => {
const { $disabled } = opts;
return {
backgroundColor: getInputBackgroundColor({ disabled: $disabled, customColors, colors }),
...padding('0', scale600, '0', '0'),
...getStyleOverrides(opts, EndEnhancerOverrides?.style),
};
EndEnhancer: {
style: (opts) => {
const { $disabled } = opts;
return {
backgroundColor: getInputBackgroundColor({ disabled: $disabled, customColors, colors }),
...padding('0', scale600, '0', '0'),
...getStyleOverrides(opts, EndEnhancerOverrides?.style),
};
},
},
},
MaskToggleButton: {
style: (opts) => {
return {
...padding('0', '0', '0', scale550),
outline: 'none',
...getStyleOverrides(opts, MaskToggleButtonOverrides?.style),
};
MaskToggleButton: {
style: (opts) => {
return {
...padding('0', '0', '0', scale550),
outline: 'none',
...getStyleOverrides(opts, MaskToggleButtonOverrides?.style),
};
},
},
},
...styleOverrides,
};
...styleOverrides,
};

return showSkeleton ? (
<Skeleton animation height={scale975} width={width} />
) : (
<BaseInput
value={value}
disabled={disabled}
startEnhancer={startEnhancer}
endEnhancer={endEnhancer}
overrides={baseOverrides}
type={type}
autoComplete={autoComplete}
id={name}
{...rest}
/>
);
};
return showSkeleton ? (
<Skeleton animation height={scale975} width={width} />
) : (
<BaseInput
value={value}
disabled={disabled}
startEnhancer={startEnhancer}
endEnhancer={endEnhancer}
overrides={baseOverrides}
type={type}
autoComplete={autoComplete}
id={name}
{...rest}
/>
);
},
);
2 changes: 1 addition & 1 deletion src/components/typography/label-small/LabelSmall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { forwardRef } from 'react';
import { LabelSmall as BaseLabelSmall } from 'baseui/typography';
import { BlockProps } from 'baseui/block';

export const LabelSmall = forwardRef<HTMLButtonElement, BlockProps & { for?: string }>(
export const LabelSmall = forwardRef<HTMLButtonElement, BlockProps & { htmlFor?: string }>(
({ children, marginTop = '0', marginBottom = '0', ...rest }: BlockProps, ref) => (
<BaseLabelSmall ref={ref} marginTop={marginTop} marginBottom={marginBottom} {...rest}>
{children}
Expand Down

0 comments on commit d8b1a10

Please sign in to comment.