Skip to content

Commit

Permalink
feat(Textfield): add shorthand sizes (#2018)
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsnes authored May 16, 2024
1 parent c511c59 commit 9c52938
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 21 deletions.
18 changes: 9 additions & 9 deletions packages/css/textfield.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,39 @@
border-bottom-right-radius: var(--fds-border_radius-medium);
}

.fds-textfield--small .fds-textfield__adornment {
.fds-textfield--sm .fds-textfield__adornment {
padding: var(--fds-sizing-2) var(--fds-spacing-3);
}

.fds-textfield--medium .fds-textfield__adornment {
.fds-textfield--md .fds-textfield__adornment {
padding: 0.65rem var(--fds-spacing-4);
}

.fds-textfield--large .fds-textfield__adornment {
.fds-textfield--lg .fds-textfield__adornment {
padding: 0.85rem var(--fds-spacing-5);
}

.fds-textfield--small .fds-textfield__field {
.fds-textfield--sm .fds-textfield__field {
height: var(--fds-sizing-10);
}

.fds-textfield--medium .fds-textfield__field {
.fds-textfield--md .fds-textfield__field {
height: var(--fds-sizing-12);
}

.fds-textfield--large .fds-textfield__field {
.fds-textfield--lg .fds-textfield__field {
height: var(--fds-sizing-14);
}

.fds-textfield--small .fds-textfield__input {
.fds-textfield--sm .fds-textfield__input {
padding: 0 var(--fds-spacing-2);
}

.fds-textfield--medium .fds-textfield__input {
.fds-textfield--md .fds-textfield__input {
padding: 0 var(--fds-spacing-3);
}

.fds-textfield--large .fds-textfield__input {
.fds-textfield--lg .fds-textfield__input {
padding: 0 var(--fds-spacing-4);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/components/form/CharacterCounter.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ErrorMessage } from '../Typography';

import type { TextfieldProps } from './Textfield';

export type CharacterLimitProps = Omit<
CharacterCounterProps,
'id' | 'value' | 'size'
Expand All @@ -17,7 +19,7 @@ type CharacterCounterProps = {
/** The ID of the element that describes the maximum character limit for accessibility purposes. */
id: string;
/** Text size */
size?: 'small' | 'medium' | 'large';
size?: TextfieldProps['size'];
};

const defaultLabel: CharacterCounterProps['label'] = (count) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const Preview: Story = {
label: 'Label',
disabled: false,
readOnly: false,
size: 'medium',
size: 'md',
description: '',
error: '',
},
Expand Down Expand Up @@ -53,17 +53,17 @@ export const Adornments: StoryFn<typeof Textfield> = () => (
<Textfield
prefix='prefix'
suffix='suffix'
size='small'
size='sm'
/>
<Textfield
prefix='prefix'
suffix='suffix'
size='medium'
size='md'
/>
<Textfield
prefix='prefix'
suffix='suffix'
size='large'
size='lg'
/>
</div>
);
Expand Down
12 changes: 9 additions & 3 deletions packages/react/src/components/form/Textfield/Textfield.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import { CharacterCounter } from '../CharacterCounter';

import { useTextfield } from './useTextfield';

type OldTextfieldSizes = 'small' | 'medium' | 'large';

export type TextfieldProps = {
/** Label */
label?: ReactNode;
/** Visually hides `label` and `description` (still available for screen readers) */
hideLabel?: boolean;
/** Changes field size and paddings */
size?: 'small' | 'medium' | 'large';
/**
* Changes field size and paddings
* @default md
* @note `small`, `medium`, `large` is deprecated
*/
size?: 'sm' | 'md' | 'lg' | OldTextfieldSizes;
/** Prefix for field. */
prefix?: string;
/** Suffix for field. */
Expand Down Expand Up @@ -82,7 +88,7 @@ export const Textfield = forwardRef<HTMLInputElement, TextfieldProps>(
descriptionId,
hasError,
errorId,
size = 'medium',
size = 'md',
readOnly,
} = useTextfield(props);

Expand Down
7 changes: 6 additions & 1 deletion packages/react/src/components/form/Textfield/useTextfield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useContext } from 'react';
import type { FormField } from '../useFormField';
import { useFormField } from '../useFormField';
import { FieldsetContext } from '../Fieldset/FieldsetContext';
import { getSize } from '../../../utilities/getSize';

import type { TextfieldProps } from './Textfield';

Expand All @@ -18,10 +19,14 @@ export const useTextfield: UseTextfield = (props) => {
const fieldset = useContext(FieldsetContext);
const { inputProps, readOnly, ...rest } = useFormField(props, 'textfield');

const size = getSize(fieldset?.size ?? props.size ?? 'md') as NonNullable<
TextfieldProps['size']
>;

return {
...rest,
readOnly,
size: fieldset?.size ?? props.size,
size,
inputProps: {
...inputProps,
readOnly,
Expand Down
18 changes: 15 additions & 3 deletions packages/react/src/components/form/useFormField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { useContext, useId } from 'react';
import type { HTMLAttributes, InputHTMLAttributes, ReactNode } from 'react';
import cl from 'clsx';

import { getSize } from '../../utilities/getSize';

import { FieldsetContext } from './Fieldset/FieldsetContext';

type OldFormFielSize = 'small' | 'medium' | 'large';

export type FormFieldProps = {
/** Error message for form field */
error?: ReactNode;
Expand All @@ -19,8 +23,12 @@ export type FormFieldProps = {
id?: string;
/** Toggle `readOnly` */
readOnly?: boolean;
/** Changes field size and paddings */
size?: 'small' | 'medium' | 'large';
/**
* Changes field size and paddings
* @default md
* @note `small`, `medium`, `large` is deprecated
*/
size?: 'sm' | 'md' | 'lg' | OldFormFielSize;
} & Pick<HTMLAttributes<HTMLElement>, 'aria-describedby'>;

export type FormField = {
Expand Down Expand Up @@ -56,12 +64,16 @@ export const useFormField = (

const hasError = !disabled && !readOnly && !!(props.error || fieldset?.error);

const size = getSize(props.size || fieldset?.size || 'md') as NonNullable<
FormFieldProps['size']
>;

return {
readOnly,
hasError,
errorId,
descriptionId,
size: props?.size ?? fieldset?.size ?? 'medium',
size,
inputProps: {
id,
disabled,
Expand Down

0 comments on commit 9c52938

Please sign in to comment.