Skip to content

Commit 1ceddeb

Browse files
committed
LPS-195265 Set chooseAnOption as empty value
1 parent 6f54277 commit 1ceddeb

5 files changed

Lines changed: 33 additions & 27 deletions

File tree

modules/apps/dynamic-data-mapping/dynamic-data-mapping-form-field-type/src/main/resources/META-INF/resources/Select/Select.tsx

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,43 @@ function Select({
3030
readOnly,
3131
required,
3232
selectedKey,
33+
viewMode,
3334
}: SelectProps) {
3435
const [selectedLabel, setSelectedLabel] = useState('');
3536
let newSelectedKey = selectedKey;
36-
if (selectedKey === null) {
37-
newSelectedKey = 'null';
37+
if (!selectedKey.length) {
38+
newSelectedKey = 'chooseAnOption';
3839
}
3940

40-
let selectedItem = newSelectedKey || predefinedValue;
41+
let selectedItem = newSelectedKey;
4142

42-
if (selectedItem?.length === 0) {
43-
selectedItem = '';
43+
if (newSelectedKey !== 'chooseAnOption') {
44+
selectedItem = selectedItem ?? predefinedValue;
4445
}
45-
else if (typeof (selectedItem as string) === 'string') {
46-
selectedItem =
47-
(newSelectedKey as string) || (predefinedValue as string) || '';
46+
else if (
47+
newSelectedKey === 'chooseAnOption' &&
48+
predefinedValue?.[0] &&
49+
!viewMode
50+
) {
51+
selectedItem = predefinedValue?.[0];
4852
}
49-
else {
50-
selectedItem = newSelectedKey[0] || predefinedValue?.[0] || '';
53+
else if (viewMode) {
54+
selectedItem = selectedItem ?? predefinedValue;
55+
}
56+
57+
if (typeof selectedItem !== 'string') {
58+
selectedItem = selectedItem[0];
5159
}
5260

5361
useEffect(() => {
5462
const selectedOption = options.find(
55-
(option) => option.value === selectedKey
63+
(option) => option.value === selectedKey[0] || option.active
5664
);
5765

5866
if (selectedOption) {
5967
setSelectedLabel(selectedOption.label);
6068
}
6169

62-
setSelectedLabel('');
6370
// eslint-disable-next-line react-hooks/exhaustive-deps
6471
}, [selectedKey]);
6572

@@ -83,9 +90,6 @@ function Select({
8390
if ((itemKey as string)?.includes('$.')) {
8491
newItemKey = '.';
8592
}
86-
else if (itemKey === 'null') {
87-
newItemKey = null;
88-
}
8993

9094
const field = options.find(
9195
({value}) => value === newItemKey
@@ -94,7 +98,7 @@ function Select({
9498
onChange({}, [field.value]);
9599
}}
96100
placeholder={Liferay.Language.get('choose-an-option')}
97-
selectedKey={selectedItem}
101+
selectedKey={selectedItem as string}
98102
>
99103
{(group) => (
100104
<DropDown.Group header={group.label} items={group.items}>
@@ -168,17 +172,19 @@ const Main = ({
168172

169173
if (!multiple) {
170174
if (
171-
options.length &&
175+
normalizedOptions.length &&
172176
value[0] &&
173-
!options.find((option) => option.value === value[0])
177+
!normalizedOptions.find((option) => option.value === value[0])
174178
) {
175179
newValue = '';
176180
}
177181

178182
if (
179-
options.length &&
183+
normalizedOptions.length &&
180184
predefinedValueArray[0] &&
181-
!options.find((option) => option.value === predefinedValueArray[0])
185+
!normalizedOptions.find(
186+
(option) => option.value === predefinedValueArray[0]
187+
)
182188
) {
183189
newPredefinedValue = [];
184190
}
@@ -228,6 +234,7 @@ const Main = ({
228234
required={otherProps.required}
229235
selectedKey={selectedKey || (newValue as string)}
230236
showEmptyOption={false}
237+
viewMode={viewMode}
231238
/>
232239
)}
233240
</ClayTooltipProvider>

modules/apps/dynamic-data-mapping/dynamic-data-mapping-form-field-type/src/main/resources/META-INF/resources/Select/select.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ interface Option<T> {
4747

4848
interface SelectProps extends Omit<MainProps, 'editingLanguageId' | 'value'> {
4949
selectedKey: string;
50+
viewMode: unknown;
5051
}
5152

5253
type MultiSelectItem = {

modules/apps/dynamic-data-mapping/dynamic-data-mapping-form-field-type/src/main/resources/META-INF/resources/util/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function normalizeOptions({
6565
if (showEmptyOption && !multiple) {
6666
newOptions.push({
6767
label: Liferay.Language.get('choose-an-option'),
68-
value: null,
68+
value: 'chooseAnOption',
6969
});
7070
}
7171

modules/apps/dynamic-data-mapping/dynamic-data-mapping-form-field-type/src/main/resources/META-INF/resources/util/tooltip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function getTooltipTitle({
1010
placeholder: string;
1111
value: string;
1212
}) {
13-
let tooltipText: {title: string} | string = '';
13+
let tooltipText: {title: string} = {title: ''};
1414

1515
if (value !== '') {
1616
tooltipText = {title: value};

modules/apps/dynamic-data-mapping/dynamic-data-mapping-form-field-type/types/src/main/resources/META-INF/resources/util/tooltip.d.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ export declare function getTooltipTitle({
99
}: {
1010
placeholder: string;
1111
value: string;
12-
}):
13-
| string
14-
| {
15-
title: string;
16-
};
12+
}): {
13+
title: string;
14+
};

0 commit comments

Comments
 (0)