Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 83 additions & 2 deletions src/lib/forms/SelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
import { FeedbackLabel } from "../forms/FeedbackLabel";

export class SelectField extends Component {
constructor(props) {
super(props);
this.state = {
options: props.options || [],
};
}

renderError = (meta, initialValue, initialErrors, value, errors) => {

Check warning on line 22 in src/lib/forms/SelectField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

renderError should be placed after componentDidUpdate

Check warning on line 22 in src/lib/forms/SelectField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

renderError should be placed after componentDidUpdate
const { error, fieldPath } = this.props;
const computedError =
error ||
Expand All @@ -31,6 +38,23 @@
);
};

componentDidUpdate(prevProps) {
if (prevProps.options !== this.props.options) {

Check warning on line 42 in src/lib/forms/SelectField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Must use destructuring props assignment

Check warning on line 42 in src/lib/forms/SelectField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Must use destructuring props assignment
const nextOptions = this.props.options || [];

Check warning on line 43 in src/lib/forms/SelectField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Must use destructuring props assignment

Check warning on line 43 in src/lib/forms/SelectField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Must use destructuring props assignment
this.setState((prevState) => {
// Merge previous state options and new props options, de-duplicating by value
const existing = prevState.options || [];
const merged = [...existing];
nextOptions.forEach((opt) => {
if (!merged.some((o) => o.value === opt.value)) {
merged.push(opt);
}
});
return { options: merged };
});
}
}

renderFormField = (formikProps) => {
const {
form: {
Expand All @@ -55,11 +79,42 @@
multiple,
disabled,
required,
allowAdditions,
...uiProps
} = cmpProps;
const _defaultValue = multiple ? [] : "";
const value = getIn(values, fieldPath, defaultValue || _defaultValue);
console.log("defaultValue:", defaultValue);
let value = getIn(values, fieldPath, defaultValue || _defaultValue);
// Fix: for multiple selects, normalize empty string to empty array
if (multiple && (value === "" || value === null || value === undefined)) {
value = [];
}
const initialValue = getIn(initialValues, fieldPath, _defaultValue);
let dropdownOptions =
(this.state.options && this.state.options.length > 0

Check warning on line 94 in src/lib/forms/SelectField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Must use destructuring state assignment

Check warning on line 94 in src/lib/forms/SelectField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Must use destructuring state assignment

Check warning on line 94 in src/lib/forms/SelectField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Must use destructuring state assignment

Check warning on line 94 in src/lib/forms/SelectField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Must use destructuring state assignment
? this.state.options

Check warning on line 95 in src/lib/forms/SelectField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Must use destructuring state assignment

Check warning on line 95 in src/lib/forms/SelectField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Must use destructuring state assignment
: options) || [];

// Ensure that all currently selected values are present in the options list
const ensureOptionPresent = (val) => {
if (val === undefined || val === null || val === "") return;
if (!dropdownOptions.some((opt) => opt.value === val)) {
dropdownOptions = [...dropdownOptions, { key: val, text: val, value: val }];
}
};

if (multiple) {
if (Array.isArray(value)) {
value.forEach((v) => ensureOptionPresent(v));
} else {
ensureOptionPresent(value);
}
} else {
ensureOptionPresent(value);
}
console.log("SelectField fieldPath:", fieldPath);
console.log("SelectField value:", value);
console.log("SelectField options:", dropdownOptions);
return (
<Form.Dropdown
fluid
Expand All @@ -83,12 +138,36 @@
onAddItem={(event, data) => {
if (onAddItem) {
onAddItem({ event, data, formikProps });
} else {
const newValue = data.value;
// Add the new option to the local options state (if not already present)
this.setState((prevState) => {
const prevOptions = prevState.options || [];
if (prevOptions.some((opt) => opt.value === newValue)) {
return null;
}
const newOption = { key: newValue, text: newValue, value: newValue };
return { options: [...prevOptions, newOption] };
});

// Update the Formik field value
if (multiple) {
const current = Array.isArray(value)
? value
: value === undefined || value === null || value === ""
? []
: [value];
setFieldValue(fieldPath, [...current, newValue]);
} else {
setFieldValue(fieldPath, newValue);
}
}
}}
options={options}
options={dropdownOptions}
value={value}
multiple={multiple}
selectOnBlur={false}
allowAdditions={allowAdditions}
{...uiProps}
/>
);
Expand Down Expand Up @@ -120,6 +199,7 @@
label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
onChange: PropTypes.func,
onAddItem: PropTypes.func,
allowAdditions: PropTypes.bool,
multiple: PropTypes.bool,
helpText: PropTypes.string,
required: PropTypes.bool,
Expand All @@ -137,4 +217,5 @@
helpText: undefined,
required: false,
disabled: false,
allowAdditions: false,
};
4 changes: 4 additions & 0 deletions src/lib/forms/widgets/select/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DropdownComponent extends Component {
required,
disabled,
optimized,
allowAdditions,
} = this.props;

const helpText = helpTextProp ?? description;
Expand All @@ -54,6 +55,7 @@ class DropdownComponent extends Component {
defaultValue={multiple ? [] : ""}
helpText={helpText}
optimized={optimized}
allowAdditions={allowAdditions}
/>
);
}
Expand All @@ -78,6 +80,7 @@ DropdownComponent.propTypes = {
*/
icon: PropTypes.string,
optimized: PropTypes.bool,
allowAdditions: PropTypes.bool,
...fieldCommonProps,
};

Expand All @@ -88,6 +91,7 @@ DropdownComponent.defaultProps = {
clearable: true,
description: undefined,
optimized: true,
allowAdditions: false,
};

export const Dropdown = showHideOverridableWithDynamicId(DropdownComponent);