Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

[terra-form-select] Native Select allow initial values #3829

Merged
merged 8 commits into from
Jul 3, 2023
2 changes: 2 additions & 0 deletions packages/terra-core-docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## Unreleased
* Added
* Added test example for controlled form native select component.

* Changed
* Updated `terra-status-view - Change Variant` example component to use the apply button to change the selected variant.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { useState } from 'react';
import classNames from 'classnames/bind';
import NativeSelect from 'terra-form-select/lib/native-select/NativeSelect';
import Button from 'terra-button';
import styles from './NativeSelectTest.module.scss';

const cx = classNames.bind(styles);

const ControlledSelect = () => {
const [value, setValue] = useState(null);

return (
<div className={cx('test-shell')}>
<NativeSelect
ariaLabel="Controlled Select Example"
id="test-select-id"
onChange={event => setValue(event.currentTarget.value)}
options={[
{ value: 'volvo', display: 'Volvo' },
{ value: 'saab', display: 'Saab' },
{ value: 'mercedes', display: 'Mercedes' },
{ value: 'audi', display: 'Audi' },
]}
value={value}
className={cx('form-select')}
/>
<Button id="test-button-id" text="Set Mercedes" onClick={() => setValue('mercedes')} />
</div>
);
};

export default ControlledSelect;
5 changes: 5 additions & 0 deletions packages/terra-form-select/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## Unreleased
* Fixed
* Fixed `Native Select` to allow updating the value prop in subsequent renders.

* Added
* Added WDIO screenshots for `Native Select` with initial value as null and without null.
MadanKumarGovindaswamy marked this conversation as resolved.
Show resolved Hide resolved

## 6.41.0 - (May 9, 2023)

Expand Down
14 changes: 11 additions & 3 deletions packages/terra-form-select/src/native-select/NativeSelect.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {
useState,
useRef,
useEffect,
} from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
Expand Down Expand Up @@ -167,9 +168,16 @@ const NativeSelect = ({
...customProps
}) => {
const [uncontrolledValue, setUncontrolledValue] = useState(!isValuePresent(value) ? defaultValue || getFirstValue(options, isFilterStyle) : undefined);
const refIsControlled = useRef(isValuePresent(value));
const refSelect = useRef();
const theme = React.useContext(ThemeContext);
const refIsControlled = isValuePresent(value);

useEffect(() => {
// added this to allow initial null values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this condition was not added to allow initial null values !!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

if (value) {
setUncontrolledValue('');
}
}, [value]);

// The native select's presentation is masked to allow for better customization of the inputs display.
// In order to facilitate this, the mouseDown, blur, and focus events need to be mapped mapped to the mask.
Expand All @@ -194,15 +202,15 @@ const NativeSelect = ({
}
};
const handleOnChange = event => {
if (!refIsControlled.current) {
if (!refIsControlled) {
setUncontrolledValue(event.currentTarget.value);
}
if (onChange) {
onChange(event);
}
};

let currentValue = refIsControlled.current ? value : uncontrolledValue;
let currentValue = refIsControlled ? value : uncontrolledValue;
let currentDisplay = getDisplay(currentValue, options, isFilterStyle, intl);
if (!currentDisplay) {
currentValue = getFirstValue(options, isFilterStyle);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions packages/terra-form-select/tests/wdio/native-select-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,18 @@ Terra.describeViewports('Native Select', ['tiny'], () => {
Terra.validates.element('filter-style-hover-and-focus');
});
});

describe('Controlled Select', () => {
before(() => browser.url('/raw/tests/cerner-terra-core-docs/form-select/native-select/controlled-select'));

it('native select with null value', () => {
Terra.validates.element('controlled-select-null');
});

it('should set Mercedes value on click', () => {
$('#test-button-id').click();

MadanKumarGovindaswamy marked this conversation as resolved.
Show resolved Hide resolved
Terra.validates.element('controlled-select-value');
});
});
});