Skip to content

Commit

Permalink
[new_profile] Auto-select only site option (#6156)
Browse files Browse the repository at this point in the history
Resolves #5380
  • Loading branch information
zaliqarosli authored Aug 19, 2020
1 parent d504f85 commit 34d1d5a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Quality Control, and Behavioural Quality Control. (PR #6041)
- Addition of a new `account_request_date` in `users` table that will be used when
requesting a new account and will be displayed in the User Accounts module (PR #6191)
- Candidate's age can be retrieved from the Candidate class in days, months, or years (PR #5945)
- Addition of autoSelect prop to React SelectElement allows for auto-selection of only available select option (PR #6156)
- An `AcquisitionDate` field has been added to the `files` table (PR #6892)
#### Bug Fixes
- *Add item here*
Expand Down
2 changes: 2 additions & 0 deletions jsx/Filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Filter extends Component {
key={filter.name}
options={filter.options}
sortByValue={filter.sortByValue}
autoSelect={false}
/>
);
break;
Expand All @@ -87,6 +88,7 @@ class Filter extends Component {
options={filter.options}
multiple={true}
emptyOption={false}
autoSelect={false}
/>;
break;
case 'numeric':
Expand Down
31 changes: 31 additions & 0 deletions jsx/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,35 @@ class SelectElement extends Component {
this.handleChange = this.handleChange.bind(this);
}

/**
* Call onUserInput on component rendered to select only option
* if autoSelect prop is set to true
*/
componentDidMount() {
const optionsArray = Object.keys(this.props.options);
if (this.props.autoSelect && optionsArray.length === 1) {
this.props.onUserInput(this.props.name, optionsArray[0]);
}
}

/**
* On component update, if number of options dynamically
* changes to 1, call onUserInput to select only option
* if autoSelect prop is set to true
*
* @param {object} prevProps - component props before component update
*/
componentDidUpdate(prevProps) {
const options = Object.keys(this.props.options);
const prevOptions = Object.keys(prevProps.options);
if (options.length !== prevOptions.length ||
!options.every((v, i) => v === prevOptions[i])) {
if (this.props.autoSelect && options.length === 1) {
this.props.onUserInput(this.props.name, options[0]);
}
}
}

/**
* Handle change
*
Expand Down Expand Up @@ -605,6 +634,7 @@ SelectElement.propTypes = {
disabled: PropTypes.bool,
required: PropTypes.bool,
emptyOption: PropTypes.bool,
autoSelect: PropTypes.bool,
hasError: PropTypes.bool,
errorMessage: PropTypes.string,
onUserInput: PropTypes.func,
Expand All @@ -621,6 +651,7 @@ SelectElement.defaultProps = {
required: false,
sortByValue: true,
emptyOption: true,
autoSelect: true,
hasError: false,
errorMessage: 'The field is required!',
onUserInput: function() {
Expand Down
7 changes: 1 addition & 6 deletions modules/new_profile/php/new_profile.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,7 @@ class New_Profile extends \NDB_Form

// Get sites for the select dropdown
$user_list_of_sites = $user->getData('CenterIDs');
$num_sites = count($user_list_of_sites);
$psc_labelOptions = [];
if ($num_sites > 1) {
$psc_labelOptions = \Utility::getSiteNameListByIDs($user_list_of_sites);
}
$site = $psc_labelOptions;
$site = \Utility::getSiteNameListByIDs($user_list_of_sites);

// Get projects for the select dropdown
$projList = [];
Expand Down

0 comments on commit 34d1d5a

Please sign in to comment.