Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Input): expose select() method from ref #2928

Merged
merged 3 commits into from
Jul 6, 2018
Merged
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
2 changes: 2 additions & 0 deletions src/elements/Input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class Input extends Component {

focus = () => this.inputRef.focus()

select = () => this.inputRef.select()

handleChange = (e) => {
const value = _.get(e, 'target.value')

Expand Down
53 changes: 37 additions & 16 deletions test/specs/elements/Input/Input-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,14 @@ describe('Input', () => {

common.propKeyAndValueToClassName(Input, 'actionPosition', ['left'], { className: 'action' })
common.propKeyAndValueToClassName(Input, 'iconPosition', ['left'], { className: 'icon' })
common.propKeyAndValueToClassName(Input, 'labelPosition', ['left', 'right', 'left corner', 'right corner'], {
className: 'labeled',
})
common.propKeyAndValueToClassName(
Input,
'labelPosition',
['left', 'right', 'left corner', 'right corner'],
{
className: 'labeled',
},
)

common.propKeyOnlyToClassName(Input, 'action')
common.propKeyOnlyToClassName(Input, 'disabled')
Expand Down Expand Up @@ -113,13 +118,9 @@ describe('Input', () => {
const wrapper = shallow(<Input {...{ [propName]: propValue }} />)

// account for overloading the onChange prop
const expectedValue = propName === 'onChange'
? wrapper.instance().handleChange
: propValue
const expectedValue = propName === 'onChange' ? wrapper.instance().handleChange : propValue

wrapper
.find('input')
.should.have.prop(propName, expectedValue)
wrapper.find('input').should.have.prop(propName, expectedValue)
})

it(`passes \`${propName}\` to the <input> when using children`, () => {
Expand All @@ -131,13 +132,9 @@ describe('Input', () => {
)

// account for overloading the onChange prop
const expectedValue = propName === 'onChange'
? wrapper.instance().handleChange
: propValue
const expectedValue = propName === 'onChange' ? wrapper.instance().handleChange : propValue

wrapper
.find('input')
.should.have.prop(propName, expectedValue)
wrapper.find('input').should.have.prop(propName, expectedValue)
})
})
})
Expand All @@ -158,6 +155,25 @@ describe('Input', () => {
})
})

describe('select', () => {
it('can be set via a ref', () => {
const mountNode = document.createElement('div')
document.body.appendChild(mountNode)

const value = 'expect this text to be selected'
const wrapper = mount(<Input value={value} />, { attachTo: mountNode })
wrapper.instance().select()

window
.getSelection()
.toString()
.should.equal(value)

wrapper.detach()
document.body.removeChild(mountNode)
})
})

describe('loading', () => {
it("don't add icon if it's defined", () => {
shallow(<Input icon='user' loading />)
Expand Down Expand Up @@ -210,7 +226,12 @@ describe('Input', () => {
const mountNode = document.createElement('div')
document.body.appendChild(mountNode)

const wrapper = mount(<Input><input ref={ref} /></Input>, { attachTo: mountNode })
const wrapper = mount(
<Input>
<input ref={ref} />
</Input>,
{ attachTo: mountNode },
)
const input = document.querySelector('.ui.input input')

ref.should.have.been.calledOnce()
Expand Down