@centreon/ui 26.11.6 → 26.11.8

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.6",
3
+ "version": "26.11.8",
4
4
  "description": "Centreon UI Components",
5
5
  "scripts": {
6
6
  "update:deps": "pnpx npm-check-updates -i --format group",
@@ -1,4 +1,7 @@
1
+ import { object } from 'yup';
2
+
1
3
  import { Form, GroupDirection } from './Form';
4
+ import { InputType } from './Inputs/models';
2
5
  import {
3
6
  type BasicForm,
4
7
  basicFormGroups,
@@ -55,3 +58,55 @@ export const basicFormWithHorizontalGroups = (): JSX.Element => (
55
58
  groups={basicFormGroups.filter((group) => group.order !== 3)}
56
59
  />
57
60
  );
61
+
62
+ export const exclusiveCheckboxGroup = (): JSX.Element => (
63
+ <Form
64
+ initialValues={{
65
+ hostNotificationOptions: ['Down', 'Recovery'],
66
+ serviceNotificationOptions: ['None']
67
+ }}
68
+ inputs={[
69
+ {
70
+ exclusiveCheckboxGroup: {
71
+ direction: 'horizontal',
72
+ exclusiveLabel: 'No notifications',
73
+ exclusiveOption: 'None',
74
+ options: [
75
+ 'Down',
76
+ 'Unreachable',
77
+ 'Recovery',
78
+ 'Flapping',
79
+ 'Downtime Scheduled',
80
+ 'None'
81
+ ]
82
+ },
83
+ fieldName: 'hostNotificationOptions',
84
+ group: '',
85
+ label: 'Host notification options',
86
+ type: InputType.ExclusiveCheckboxGroup
87
+ },
88
+ {
89
+ exclusiveCheckboxGroup: {
90
+ direction: 'horizontal',
91
+ exclusiveLabel: 'No notifications',
92
+ exclusiveOption: 'None',
93
+ options: [
94
+ 'Warning',
95
+ 'Unknown',
96
+ 'Critical',
97
+ 'Recovery',
98
+ 'Flapping',
99
+ 'Downtime Scheduled',
100
+ 'None'
101
+ ]
102
+ },
103
+ fieldName: 'serviceNotificationOptions',
104
+ group: '',
105
+ label: 'Service notification options',
106
+ type: InputType.ExclusiveCheckboxGroup
107
+ }
108
+ ]}
109
+ submit={submit}
110
+ validationSchema={object()}
111
+ />
112
+ );
@@ -29,11 +29,7 @@ const CheckboxGroup = ({
29
29
  if (!disabled && !hideCheckbox) {
30
30
  return;
31
31
  }
32
- const resetedValue = (value ?? []).map((element) => ({
33
- ...(element as unknown as Record<string, unknown>),
34
- checked: false
35
- }));
36
- setFieldValue(fieldName, resetedValue);
32
+ setFieldValue(fieldName, []);
37
33
  }, [disabled, hideCheckbox, fieldName, setFieldValue]);
38
34
 
39
35
  const handleChange = (event: ChangeEvent<HTMLInputElement>): void => {
@@ -45,14 +45,20 @@ const ConnectedAutocomplete = ({
45
45
  const isMultiple = equals(type, InputType.MultiConnectedAutocomplete);
46
46
 
47
47
  const getEndpoint = (parameters: GetEndpointParams): string => {
48
+ // Once a value is selected the field prepends a `$ni` condition excluding
49
+ // it, so the typed text is not necessarily the first condition -- and
50
+ // clearing the text leaves no `$lk` condition at all.
51
+ const searchedValue = parameters?.search?.conditions?.find(
52
+ (condition) => condition?.values?.$lk
53
+ )?.values?.$lk as string | undefined;
54
+
48
55
  const nameQueryParameters =
49
- connectedAutocomplete?.useNewAPIFormat && parameters?.search
56
+ connectedAutocomplete?.useNewAPIFormat && searchedValue
50
57
  ? [
51
58
  {
52
59
  name: 'name[lk]',
53
- value: (
54
- parameters.search.conditions?.[0].values?.$lk as string
55
- ).slice(1, -1)
60
+ // The field wraps the typed text in `%`.
61
+ value: searchedValue.slice(1, -1)
56
62
  }
57
63
  ]
58
64
  : [];
@@ -166,6 +172,7 @@ const ConnectedAutocomplete = ({
166
172
  return useMemoComponent({
167
173
  Component: (
168
174
  <TypedAutocompleteField
175
+ baseEndpoint={connectedAutocomplete?.baseEndpoint}
169
176
  chipProps={chipProps}
170
177
  dataTestId={dataTestId}
171
178
  decoder={connectedAutocomplete?.decoder}
@@ -187,6 +194,7 @@ const ConnectedAutocomplete = ({
187
194
  onBlur={blur}
188
195
  onChange={changeAutocomplete}
189
196
  optionProperty={connectedAutocomplete?.optionProperty}
197
+ queryKey={connectedAutocomplete?.queryKey}
190
198
  required={isRequired}
191
199
  searchConditions={connectedAutocomplete?.additionalConditionParameters}
192
200
  value={value ?? null}
@@ -0,0 +1,174 @@
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 = ['Down', 'Unreachable', 'Recovery', 'None'];
10
+
11
+ const ValueDisplay = (): JSX.Element => {
12
+ const { values } = useFormikContext<{
13
+ notificationOptions: Array<string> | null;
14
+ }>();
15
+
16
+ return (
17
+ <Typography data-testid="value">
18
+ {JSON.stringify(values.notificationOptions)}
19
+ </Typography>
20
+ );
21
+ };
22
+
23
+ const initialize = ({
24
+ notificationOptions = [],
25
+ disabled = false
26
+ }: {
27
+ disabled?: boolean;
28
+ notificationOptions?: Array<string> | null;
29
+ } = {}): void => {
30
+ cy.mount({
31
+ Component: (
32
+ <Form
33
+ initialValues={{ notificationOptions }}
34
+ inputs={[
35
+ {
36
+ dataTestId: 'notification-options',
37
+ exclusiveCheckboxGroup: {
38
+ direction: 'horizontal',
39
+ exclusiveLabel: 'No notifications',
40
+ exclusiveOption: 'None',
41
+ options
42
+ },
43
+ fieldName: 'notificationOptions',
44
+ getDisabled: () => disabled,
45
+ group: '',
46
+ label: 'Notification options',
47
+ type: InputType.ExclusiveCheckboxGroup
48
+ },
49
+ {
50
+ custom: { Component: ValueDisplay },
51
+ fieldName: 'value',
52
+ group: '',
53
+ label: 'value',
54
+ type: InputType.Custom
55
+ }
56
+ ]}
57
+ submit={cy.stub()}
58
+ validationSchema={object()}
59
+ />
60
+ )
61
+ });
62
+ };
63
+
64
+ const toggle = (): Cypress.Chainable => cy.findByLabelText('No notifications');
65
+
66
+ const expectValue = (value: Array<string> | null): void => {
67
+ cy.findByTestId('value').should('have.text', JSON.stringify(value));
68
+ };
69
+
70
+ describe('Exclusive checkbox group', () => {
71
+ it('displays the toggle and every option except the exclusive one', () => {
72
+ initialize();
73
+
74
+ toggle().should('not.be.checked');
75
+ cy.findByLabelText('Down').should('exist');
76
+ cy.findByLabelText('Unreachable').should('exist');
77
+ cy.findByLabelText('Recovery').should('exist');
78
+ cy.findByLabelText('None').should('not.exist');
79
+
80
+ cy.makeSnapshot();
81
+ });
82
+
83
+ it('keeps the inherited state when nothing is selected', () => {
84
+ initialize();
85
+
86
+ toggle().should('not.be.checked');
87
+ cy.findByLabelText('Down').should('not.be.checked').and('be.enabled');
88
+ expectValue([]);
89
+ });
90
+
91
+ it('treats a null value as the inherited state', () => {
92
+ initialize({ notificationOptions: null });
93
+
94
+ toggle().should('not.be.checked');
95
+ cy.findByLabelText('Down').should('not.be.checked').and('be.enabled');
96
+ });
97
+
98
+ it('displays the exclusive state from the initial value', () => {
99
+ initialize({ notificationOptions: ['None'] });
100
+
101
+ toggle().should('be.checked');
102
+ cy.findByLabelText('Down').should('not.be.checked').and('be.disabled');
103
+ expectValue(['None']);
104
+
105
+ cy.makeSnapshot();
106
+ });
107
+
108
+ it('displays a selection from the initial value', () => {
109
+ initialize({ notificationOptions: ['Down', 'Recovery'] });
110
+
111
+ toggle().should('not.be.checked');
112
+ cy.findByLabelText('Down').should('be.checked');
113
+ cy.findByLabelText('Unreachable').should('not.be.checked');
114
+ cy.findByLabelText('Recovery').should('be.checked');
115
+ expectValue(['Down', 'Recovery']);
116
+ });
117
+
118
+ it('goes from the inherited state to a selection and back', () => {
119
+ initialize();
120
+
121
+ cy.findByLabelText('Down').click();
122
+ cy.findByLabelText('Recovery').click();
123
+ expectValue(['Down', 'Recovery']);
124
+
125
+ cy.findByLabelText('Down').click();
126
+ cy.findByLabelText('Recovery').click();
127
+ expectValue([]);
128
+ toggle().should('not.be.checked');
129
+ });
130
+
131
+ it('goes from the inherited state to the exclusive state and back', () => {
132
+ initialize();
133
+
134
+ toggle().click();
135
+ toggle().should('be.checked');
136
+ expectValue(['None']);
137
+
138
+ toggle().click();
139
+ toggle().should('not.be.checked');
140
+ expectValue([]);
141
+ });
142
+
143
+ it('clears the selection when the toggle is turned on and leaves it empty when turned off', () => {
144
+ initialize({ notificationOptions: ['Down', 'Recovery'] });
145
+
146
+ toggle().click();
147
+ expectValue(['None']);
148
+ cy.findByLabelText('Down').should('not.be.checked').and('be.disabled');
149
+ cy.findByLabelText('Recovery').should('not.be.checked').and('be.disabled');
150
+
151
+ toggle().click();
152
+ expectValue([]);
153
+ cy.findByLabelText('Down').should('not.be.checked').and('be.enabled');
154
+
155
+ cy.findByLabelText('Unreachable').click();
156
+ expectValue(['Unreachable']);
157
+ });
158
+
159
+ it('prevents ticking a box while the toggle is on', () => {
160
+ initialize({ notificationOptions: ['None'] });
161
+
162
+ cy.findByLabelText('Down').click({ force: true });
163
+ cy.findByLabelText('Down').should('not.be.checked');
164
+ expectValue(['None']);
165
+ });
166
+
167
+ it('disables both the toggle and the checkboxes when the input is disabled', () => {
168
+ initialize({ disabled: true, notificationOptions: ['Down'] });
169
+
170
+ toggle().should('be.disabled');
171
+ cy.findByLabelText('Down').should('be.checked').and('be.disabled');
172
+ expectValue(['Down']);
173
+ });
174
+ });
@@ -0,0 +1,96 @@
1
+ import { Box } from '@mui/material';
2
+
3
+ import { type FormikValues, useFormikContext } from 'formik';
4
+ import { equals, includes, path, reject, split } from 'ramda';
5
+ import type { ChangeEvent } from 'react';
6
+
7
+ import { useMemoComponent } from '../..';
8
+ import { CheckboxGroup as CheckboxGroupComponent } from '../../Checkbox';
9
+ import {
10
+ type ChangeArgs,
11
+ type InputPropsWithoutGroup,
12
+ InputType
13
+ } from './models';
14
+ import Switch from './Switch';
15
+
16
+ const ExclusiveCheckboxGroup = ({
17
+ exclusiveCheckboxGroup,
18
+ fieldName,
19
+ getDisabled,
20
+ dataTestId
21
+ }: InputPropsWithoutGroup): JSX.Element => {
22
+ const { values, setFieldValue } = useFormikContext<FormikValues>();
23
+
24
+ const fieldNamePath = split('.', fieldName);
25
+
26
+ const value = (path(fieldNamePath, values) as Array<string> | null) ?? [];
27
+
28
+ const exclusiveOption = exclusiveCheckboxGroup?.exclusiveOption as string;
29
+ const options = reject(
30
+ equals(exclusiveOption),
31
+ exclusiveCheckboxGroup?.options ?? []
32
+ );
33
+
34
+ const isExclusive = includes(exclusiveOption, value);
35
+ const disabled = getDisabled?.(values) || false;
36
+
37
+ const changeToggle = ({ value: checked }: ChangeArgs): void => {
38
+ setFieldValue(fieldName, checked ? [exclusiveOption] : []);
39
+ };
40
+
41
+ const changeCheckbox = (event: ChangeEvent<HTMLInputElement>): void => {
42
+ if (disabled || isExclusive) {
43
+ return;
44
+ }
45
+
46
+ const option = event.target.id;
47
+
48
+ setFieldValue(
49
+ fieldName,
50
+ includes(option, value)
51
+ ? reject(equals(option), value)
52
+ : [...value, option]
53
+ );
54
+ };
55
+
56
+ const baseTestId = dataTestId || fieldName;
57
+
58
+ return useMemoComponent({
59
+ Component: (
60
+ <Box sx={{ alignItems: 'center', columnGap: 2, display: 'flex' }}>
61
+ <CheckboxGroupComponent
62
+ dataTestId={baseTestId}
63
+ direction={exclusiveCheckboxGroup?.direction}
64
+ disabled={disabled || isExclusive}
65
+ labelPlacement={exclusiveCheckboxGroup?.labelPlacement || 'end'}
66
+ onChange={changeCheckbox}
67
+ options={options}
68
+ values={isExclusive ? [] : value}
69
+ />
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
+ />
82
+ </Box>
83
+ ),
84
+ memoProps: [
85
+ value,
86
+ disabled,
87
+ options,
88
+ exclusiveOption,
89
+ exclusiveCheckboxGroup?.exclusiveLabel,
90
+ exclusiveCheckboxGroup?.direction,
91
+ exclusiveCheckboxGroup?.labelPlacement
92
+ ]
93
+ });
94
+ };
95
+
96
+ export default ExclusiveCheckboxGroup;
@@ -35,6 +35,7 @@ import Checkbox from './Checkbox';
35
35
  import CheckboxGroup from './CheckboxGroup';
36
36
  import ConnectedAutocomplete from './ConnectedAutocomplete';
37
37
  import Custom from './Custom';
38
+ import ExclusiveCheckboxGroup from './ExclusiveCheckboxGroup';
38
39
  import FieldsTable from './FieldsTable/FieldsTable';
39
40
  import File from './File';
40
41
  import Grid from './Grid';
@@ -84,6 +85,10 @@ export const getInput = cond<
84
85
  equals(InputType.CheckboxGroup) as (b: InputType) => boolean,
85
86
  always(CheckboxGroup)
86
87
  ],
88
+ [
89
+ equals(InputType.ExclusiveCheckboxGroup) as (b: InputType) => boolean,
90
+ always(ExclusiveCheckboxGroup)
91
+ ],
87
92
  [equals(InputType.List) as (b: InputType) => boolean, always(List)],
88
93
  [equals(InputType.File) as (b: InputType) => boolean, always(File)],
89
94
  [equals(InputType.Text) as (b: InputType) => boolean, always(TextInput)],
@@ -50,7 +50,8 @@ export enum InputType {
50
50
  CheckboxGroup = 12,
51
51
  List = 13,
52
52
  File = 14,
53
- Divider = 15
53
+ Divider = 15,
54
+ ExclusiveCheckboxGroup = 16
54
55
  }
55
56
 
56
57
  interface FieldsTableGetRequiredProps {
@@ -87,6 +88,12 @@ export interface InputProps {
87
88
  useNewAPIFormat?: boolean;
88
89
  additionalConditionParameters: Array<ConditionsSearchParameter>;
89
90
  customQueryParameters: Array<QueryParameter>;
91
+ // The API the options are read from. Left out, the request goes to the
92
+ // default `./api/latest` as it always has.
93
+ baseEndpoint?: string;
94
+ // What the options are cached under. Left out, the field's label is used,
95
+ // which two fields reading different endpoints can share.
96
+ queryKey?: string;
90
97
  chipColor?: string;
91
98
  endpoint?: string;
92
99
  filterKey?: string;
@@ -98,6 +105,13 @@ export interface InputProps {
98
105
  limitTags?: number;
99
106
  decoder?: JsonDecoder.Decoder<unknown>;
100
107
  };
108
+ exclusiveCheckboxGroup?: {
109
+ direction?: 'horizontal' | 'vertical';
110
+ exclusiveLabel: string;
111
+ exclusiveOption: string;
112
+ labelPlacement?: LabelPlacement;
113
+ options: Array<string>;
114
+ };
101
115
  file?: {
102
116
  multiple?: boolean;
103
117
  accept?: string;
@@ -126,8 +126,11 @@ const ConnectedAutocompleteField = (
126
126
  search: searchParameter
127
127
  });
128
128
  },
129
+ // Two fields can carry the same label and read different APIs, and
130
+ // would otherwise share each other's options.
129
131
  getQueryKey: () => [
130
132
  `autocomplete-${queryKey || props.label}`,
133
+ baseEndpoint,
131
134
  page,
132
135
  searchParameter
133
136
  ],