Skip to content

Commit

Permalink
feat: compact textfield and masked textfield
Browse files Browse the repository at this point in the history
  • Loading branch information
kostasdano committed Oct 31, 2023
1 parent 364a636 commit 4e06530
Show file tree
Hide file tree
Showing 18 changed files with 530 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/components/DatePicker/DatePicker.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type DatePickerProps = {
/** Calendar options for DateRangePicker */
options?: ExtraOption[];
/** Props of the TextField input */
inputProps?: TextFieldProps;
inputProps?: Omit<TextFieldProps, 'size'>;
/** The format of the date displayed in the input field */
dateFormatOverride?: DateFormatType;
/** if the datepicker can be clear with backspace */
Expand Down
8 changes: 6 additions & 2 deletions src/components/Label/Label.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { generateStylesFromTokens } from 'components/Typography/utils';
export const LABEL_TRANSFORM_LEFT_SPACING = rem(3);

export const labelStyle =
({ isAnimated, hasError }: Pick<LabelProps, 'isAnimated' | 'hasError'>) =>
({
isAnimated,
hasError,
size = 'normal',
}: Pick<LabelProps, 'isAnimated' | 'hasError' | 'size'>) =>
(theme: Theme): SerializedStyles => {
const tokens = getTextInputBaseTokens(theme);

Expand All @@ -34,6 +38,6 @@ export const labelStyle =
margin: auto;
white-space: nowrap;
${generateStylesFromTokens(tokens('normal.input'))}
${generateStylesFromTokens(tokens(`input.${size}` as const))}
`;
};
8 changes: 6 additions & 2 deletions src/components/Label/Label.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { ComponentSizes } from 'types';

import { labelStyle } from './Label.style';

Expand All @@ -11,19 +12,22 @@ export type LabelProps = {
isRequired: boolean;
/** If the label must be moved to the top */
isAnimated?: boolean;
/** HTML <label/>'s for prop */
htmlFor?: string;
size?: string;
/** The size of the label */
size?: ComponentSizes;
};

const Label: React.FCC<LabelProps> = ({
hasError = false,
htmlFor,
label,
size = 'normal',
isRequired = false,
isAnimated = false,
}) => {
return (
<label htmlFor={htmlFor} css={labelStyle({ isAnimated, hasError })}>
<label htmlFor={htmlFor} css={labelStyle({ isAnimated, hasError, size })}>
{label} {isRequired && '*'}
</label>
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/List/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Key } from 'react';
import { ComponentSizes } from 'types';

import { SelectOption } from 'components/Select';

Expand All @@ -7,6 +8,6 @@ export type ListSelected = 'all' | Iterable<Key>;

export type ListItemType = SelectOption;

export type ListRowSize = 'compact' | 'normal';
export type ListRowSize = ComponentSizes;

export type SelectHandlerType = (option: ListItemType) => void;
1 change: 0 additions & 1 deletion src/components/MultiTextFieldBase/MultiTextFieldBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ const MultiTextFieldBase = React.forwardRef<HTMLInputElement, Props & InputProps
/>
{label && (
<Label
size={'md'}
htmlFor={id}
label={label}
isRequired={isRequired}
Expand Down
1 change: 1 addition & 0 deletions src/components/NumberField/NumberField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type NumberFieldProps = Omit<
TextFieldProps,
| 'mask'
| 'maskChar'
| 'size'
| 'value'
| 'onChange'
| 'isMulti'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const buttonWrapperStyle =
return css`
background: transparent;
border: none;
height: calc(${tokens('container')} / 2);
height: calc(${tokens('container.normal')} / 2);
&:hover {
cursor: pointer;
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions src/components/SearchField/SearchField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getTextInputBaseTokens } from 'components/TextInputBase/TextInputBase.t
export type SearchFieldProps = {
/** A callback that's called when the user clicks the 'clear' icon */
onClear: () => void;
} & TextFieldProps &
} & Omit<TextFieldProps, 'size'> &
TestProps;

const SearchField = React.forwardRef<HTMLInputElement, SearchFieldProps>((props, ref) => {
Expand All @@ -28,7 +28,7 @@ const SearchField = React.forwardRef<HTMLInputElement, SearchFieldProps>((props,
const sx = {
wrapper: {
borderRadius: rem(100),
height: tokens('container'),
height: tokens('container.normal'),
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ const MultiselectTextField = React.forwardRef<HTMLInputElement, Props & InputPro
/>
{label && (
<Label
size={'md'}
htmlFor={id}
label={label}
isRequired={isRequired}
Expand Down
21 changes: 21 additions & 0 deletions src/components/TextField/TextField.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ such as icons and components, and a multi-input functionality with the help of t

## Variants

### TextField sizes

There are 2 Textfield sizes: `normal` and `compact`.

<Tip>
Compact TextField doesn't support placeholder (only in masked version using the mask prop) and
multi-textfield functionality. To use these, please use normal-size TextField.
</Tip>

<Preview>
<Story name="TextField sizes">
<Stack>
<TextField label={'Label'} />
</Stack>
<Stack>
<TextField label={'Label'} size="compact" />
</Stack>
</Story>
</Preview>

### TextField with Label and Placeholder

Regular TextField with label options. Label that will float, with or without Placeholder
Expand Down Expand Up @@ -201,6 +221,7 @@ MultiTextField enables a user to enter one or individual entries as free text.
<Stack>
<TextFieldShowCase
label={text('Label', 'Label')}
size={select('size', ['normal', 'compact'], 'normal')}
placeholder={text('Placeholder', 'Placeholder')}
isDisabled={boolean('isDisabled', false)}
hasSuffix={boolean('hasSuffixIcon', false)}
Expand Down
11 changes: 8 additions & 3 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import TextInputBase, { TextInputBaseProps } from 'components/TextInputBase';
import { inputStyle } from 'components/TextInputBase/TextInputBase.style';

export type InputProps = Partial<
Omit<InputHTMLAttributes<HTMLInputElement>, 'readOnly' | 'disabled'>
Omit<InputHTMLAttributes<HTMLInputElement>, 'readOnly' | 'disabled' | 'size'>
>;

type MaskProps = { mask?: never } | { mask?: string | (string | RegExp)[]; placeholder?: never };
Expand Down Expand Up @@ -53,6 +53,7 @@ const TextField = React.forwardRef<HTMLInputElement, TextFieldProps>((props, ref
suffix = null,
label,
placeholder = '',
size = 'normal',
isRequired = false,
isDisabled,
isReadOnly,
Expand All @@ -79,6 +80,7 @@ const TextField = React.forwardRef<HTMLInputElement, TextFieldProps>((props, ref
id,
suffix,
status,
size,
isDisabled,
ref,
sx,
Expand All @@ -87,8 +89,10 @@ const TextField = React.forwardRef<HTMLInputElement, TextFieldProps>((props, ref
const inputProps = {
// eslint-disable-next-line @typescript-eslint/naming-convention
readOnly: isLocked || isReadOnly,
css: inputStyle({ label, placeholder, isLocked, isDisabled }),
placeholder: placeholder ? `${placeholder} ${isRequired ? '*' : ''}` : label,
css: inputStyle({ label, placeholder, isLocked, isDisabled, size }),
...(size === 'normal'
? { placeholder: placeholder ? `${placeholder} ${isRequired ? '*' : ''}` : label }
: { placeholder: ' ' }),
// eslint-disable-next-line @typescript-eslint/naming-convention
required: isRequired,
id: id,
Expand Down Expand Up @@ -139,6 +143,7 @@ const TextField = React.forwardRef<HTMLInputElement, TextFieldProps>((props, ref
<Label
htmlFor={id}
label={label}
size={size}
isRequired={isRequired}
isAnimated={Boolean(rest.value)}
hasError={!isDisabled && status?.type === 'error'}
Expand Down
Loading

0 comments on commit 4e06530

Please sign in to comment.