Skip to content

Commit

Permalink
chore(Select,FormSelect,FormDropdown): use React.forwardRef() (#4408)
Browse files Browse the repository at this point in the history
* chore(Select): use React.forwardRef()

* chore(FormSelect): use React.forwardRef()

* chore(FormDropdown): use React.forwardRef()

* fix tests, fix Dropdown component

Co-authored-by: Oleksandr Fediashov <[email protected]>
  • Loading branch information
felixmosh and layershifter authored Dec 13, 2022
1 parent eeffde3 commit 88974b5
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/addons/Select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import Dropdown from '../../modules/Dropdown'
* @see Dropdown
* @see Form
*/
function Select(props) {
return <Dropdown {...props} selection />
}
const Select = React.forwardRef(function (props, ref) {
return <Dropdown {...props} selection ref={ref} />
})

Select.displayName = 'Select'
Select.propTypes = {
/** Array of Dropdown.Item props e.g. `{ text: '', value: '' }` */
options: PropTypes.arrayOf(PropTypes.shape(Dropdown.Item.propTypes)).isRequired,
Expand Down
7 changes: 4 additions & 3 deletions src/collections/Form/FormDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import FormField from './FormField'
* @see Dropdown
* @see Form
*/
function FormDropdown(props) {
const FormDropdown = React.forwardRef(function (props, ref) {
const { control } = props
const rest = getUnhandledProps(FormDropdown, props)
const ElementType = getElementType(FormDropdown, props)

return <ElementType {...rest} control={control} />
}
return <ElementType {...rest} control={control} ref={ref} />
})

FormDropdown.displayName = 'FormDropdown'
FormDropdown.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
7 changes: 4 additions & 3 deletions src/collections/Form/FormSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import FormField from './FormField'
* @see Form
* @see Select
*/
function FormSelect(props) {
const FormSelect = React.forwardRef(function (props, ref) {
const { control, options } = props
const rest = getUnhandledProps(FormSelect, props)
const ElementType = getElementType(FormSelect, props)

return <ElementType {...rest} control={control} options={options} />
}
return <ElementType {...rest} control={control} options={options} ref={ref} />
})

FormSelect.displayName = 'FormSelect'
FormSelect.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
8 changes: 7 additions & 1 deletion src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getUnhandledProps,
makeDebugger,
objectDiff,
setRef,
useKeyOnly,
useKeyOrValueAndKey,
} from '../../lib'
Expand Down Expand Up @@ -73,6 +74,11 @@ class DropdownInner extends Component {
sizerRef = createRef()
ref = createRef()

handleRef = (el) => {
this.ref.current = el
setRef(this.props.innerRef, el)
}

getInitialAutoControlledState() {
return { focus: false, searchQuery: '' }
}
Expand Down Expand Up @@ -1097,7 +1103,7 @@ class DropdownInner extends Component {
onFocus={this.handleFocus}
onChange={this.handleChange}
tabIndex={this.computeTabIndex()}
ref={this.ref}
ref={this.handleRef}
>
{this.renderLabels()}
{this.renderSearchInput()}
Expand Down
1 change: 1 addition & 0 deletions test/specs/addons/Select/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const requiredProps = {
describe('Select', () => {
common.isConformant(Select, { requiredProps })
common.hasSubcomponents(Select, [Dropdown.Divider, Dropdown.Header, Dropdown.Item, Dropdown.Menu])
common.forwardsRef(Select, { requiredProps })

it('renders a selection Dropdown', () => {
shallow(<Select {...requiredProps} />)
Expand Down
1 change: 1 addition & 0 deletions test/specs/collections/Form/FormDropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as common from 'test/specs/commonTests'
describe('FormDropdown', () => {
common.isConformant(FormDropdown, { ignoredTypingsProps: ['error'] })
common.labelImplementsHtmlForProp(FormDropdown)
common.forwardsRef(FormDropdown)

it('renders a FormField with a Dropdown control', () => {
shallow(<FormDropdown />)
Expand Down
1 change: 1 addition & 0 deletions test/specs/collections/Form/FormSelect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const requiredProps = {
describe('FormSelect', () => {
common.isConformant(FormSelect, { requiredProps, ignoredTypingsProps: ['error'] })
common.labelImplementsHtmlForProp(FormSelect, { requiredProps })
common.forwardsRef(FormSelect, { requiredProps })

it('renders a FormField with a Select control', () => {
shallow(<FormSelect {...requiredProps} />)
Expand Down
1 change: 1 addition & 0 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ describe('Dropdown', () => {
})

common.isConformant(Dropdown)
common.forwardsRef(Dropdown)
common.hasUIClassName(Dropdown)
common.hasSubcomponents(Dropdown, [
DropdownDivider,
Expand Down

0 comments on commit 88974b5

Please sign in to comment.