Skip to content

Commit

Permalink
Merge pull request #241 from dnbexperience/fix/input
Browse files Browse the repository at this point in the history
fix: fix #input and #textarea empty value change
  • Loading branch information
tujoworker authored Aug 6, 2019
2 parents e10750f + 10efba8 commit 08bb0df
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 42 deletions.
9 changes: 3 additions & 6 deletions packages/dnb-ui-lib/src/components/input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,7 @@ export default class Input extends PureComponent {

static getDerivedStateFromProps(props, state) {
const value = Input.getValue(props)
if (
state._listenForPropChanges &&
value !== null &&
value !== state.value
) {
if (state._listenForPropChanges && value !== state.value) {
state.value = value
}
if (props.input_state) {
Expand Down Expand Up @@ -222,8 +218,9 @@ export default class Input extends PureComponent {
dispatchCustomElementEvent(this, 'on_change', { value, event })
}
onKeyDownHandler = event => {
const value = event.target.value
dispatchCustomElementEvent(this, 'on_key_down', { value, event })
if (event.key === 'Enter') {
const { value } = event.target
dispatchCustomElementEvent(this, 'on_submit', { value, event })
}
}
Expand Down
53 changes: 47 additions & 6 deletions packages/dnb-ui-lib/src/components/input/__tests__/Input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,64 @@ describe('Input component', () => {
'false'
)

const value = 'new value'
Comp.find('input').simulate('change', { target: { value } })
const newValue = 'new value'
Comp.find('input').simulate('change', { target: { value: newValue } })

expect(Comp.find('.dnb-input__shell').prop('data-has-content')).toBe(
'true'
)

expect(Comp.state().value).toBe(value)
expect(Comp.state().value).toBe(newValue)
})

it('events gets emmited correctly: "on_change" and "onKeyDown"', () => {
const initValue = 'init value'
const newValue = 'new value'
const emptyValue = null // gets emmited also on values as null

const on_change = jest.fn()
const onKeyDown = jest.fn() // additional native event test

const Comp = mount(
<Component
{...props}
value={initValue}
on_change={on_change}
onKeyDown={onKeyDown} // additional native event test
/>
)

expect(Comp.state().value).toBe(initValue)

Comp.find('input').simulate('change', {
target: { value: newValue }
})
expect(on_change.mock.calls.length).toBe(1)
expect(Comp.state().value).toBe(newValue)

Comp.find('input').simulate('change', {
target: { value: emptyValue }
})
expect(on_change.mock.calls.length).toBe(2)
expect(Comp.state().value).toBe(emptyValue)

// additional native event test
Comp.find('input').simulate('keydown', { key: 'Space', keyCode: 84 }) // space
expect(onKeyDown.mock.calls.length).toBe(1)
})

// make sure getDerivedStateFromProps works
it('has correct state after changeing "value" prop (set by getDerivedStateFromProps)', () => {
const value = 'new prop value'
const initValue = 'new prop value'
const emptyValue = null

Comp.setProps({
value
value: initValue
})
expect(Comp.state().value).toBe(value)
expect(Comp.state().value).toBe(initValue)

Comp.setProps({ value: emptyValue })
expect(Comp.state().value).toBe(emptyValue)

// get dom node
// console.log('domNode', Comp.find('input').getDOMNode().value)
Expand Down
14 changes: 5 additions & 9 deletions packages/dnb-ui-lib/src/components/textarea/Textarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export const propTypes = {
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
id: PropTypes.string,
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.node
]),
PropTypes.string,
PropTypes.func,
PropTypes.node
]),
label_direction: PropTypes.oneOf(['horizontal', 'vertical']),
status: PropTypes.oneOfType([
PropTypes.string,
Expand Down Expand Up @@ -119,11 +119,7 @@ export default class Textarea extends PureComponent {

static getDerivedStateFromProps(props, state) {
const value = Textarea.getValue(props)
if (
state._listenForPropChanges &&
value !== null &&
value !== state.value
) {
if (state._listenForPropChanges && value !== state.value) {
state.value = value
}
if (isTrue(props.disabled)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,16 @@ describe('Textarea component', () => {

// // make sure getDerivedStateFromProps works
it('has correct state after changeing "value" prop (set by getDerivedStateFromProps)', () => {
const value = 'new prop value'
const initValue = 'new prop value'
const emptyValue = null

Comp.setProps({
value
value: initValue
})
expect(Comp.state().value).toBe(value)
expect(Comp.state().value).toBe(initValue)

Comp.setProps({ value: emptyValue })
expect(Comp.state().value).toBe(emptyValue)
})

it('has to to have a prop value like value', () => {
Expand Down
51 changes: 33 additions & 18 deletions packages/dnb-ui-lib/stories/components/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
*/

import React /* , { useState, useEffect } */ from 'react'
import React, { useState, useEffect } from 'react'
import { Wrapper, Box } from '../helpers'
import styled from '@emotion/styled'

Expand Down Expand Up @@ -181,27 +181,12 @@ export default [
<Box>
<form
onSubmit={event => {
console.log('onSubmit', event)
// console.log('onSubmit', event)
event.preventDefault()
// event.persist()
}}
>
<Input
label="Label:"
on_change={event => {
console.log('on_change', event)
}}
onChange={event => {
console.log('onChange', event)
}}
on_submit={event => {
console.log('on_submit', event)
}}
onSubmit={event => {
console.log('on_submit', event)
}}
value="Input ..."
/>
<InputUpdate />
<Button
text="Submit"
type="submit"
Expand All @@ -218,3 +203,33 @@ export default [
</CustomStyle>
)
]

const InputUpdate = () => {
const [initValue, setNewValue] = useState('Input ...')

useEffect(() => {
const timeoutId = setTimeout(() => {
setNewValue('')
}, 1e3)
return () => clearTimeout(timeoutId)
})

return (
<Input
label="Label:"
on_change={({ value }) => {
console.log('on_change', value)
}}
onChange={({ value }) => {
console.log('onChange', value)
}}
on_submit={({ value }) => {
console.log('on_submit', value)
}}
onSubmit={({ value }) => {
console.log('onSubmit', value)
}}
value={initValue}
/>
)
}

0 comments on commit 08bb0df

Please sign in to comment.