@centreon/ui 26.11.11 → 26.11.12

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@centreon/ui",
3
- "version": "26.11.11",
3
+ "version": "26.11.12",
4
4
  "description": "Centreon UI Components",
5
5
  "scripts": {
6
6
  "update:deps": "pnpx npm-check-updates -i --format group",
@@ -22,10 +22,12 @@ const ValueDisplay = (): JSX.Element => {
22
22
 
23
23
  const initialize = ({
24
24
  notificationOptions = [],
25
- disabled = false
25
+ disabled = false,
26
+ variant
26
27
  }: {
27
28
  disabled?: boolean;
28
29
  notificationOptions?: Array<string> | null;
30
+ variant?: 'checkboxes' | 'chips';
29
31
  } = {}): void => {
30
32
  cy.mount({
31
33
  Component: (
@@ -38,7 +40,8 @@ const initialize = ({
38
40
  direction: 'horizontal',
39
41
  exclusiveLabel: 'No notifications',
40
42
  exclusiveOption: 'None',
41
- options
43
+ options,
44
+ variant
42
45
  },
43
46
  fieldName: 'notificationOptions',
44
47
  getDisabled: () => disabled,
@@ -172,3 +175,62 @@ describe('Exclusive checkbox group', () => {
172
175
  expectValue(['Down']);
173
176
  });
174
177
  });
178
+
179
+ describe('Exclusive checkbox group, as chips', () => {
180
+ const chip = (option: string): Cypress.Chainable =>
181
+ cy.findByTestId(`notification-options-${option}`);
182
+
183
+ it('displays the options as chips and the exclusive one as the toggle', () => {
184
+ initialize({ variant: 'chips' });
185
+
186
+ ['Down', 'Unreachable', 'Recovery'].forEach((option) => {
187
+ chip(option).should('have.attr', 'aria-pressed', 'false');
188
+ });
189
+ chip('None').should('not.exist');
190
+ toggle().should('not.be.checked');
191
+ cy.findByRole('checkbox', { name: 'Down' }).should('not.exist');
192
+ });
193
+
194
+ it('toggles a chip on and off', () => {
195
+ initialize({ variant: 'chips' });
196
+
197
+ chip('Down').click();
198
+ chip('Recovery').click();
199
+
200
+ expectValue(['Down', 'Recovery']);
201
+ chip('Down').should('have.attr', 'aria-pressed', 'true');
202
+
203
+ chip('Down').click();
204
+
205
+ expectValue(['Recovery']);
206
+ });
207
+
208
+ it('clears and disables the chips while the toggle is on', () => {
209
+ initialize({ notificationOptions: ['Down', 'Recovery'], variant: 'chips' });
210
+
211
+ toggle().click();
212
+
213
+ expectValue(['None']);
214
+ chip('Down')
215
+ .should('have.attr', 'aria-pressed', 'false')
216
+ .and('have.attr', 'aria-disabled', 'true');
217
+ });
218
+
219
+ it('leaves the chips empty and usable once the toggle is off', () => {
220
+ initialize({ notificationOptions: ['None'], variant: 'chips' });
221
+
222
+ toggle().click();
223
+
224
+ expectValue([]);
225
+ chip('Unreachable').click();
226
+
227
+ expectValue(['Unreachable']);
228
+ });
229
+
230
+ it('disables the chips and the toggle when the input is disabled', () => {
231
+ initialize({ disabled: true, variant: 'chips' });
232
+
233
+ chip('Down').should('have.attr', 'aria-disabled', 'true');
234
+ toggle().should('be.disabled');
235
+ });
236
+ });
@@ -1,8 +1,9 @@
1
- import { Box } from '@mui/material';
1
+ import { Box, Chip } from '@mui/material';
2
2
 
3
3
  import { type FormikValues, useFormikContext } from 'formik';
4
4
  import { equals, includes, path, reject, split } from 'ramda';
5
5
  import type { ChangeEvent } from 'react';
6
+ import { useTranslation } from 'react-i18next';
6
7
 
7
8
  import { useMemoComponent } from '../..';
8
9
  import { CheckboxGroup as CheckboxGroupComponent } from '../../Checkbox';
@@ -19,6 +20,7 @@ const ExclusiveCheckboxGroup = ({
19
20
  getDisabled,
20
21
  dataTestId
21
22
  }: InputPropsWithoutGroup): JSX.Element => {
23
+ const { t } = useTranslation();
22
24
  const { values, setFieldValue } = useFormikContext<FormikValues>();
23
25
 
24
26
  const fieldNamePath = split('.', fieldName);
@@ -53,10 +55,63 @@ const ExclusiveCheckboxGroup = ({
53
55
  );
54
56
  };
55
57
 
58
+ const toggleChip = (option: string) => (): void => {
59
+ setFieldValue(
60
+ fieldName,
61
+ includes(option, value)
62
+ ? reject(equals(option), value)
63
+ : [...value, option]
64
+ );
65
+ };
66
+
56
67
  const baseTestId = dataTestId || fieldName;
57
68
 
69
+ const isChips = equals(exclusiveCheckboxGroup?.variant, 'chips');
70
+
71
+ const exclusiveToggle = (
72
+ <Switch
73
+ change={changeToggle}
74
+ dataTestId={`${baseTestId}-exclusive`}
75
+ fieldName={fieldName}
76
+ getDisabled={() => disabled}
77
+ label={exclusiveCheckboxGroup?.exclusiveLabel as string}
78
+ switchInput={{
79
+ getChecked: (fieldValue) =>
80
+ includes(exclusiveOption, (fieldValue as Array<string>) ?? [])
81
+ }}
82
+ type={InputType.Switch}
83
+ />
84
+ );
85
+
58
86
  return useMemoComponent({
59
- Component: (
87
+ Component: isChips ? (
88
+ <div
89
+ className="flex flex-wrap items-center gap-x-6 gap-y-3"
90
+ data-testid={baseTestId}
91
+ >
92
+ {options.map((option) => {
93
+ const isSelected = !isExclusive && includes(option, value);
94
+
95
+ return (
96
+ <Chip
97
+ aria-pressed={isSelected}
98
+ // The theme sizes chips for status badges, 12px or 20px tall.
99
+ className="h-6 rounded-3xl text-sm [&_.MuiChip-label]:px-3 [&_.MuiChip-label]:leading-[21px]"
100
+ clickable
101
+ color={isSelected ? 'primary' : 'default'}
102
+ data-testid={`${baseTestId}-${option}`}
103
+ disabled={disabled || isExclusive}
104
+ key={option}
105
+ label={t(option)}
106
+ onClick={toggleChip(option)}
107
+ size="small"
108
+ variant={isSelected ? 'filled' : 'outlined'}
109
+ />
110
+ );
111
+ })}
112
+ {exclusiveToggle}
113
+ </div>
114
+ ) : (
60
115
  <Box sx={{ alignItems: 'center', columnGap: 2, display: 'flex' }}>
61
116
  <CheckboxGroupComponent
62
117
  dataTestId={baseTestId}
@@ -67,18 +122,7 @@ const ExclusiveCheckboxGroup = ({
67
122
  options={options}
68
123
  values={isExclusive ? [] : value}
69
124
  />
70
- <Switch
71
- change={changeToggle}
72
- dataTestId={`${baseTestId}-exclusive`}
73
- fieldName={fieldName}
74
- getDisabled={() => disabled}
75
- label={exclusiveCheckboxGroup?.exclusiveLabel as string}
76
- switchInput={{
77
- getChecked: (fieldValue) =>
78
- includes(exclusiveOption, (fieldValue as Array<string>) ?? [])
79
- }}
80
- type={InputType.Switch}
81
- />
125
+ {exclusiveToggle}
82
126
  </Box>
83
127
  ),
84
128
  memoProps: [
@@ -88,7 +132,8 @@ const ExclusiveCheckboxGroup = ({
88
132
  exclusiveOption,
89
133
  exclusiveCheckboxGroup?.exclusiveLabel,
90
134
  exclusiveCheckboxGroup?.direction,
91
- exclusiveCheckboxGroup?.labelPlacement
135
+ exclusiveCheckboxGroup?.labelPlacement,
136
+ exclusiveCheckboxGroup?.variant
92
137
  ]
93
138
  });
94
139
  };
@@ -12,6 +12,10 @@ const getSkeleton = cond<Array<InputType>, JSX.Element>([
12
12
  equals(InputType.Radio) as (b: InputType) => boolean,
13
13
  always(<LoadingSkeleton height={104} />)
14
14
  ],
15
+ [
16
+ equals(InputType.SegmentedButtons) as (b: InputType) => boolean,
17
+ always(<LoadingSkeleton height={56} />)
18
+ ],
15
19
  [
16
20
  equals(InputType.Text) as (b: InputType) => boolean,
17
21
  always(<LoadingSkeleton height={52} />)
@@ -0,0 +1,101 @@
1
+ import { Typography } from '@mui/material';
2
+
3
+ import { useFormikContext } from 'formik';
4
+ import { object } from 'yup';
5
+
6
+ import { Form } from '../Form';
7
+ import { InputType } from './models';
8
+
9
+ const options = [
10
+ { label: 'Yes', value: 'true' },
11
+ { label: 'No', value: 'false' },
12
+ { label: 'Default', value: 'use_default' }
13
+ ];
14
+
15
+ const ValueDisplay = (): JSX.Element => {
16
+ const { values } = useFormikContext<{ enabled: string }>();
17
+
18
+ return (
19
+ <Typography data-testid="value">
20
+ {JSON.stringify(values.enabled)}
21
+ </Typography>
22
+ );
23
+ };
24
+
25
+ const initialize = ({
26
+ enabled = 'use_default',
27
+ disabled = false
28
+ }: {
29
+ disabled?: boolean;
30
+ enabled?: string;
31
+ } = {}): void => {
32
+ cy.mount({
33
+ Component: (
34
+ <Form
35
+ initialValues={{ enabled }}
36
+ inputs={[
37
+ {
38
+ dataTestId: 'enabled',
39
+ fieldName: 'enabled',
40
+ getDisabled: () => disabled,
41
+ group: '',
42
+ label: 'Notification Enabled',
43
+ segmentedButtons: { options },
44
+ type: InputType.SegmentedButtons
45
+ },
46
+ {
47
+ custom: { Component: ValueDisplay },
48
+ fieldName: 'value',
49
+ group: '',
50
+ label: 'value',
51
+ type: InputType.Custom
52
+ }
53
+ ]}
54
+ submit={cy.stub()}
55
+ validationSchema={object()}
56
+ />
57
+ )
58
+ });
59
+ };
60
+
61
+ const button = (label: string): Cypress.Chainable =>
62
+ cy.findByRole('button', { name: label });
63
+
64
+ describe('Segmented buttons', () => {
65
+ it('displays every option and marks the current one', () => {
66
+ initialize();
67
+
68
+ cy.contains('Notification Enabled').should('be.visible');
69
+ button('Yes').should('have.attr', 'aria-pressed', 'false');
70
+ button('No').should('have.attr', 'aria-pressed', 'false');
71
+ button('Default').should('have.attr', 'aria-pressed', 'true');
72
+ });
73
+
74
+ it('stores the value of the clicked option as given', () => {
75
+ initialize();
76
+
77
+ button('Yes').click();
78
+
79
+ // A string, not the boolean the radio would store.
80
+ cy.findByTestId('value').should('have.text', '"true"');
81
+ button('Yes').should('have.attr', 'aria-pressed', 'true');
82
+ button('Default').should('have.attr', 'aria-pressed', 'false');
83
+
84
+ button('No').click();
85
+
86
+ cy.findByTestId('value').should('have.text', '"false"');
87
+ });
88
+
89
+ it('starts on the initial value', () => {
90
+ initialize({ enabled: 'false' });
91
+
92
+ button('No').should('have.attr', 'aria-pressed', 'true');
93
+ });
94
+
95
+ it('does not change when disabled', () => {
96
+ initialize({ disabled: true });
97
+
98
+ button('Yes').should('be.disabled');
99
+ cy.findByTestId('value').should('have.text', '"use_default"');
100
+ });
101
+ });
@@ -0,0 +1,71 @@
1
+ import { Button, ButtonGroup, FormGroup, FormLabel } from '@mui/material';
2
+
3
+ import { type FormikValues, useFormikContext } from 'formik';
4
+ import { equals, path, split } from 'ramda';
5
+ import { useTranslation } from 'react-i18next';
6
+
7
+ import { useMemoComponent } from '../..';
8
+ import type { InputPropsWithoutGroup } from './models';
9
+
10
+ const SegmentedButtons = ({
11
+ dataTestId,
12
+ fieldName,
13
+ label,
14
+ segmentedButtons,
15
+ getDisabled,
16
+ additionalMemoProps
17
+ }: InputPropsWithoutGroup): JSX.Element => {
18
+ const { t } = useTranslation();
19
+
20
+ const { values, setFieldValue, setFieldTouched } =
21
+ useFormikContext<FormikValues>();
22
+
23
+ const value = path(split('.', fieldName), values);
24
+
25
+ const disabled = getDisabled?.(values) || false;
26
+
27
+ const select = (optionValue: string) => (): void => {
28
+ setFieldTouched(fieldName, true, false);
29
+ setFieldValue(fieldName, optionValue);
30
+ };
31
+
32
+ return useMemoComponent({
33
+ Component: (
34
+ <FormGroup>
35
+ <FormLabel>{t(label)}</FormLabel>
36
+ <ButtonGroup
37
+ aria-label={t(label)}
38
+ // As wide as its column, up to what three labels need.
39
+ className="max-w-[412px]"
40
+ color="primary"
41
+ data-testid={dataTestId}
42
+ disabled={disabled}
43
+ fullWidth
44
+ size="small"
45
+ >
46
+ {segmentedButtons?.options.map(
47
+ ({ label: optionLabel, value: optionValue }) => {
48
+ const isSelected = equals(value, optionValue);
49
+
50
+ return (
51
+ <Button
52
+ aria-pressed={isSelected}
53
+ className="h-7 px-3 py-0 font-medium text-sm leading-[21px] normal-case"
54
+ data-testid={`${dataTestId}-${optionValue}`}
55
+ key={optionValue}
56
+ onClick={select(optionValue)}
57
+ variant={isSelected ? 'contained' : 'outlined'}
58
+ >
59
+ {t(optionLabel)}
60
+ </Button>
61
+ );
62
+ }
63
+ )}
64
+ </ButtonGroup>
65
+ </FormGroup>
66
+ ),
67
+ memoProps: [value, disabled, segmentedButtons, additionalMemoProps]
68
+ });
69
+ };
70
+
71
+ export default SegmentedButtons;
@@ -48,6 +48,7 @@ import {
48
48
  InputType
49
49
  } from './models';
50
50
  import RadioInput from './Radio';
51
+ import SegmentedButtons from './SegmentedButtons';
51
52
  import { SubgroupDivider } from './SubGroupDivider';
52
53
  import SwitchInput from './Switch';
53
54
  import TextInput from './Text';
@@ -89,6 +90,10 @@ export const getInput = cond<
89
90
  equals(InputType.ExclusiveCheckboxGroup) as (b: InputType) => boolean,
90
91
  always(ExclusiveCheckboxGroup)
91
92
  ],
93
+ [
94
+ equals(InputType.SegmentedButtons) as (b: InputType) => boolean,
95
+ always(SegmentedButtons)
96
+ ],
92
97
  [equals(InputType.List) as (b: InputType) => boolean, always(List)],
93
98
  [equals(InputType.File) as (b: InputType) => boolean, always(File)],
94
99
  [equals(InputType.Text) as (b: InputType) => boolean, always(TextInput)],
@@ -51,7 +51,8 @@ export enum InputType {
51
51
  List = 13,
52
52
  File = 14,
53
53
  Divider = 15,
54
- ExclusiveCheckboxGroup = 16
54
+ ExclusiveCheckboxGroup = 16,
55
+ SegmentedButtons = 17
55
56
  }
56
57
 
57
58
  interface FieldsTableGetRequiredProps {
@@ -111,6 +112,8 @@ export interface InputProps {
111
112
  exclusiveOption: string;
112
113
  labelPlacement?: LabelPlacement;
113
114
  options: Array<string>;
115
+ // The options as chips instead of checkboxes; the exclusive one stays a switch.
116
+ variant?: 'checkboxes' | 'chips';
114
117
  };
115
118
  file?: {
116
119
  multiple?: boolean;
@@ -167,6 +170,12 @@ export interface InputProps {
167
170
  row?: boolean;
168
171
  };
169
172
  required?: boolean;
173
+ segmentedButtons?: {
174
+ options: Array<{
175
+ label: string;
176
+ value: string;
177
+ }>;
178
+ };
170
179
  switchInput?: {
171
180
  getChecked?: (value: unknown) => boolean;
172
181
  };