Skip to content

Commit

Permalink
feat(pie-input): DSW-1586 add size property to input component (#1354)
Browse files Browse the repository at this point in the history
* feat(pie-input): DSW-1586 add size property to input component

* feat(pie-storybook): DSW-1586 add storybook support for size prop
  • Loading branch information
jamieomaguire authored Mar 26, 2024
1 parent 6f4c27c commit 31a1b0a
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-pears-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pie-storybook": minor
---

[Added] - Support for the new `size` prop in `pie-input`
5 changes: 5 additions & 0 deletions .changeset/new-mayflies-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@justeattakeaway/pie-input": minor
---

[Added] - `size` property to input component
10 changes: 9 additions & 1 deletion apps/pie-storybook/stories/pie-input.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useArgs as UseArgs } from '@storybook/preview-api';
/* eslint-disable import/no-duplicates */
import '@justeattakeaway/pie-input';
import {
types, inputModes, statusTypes, InputProps as InputPropsBase,
types, inputModes, statusTypes, InputProps as InputPropsBase, sizes,
} from '@justeattakeaway/pie-input';
/* eslint-enable import/no-duplicates */

Expand Down Expand Up @@ -34,6 +34,7 @@ const defaultArgs: InputProps = {
leadingSlot: 'None',
trailingSlot: 'None',
assistiveText: '',
size: 'medium',
};

const slotOptions = ['Icon (Placeholder)', 'Short text (#)', 'None'] as const;
Expand Down Expand Up @@ -188,6 +189,11 @@ const inputStoryMeta: InputStoryMeta = {
summary: undefined,
},
},
size: {
description: 'The size of the input field. Can be `small`, `medium`, or `large`. Defaults to `medium`.',
control: 'select',
options: sizes,
},
},
args: defaultArgs,
parameters: {
Expand Down Expand Up @@ -219,6 +225,7 @@ const Template = ({
assistiveText,
status,
step,
size,
}: InputProps) => {
const [, updateArgs] = UseArgs();

Expand Down Expand Up @@ -271,6 +278,7 @@ const Template = ({
?readonly="${readonly}"
assistiveText="${ifDefined(assistiveText)}"
status=${ifDefined(status)}
size="${ifDefined(size)}"
@input="${onInput}"
@change="${onChange}">
${renderLeadingOrTrailingSlot('leading', leadingSlot)}
Expand Down
1 change: 1 addition & 0 deletions packages/components/pie-input/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import { PieInput } from '@justeattakeaway/pie-input/dist/react';
| `step` | `number` | - | An optional amount that value should be incremented or decremented by when using the up and down arrows in the input. Only applies when type is `number`. |
| `min` | `number` | - | The minimum value of the input. Only applies when type is `number`. If the value provided is lower, the input is invalid. |
| `max` | `number` | - | The maximum value of the input. Only applies when type is `number`. If the value provided is higher, the input is invalid. |
| `size` | `'small'`, `'medium'`, `'large'` | `medium` | The size of the input field. Can be `small`, `medium`, or `large`. Defaults to `medium`. |


In your markup or JSX, you can then use these to set the properties for the `pie-input` component:
Expand Down
10 changes: 9 additions & 1 deletion packages/components/pie-input/src/defs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const types = ['text', 'number', 'password', 'url', 'email', 'tel'] as const;
export const inputModes = ['none', 'text', 'tel', 'url', 'email', 'numeric', 'decimal', 'search'] as const;
export const statusTypes = ['success', 'error'] as const;
export const sizes = ['small', 'medium', 'large'] as const;

export interface InputProps {
/**
* The type of HTML input to render.
Expand Down Expand Up @@ -96,6 +98,11 @@ export interface InputProps {
* The maximum value of the input. Only applies when type is `number`. If the value provided is higher, the input is invalid.
*/
max?: number;

/**
* The size of the input field. Can be `small`, `medium`, or `large`. Defaults to `medium`.
*/
size?: typeof sizes[number];
}

// TODO - There is a ticket to add default prop values to our existing components. This might be replaced by the code added in that ticket.
Expand All @@ -107,12 +114,13 @@ type SubsetRequiredProperties<T, K extends keyof T> = Required<Pick<T, K>>;
/**
* The default values for the `InputProps` that are required (i.e. they have a fallback value in the component).
*/
type DefaultInputPropValues = SubsetRequiredProperties<InputProps, 'type' | 'value'>;
type DefaultInputPropValues = SubsetRequiredProperties<InputProps, 'type' | 'value' | 'size'>;

/**
* Default values for optional properties that have default fallback values in the component.
*/
export const InputDefaultPropertyValues: DefaultInputPropValues = {
type: 'text',
value: '',
size: 'medium',
};
6 changes: 5 additions & 1 deletion packages/components/pie-input/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export class PieInput extends FormControlMixin(RtlMixin(LitElement)) implements
@property({ type: Number })
public max?: InputProps['max'];

@property({ type: String })
public size?: InputProps['size'] = InputDefaultPropertyValues.size;

@query('input')
private input?: HTMLInputElement;

Expand Down Expand Up @@ -173,10 +176,11 @@ export class PieInput extends FormControlMixin(RtlMixin(LitElement)) implements
status,
type,
value,
size,
} = this;

return html`
<div>
<div data-test-id="pie-input-shell" size=${ifDefined(size)}>
<slot name="leading"></slot>
<input
type=${ifDefined(type)}
Expand Down
29 changes: 29 additions & 0 deletions packages/components/pie-input/test/component/pie-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { statusTypes } from '../../src/defs.ts';

const componentSelector = '[data-test-id="pie-input"]';
const assistiveTextSelector = '[data-test-id="pie-input-assistive-text"]';
const componentShellSelector = '[data-test-id="pie-input-shell"]';

/**
* Sets up form data extraction for testing form submissions. This function expects a form element
Expand Down Expand Up @@ -767,6 +768,34 @@ test.describe('PieInput - Component tests', () => {
expect(isValid).toBe(true);
});
});

test.describe('size', () => {
test('should default to medium size if no size prop provided', async ({ mount }) => {
// Arrange
const component = await mount(PieInput, {});

// Act
const inputShell = component.locator(componentShellSelector);

// Assert
expect(inputShell).toHaveAttribute('size', 'medium');
});

test('should apply the size prop to the HTML input rendered', async ({ mount }) => {
// Arrange
const component = await mount(PieInput, {
props: {
size: 'large',
} as InputProps,
});

// Act
const inputShell = component.locator(componentShellSelector);

// Assert
expect(inputShell).toHaveAttribute('size', 'large');
});
});
});

test.describe('Events', () => {
Expand Down

0 comments on commit 31a1b0a

Please sign in to comment.