agroptima-design-system 1.2.31 → 1.2.32-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"sessionId":"f3e7999c-f3bc-4b21-918e-276243976569","pid":533964,"procStart":"130173790","acquiredAt":1785859988086}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agroptima-design-system",
3
- "version": "1.2.31",
3
+ "version": "1.2.32-beta.0",
4
4
  "scripts": {
5
5
  "dev": "npm run storybook",
6
6
  "storybook": "storybook dev -p 6006 --ci",
@@ -17,7 +17,7 @@ export interface InputProps extends React.ComponentPropsWithRef<'input'> {
17
17
  helpText?: string
18
18
  variant?: InputVariant
19
19
  id?: string
20
- suffix?: string
20
+ suffix?: React.ReactNode
21
21
  errors?: string[]
22
22
  rightIcon?: IconType
23
23
  fullWidth?: boolean
@@ -0,0 +1,83 @@
1
+ @use '../../settings/typography/form' as typography;
2
+ @use '../../settings/config';
3
+ @use '../../settings/depth';
4
+ @use '../../settings/mixins';
5
+
6
+ .select-suffix {
7
+ position: relative;
8
+ display: flex;
9
+ align-self: stretch;
10
+ margin: (config.$space-2x * -1) (config.$space-3x * -1) (config.$space-2x * -1)
11
+ 0;
12
+ }
13
+
14
+ .select-suffix-trigger {
15
+ @include typography.select-text;
16
+ display: flex;
17
+ align-items: center;
18
+ gap: config.$space-1x;
19
+ height: 100%;
20
+ padding: config.$space-2x config.$space-3x;
21
+ border: none;
22
+ border-left: 1px solid var(--neutral-color-600);
23
+ border-top-right-radius: config.$corner-radius-m;
24
+ border-bottom-right-radius: config.$corner-radius-m;
25
+ background: var(--neutral-color-50);
26
+ color: var(--neutral-color-1000);
27
+ cursor: pointer;
28
+ -webkit-appearance: none;
29
+ -moz-appearance: none;
30
+ appearance: none;
31
+
32
+ &:focus-visible {
33
+ outline: none;
34
+ }
35
+
36
+ &:disabled {
37
+ border-left: 1px solid var(--neutral-color-400);
38
+ color: var(--neutral-color-400);
39
+ cursor: default;
40
+ }
41
+ }
42
+
43
+ // Input.scss colors every .icon inside .input-container as neutral-400 by
44
+ // default (see .password-icon there for the same kind of exception). This
45
+ // selector is qualified one level deeper than that rule so it wins on
46
+ // specificity regardless of CSS load order between the two files.
47
+ .input-with-select .input-container .input-suffix .select-suffix-trigger {
48
+ @include mixins.icon-color(var(--primary-color-600));
49
+
50
+ &:disabled {
51
+ @include mixins.icon-color(var(--neutral-color-400));
52
+ }
53
+ }
54
+
55
+ .select-suffix-options {
56
+ position: absolute;
57
+ top: calc(100% + #{config.$space-2x});
58
+ right: 0;
59
+ min-width: 100%;
60
+ margin: 0;
61
+ padding: config.$space-1x 0;
62
+ list-style: none;
63
+ text-align: left;
64
+ border-radius: config.$corner-radius-xxs;
65
+ background: var(--neutral-white);
66
+ z-index: depth.$z-dropdown-options;
67
+ box-shadow:
68
+ 0 9px 28px 8px rgba(0, 0, 0, 0.05),
69
+ 0 6px 16px 0 rgba(0, 0, 0, 0.08),
70
+ 0 3px 6px -4px rgba(0, 0, 0, 0.12);
71
+
72
+ .option {
73
+ @include typography.select-option-text;
74
+ padding: config.$space-2x config.$space-3x;
75
+ cursor: default;
76
+ list-style-type: none;
77
+ text-wrap: nowrap;
78
+
79
+ &:hover {
80
+ background-color: var(--primary-color-50);
81
+ }
82
+ }
83
+ }
@@ -0,0 +1,110 @@
1
+ import './InputWithSelect.scss'
2
+ import React, { useEffect, useRef, useState } from 'react'
3
+ import { useOpen } from '../../hooks/useOpen'
4
+ import { useOutsideClick } from '../../hooks/useOutsideClick'
5
+ import { classNames } from '../../utils/classNames'
6
+ import { Icon } from '../Icon'
7
+ import type { InputProps } from '../Input'
8
+ import { Input } from '../Input'
9
+ import type { Option } from '../Select'
10
+ import { SelectItem } from '../Select/SelectItem'
11
+
12
+ const ESCAPE_KEY = 'Escape'
13
+
14
+ export interface InputWithSelectProps extends Omit<
15
+ InputProps,
16
+ 'suffix' | 'rightIcon'
17
+ > {
18
+ options: Option[]
19
+ selectAccessibilityLabel: string
20
+ selectName?: string
21
+ defaultSelectedOption?: string
22
+ onSelectChange?: (value: string) => void
23
+ }
24
+
25
+ export function InputWithSelect({
26
+ options,
27
+ selectAccessibilityLabel,
28
+ selectName,
29
+ defaultSelectedOption,
30
+ onSelectChange = () => {},
31
+ className,
32
+ ...props
33
+ }: InputWithSelectProps): React.JSX.Element {
34
+ const { isOpen, close, toggle } = useOpen()
35
+ const defaultOption =
36
+ options.find((option) => option.id === defaultSelectedOption) || options[0]
37
+ const [selectedOption, setSelectedOption] = useState<Option>(defaultOption)
38
+ const containerRef = useRef(null)
39
+ const triggerRef = useRef<HTMLButtonElement>(null)
40
+ const optionsId = `${props.id || props.name}-select-options`
41
+
42
+ function handleClose() {
43
+ if (!isOpen) return
44
+ close()
45
+ }
46
+
47
+ useOutsideClick(containerRef, handleClose)
48
+
49
+ useEffect(() => {
50
+ if (!isOpen) return
51
+
52
+ function handleKeyDown(event: KeyboardEvent) {
53
+ if (event.key !== ESCAPE_KEY) return
54
+ close()
55
+ triggerRef.current?.focus()
56
+ }
57
+
58
+ document.addEventListener('keydown', handleKeyDown)
59
+ return () => document.removeEventListener('keydown', handleKeyDown)
60
+ }, [isOpen, close])
61
+
62
+ function handleSelectOption(option: Option) {
63
+ setSelectedOption(option)
64
+ onSelectChange(option.id)
65
+ close()
66
+ triggerRef.current?.focus()
67
+ }
68
+
69
+ return (
70
+ <Input
71
+ className={classNames('input-with-select', className)}
72
+ suffix={
73
+ <span className="select-suffix" ref={containerRef}>
74
+ <button
75
+ ref={triggerRef}
76
+ type="button"
77
+ role="combobox"
78
+ className="select-suffix-trigger"
79
+ aria-label={selectAccessibilityLabel}
80
+ aria-haspopup="listbox"
81
+ aria-expanded={isOpen}
82
+ aria-controls={optionsId}
83
+ disabled={props.disabled}
84
+ onClick={toggle}
85
+ >
86
+ <span>{selectedOption.label}</span>
87
+ <Icon size="3" name={isOpen ? 'AngleUp' : 'AngleDown'} decorative />
88
+ </button>
89
+ {selectName && (
90
+ <input type="hidden" name={selectName} value={selectedOption.id} />
91
+ )}
92
+ {isOpen && (
93
+ <ul id={optionsId} className="select-suffix-options" role="listbox">
94
+ {options.map((option) => (
95
+ <SelectItem
96
+ key={option.id}
97
+ label={option.label}
98
+ isSelected={option.id === selectedOption.id}
99
+ onSelectOption={() => handleSelectOption(option)}
100
+ onClose={close}
101
+ />
102
+ ))}
103
+ </ul>
104
+ )}
105
+ </span>
106
+ }
107
+ {...props}
108
+ />
109
+ )
110
+ }
@@ -0,0 +1,4 @@
1
+ import { InputWithSelect } from './InputWithSelect'
2
+
3
+ export type { InputWithSelectProps } from './InputWithSelect'
4
+ export { InputWithSelect }
@@ -4,6 +4,10 @@ import { Meta } from "@storybook/addon-docs/blocks";
4
4
 
5
5
  # Changelog
6
6
 
7
+ ## 1.2.32
8
+
9
+ * Add new variant style for Input component
10
+
7
11
  ## 1.2.31
8
12
 
9
13
  * Add Discount icon
@@ -0,0 +1,90 @@
1
+ import type { StoryObj } from '@storybook/nextjs'
2
+ import { InputWithSelect } from '../atoms/InputWithSelect'
3
+
4
+ const meta = {
5
+ title: 'Design System/Atoms/InputWithSelect',
6
+ component: InputWithSelect,
7
+ parameters: {
8
+ docs: {
9
+ description: {
10
+ component:
11
+ '<h2>Usage guidelines</h2>' +
12
+ '<p>The InputWithSelect component is used when the value the user types needs a unit or symbol attached to it, such as a discount percentage or a measurement. It combines an input field with a compact select fused into the same field, placed as a suffix.</p>' +
13
+ '<ul>' +
14
+ ' <li>Use when the unit is closely tied to the value and only a short, fixed list of symbols applies (e.g. %, kg, m²)</li>' +
15
+ ' <li>Keep the list of options short, these are not meant to be searched</li>' +
16
+ ' <li>Always include a visible label and a select accessibility label</li>' +
17
+ ' <li>Support help and error text as in a standard input</li>' +
18
+ '</ul>',
19
+ },
20
+ },
21
+ },
22
+ tags: ['autodocs', 'Data entry components'],
23
+ argTypes: {
24
+ label: {
25
+ description: 'Label for the input',
26
+ },
27
+ helpText: {
28
+ description: 'Optional help text',
29
+ },
30
+ name: {
31
+ description: 'Set name property for the input value',
32
+ },
33
+ selectName: {
34
+ description:
35
+ 'Set name property for the hidden input holding the selected option, if it needs to be submitted in a form',
36
+ },
37
+ options: {
38
+ description: 'Array of symbols/units to be displayed on the select',
39
+ },
40
+ defaultSelectedOption: {
41
+ description: 'Option id to be selected by default',
42
+ },
43
+ onSelectChange: {
44
+ description: 'Called with the selected option id when it changes',
45
+ },
46
+ selectAccessibilityLabel: {
47
+ description: 'Describes the select purpose for screen readers',
48
+ },
49
+ errors: {
50
+ description:
51
+ 'Optional array of errors. If passed, the errors are listed and invalid style is applied.',
52
+ },
53
+ disabled: {
54
+ description: 'Is the field in disabled state?',
55
+ },
56
+ },
57
+ }
58
+
59
+ export default meta
60
+ type Story = StoryObj<typeof meta>
61
+
62
+ export const Primary: Story = {
63
+ args: {
64
+ label: 'Discount',
65
+ name: 'discount',
66
+ type: 'number',
67
+ defaultValue: 10,
68
+ helpText: 'Applied to the total amount',
69
+ selectAccessibilityLabel: 'Select discount unit',
70
+ options: [
71
+ { id: '%', label: '%' },
72
+ { id: 'eur', label: '€' },
73
+ ],
74
+ id: 'discount',
75
+ },
76
+ }
77
+
78
+ export const WithErrors: Story = {
79
+ args: {
80
+ ...Primary.args,
81
+ errors: ['Discount must be lower than 100%'],
82
+ },
83
+ }
84
+
85
+ export const Disabled: Story = {
86
+ args: {
87
+ ...Primary.args,
88
+ disabled: true,
89
+ },
90
+ }
@@ -0,0 +1,71 @@
1
+ import { render, screen } from '@testing-library/react'
2
+ import userEvent from '@testing-library/user-event'
3
+ import React from 'react'
4
+ import { InputWithSelect } from '../src/atoms/InputWithSelect'
5
+
6
+ const options = [
7
+ { id: '%', label: '%' },
8
+ { id: 'kg', label: 'kg' },
9
+ ]
10
+
11
+ describe('InputWithSelect', () => {
12
+ it('renders the input and the select suffix with its default option', () => {
13
+ render(
14
+ <InputWithSelect
15
+ label="Discount"
16
+ name="discount"
17
+ options={options}
18
+ selectAccessibilityLabel="Select unit"
19
+ type="number"
20
+ />,
21
+ )
22
+
23
+ expect(screen.getByRole('spinbutton')).toBeInTheDocument()
24
+ expect(
25
+ screen.getByRole('combobox', { name: 'Select unit' }),
26
+ ).toHaveTextContent('%')
27
+ })
28
+
29
+ it('respects the default selected option', () => {
30
+ render(
31
+ <InputWithSelect
32
+ defaultSelectedOption="kg"
33
+ label="Discount"
34
+ name="discount"
35
+ options={options}
36
+ selectAccessibilityLabel="Select unit"
37
+ type="number"
38
+ />,
39
+ )
40
+
41
+ expect(
42
+ screen.getByRole('combobox', { name: 'Select unit' }),
43
+ ).toHaveTextContent('kg')
44
+ })
45
+
46
+ it('opens the options list and updates the selected option on click', async () => {
47
+ const user = userEvent.setup()
48
+ const handleSelectChange = jest.fn()
49
+ render(
50
+ <InputWithSelect
51
+ label="Discount"
52
+ name="discount"
53
+ onSelectChange={handleSelectChange}
54
+ options={options}
55
+ selectAccessibilityLabel="Select unit"
56
+ type="number"
57
+ />,
58
+ )
59
+
60
+ await user.click(screen.getByRole('combobox', { name: 'Select unit' }))
61
+ expect(screen.getByRole('listbox')).toBeInTheDocument()
62
+
63
+ await user.click(screen.getByRole('option', { name: 'kg' }))
64
+
65
+ expect(handleSelectChange).toHaveBeenCalledWith('kg')
66
+ expect(
67
+ screen.getByRole('combobox', { name: 'Select unit' }),
68
+ ).toHaveTextContent('kg')
69
+ expect(screen.queryByRole('listbox')).not.toBeInTheDocument()
70
+ })
71
+ })