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

#trivial : fix inputs eslint issues #1105

Merged
merged 5 commits into from
Mar 20, 2020
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
40 changes: 29 additions & 11 deletions react/components/Input/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react'
import React, { Component, createRef } from 'react'
import PropTypes from 'prop-types'

import Button from '../Button'
Expand All @@ -13,6 +13,8 @@ class Input extends Component {
this.state = {
active: false,
}

this.ref = createRef(null)
}

handleChange = event => {
Expand All @@ -32,17 +34,17 @@ class Input extends Component {
}

handleFocus = event => {
if (!this.props.readOnly) {
this.setState({ active: true })
this.props.onFocus && this.props.onFocus(event)
}
if (this.props.readOnly) return

this.setState({ active: true })
this.props.onFocus && this.props.onFocus(event)
}

handleBlur = event => {
if (!this.props.readOnly) {
this.setState({ active: false })
this.props.onBlur && this.props.onBlur(event)
}
if (this.props.readOnly) return

this.setState({ active: false })
this.props.onBlur && this.props.onBlur(event)
}

handleMouseEnter = event => {
Expand All @@ -53,6 +55,17 @@ class Input extends Component {
this.props.onMouseLeave && this.props.onMouseLeave(event)
}

handleAutoFocus = () => {
const { forwardedRef, autoFocus } = this.props
if (!autoFocus) return

if (forwardedRef && forwardedRef.current) {
forwardedRef.current.focus()
} else if (this.ref.current) {
this.ref.focus()
}
}

componentDidMount() {
if (this.props.size === 'x-large') {
console.warn(
Expand All @@ -71,6 +84,12 @@ class Input extends Component {
'You should not use both prefix and suffix props in the same input. '
)
}

this.handleAutoFocus()
matheusps marked this conversation as resolved.
Show resolved Hide resolved
}

componentDidUpdate() {
this.handleAutoFocus()
}

render() {
Expand Down Expand Up @@ -208,7 +227,7 @@ class Input extends Component {
)}
<input
{...dataAttrs}
ref={this.props.forwardedRef}
ref={this.props.forwardedRef || this.ref}
matheusps marked this conversation as resolved.
Show resolved Hide resolved
onBlur={this.handleBlur}
onFocus={this.handleFocus}
onChange={this.handleChange}
Expand All @@ -222,7 +241,6 @@ class Input extends Component {
accept={this.props.accept}
autoComplete={this.props.autoComplete}
autoCorrect={this.props.autoCorrect}
autoFocus={this.props.autoFocus}
autoSave={this.props.autoSave}
defaultValue={this.props.defaultValue}
inputMode={this.props.inputMode}
Expand Down
4 changes: 4 additions & 0 deletions react/components/InputCurrency/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ BaseInput.propTypes = {
}

const baseNumber = 9999999999.9999999999
const defaultEvent = { target: null }

class InputCurrency extends Component {
handleChange = ({ floatValue }) => {
const { onChange } = this.props
const ssr = typeof document === 'undefined' || typeof window === 'undefined'
const event = ssr ? defaultEvent : window.event

onChange &&
onChange({
...event,
Expand Down
19 changes: 17 additions & 2 deletions react/components/InputPassword/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ class InputPassword extends Component {
showPassword: false,
}

toggle = () => this.setState(state => ({ showPassword: !state.showPassword }))
handleToggle = () =>
this.setState(state => ({ showPassword: !state.showPassword }))

handleKeyPress = ({ key }) => {
const SPACE = ' '
const ENTER = 'Enter'
if (key === SPACE || key === ENTER) {
this.handleToggle()
}
}

render() {
const iconSize =
Expand All @@ -32,7 +41,13 @@ class InputPassword extends Component {
token
spellCheck="false"
suffix={
<span className="pointer pt2" onClick={() => this.toggle()}>
<span
className="pointer pt2"
onClick={this.handleToggle}
role="button"
tabIndex={0}
onKeyPress={this.handleKeyPress}
>
{this.state.showPassword ? (
<VisibilityOff solid size={iconSize} />
) : (
Expand Down
29 changes: 24 additions & 5 deletions react/components/InputSearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,19 @@ class InputSearch extends Component {
...event.target,
value,
},
preventDefault: event.preventDefault || (() => {}),
preventDefault: event.preventDefault || (() => null),
})
}

handleKeyPress = (event, handler) => {
const SPACE = ' '
const ENTER = 'Enter'
const { key } = event
if (key === SPACE || key === ENTER) {
handler(event)
}
}

handleHovering = hover => {
this.setState({ hover })
}
Expand All @@ -60,6 +69,8 @@ class InputSearch extends Component {
this.setState({ focus })
}

pressedEnter = event => event.key === 'Enter'

render() {
const { hover, focus } = this.state
const { size } = this.props
Expand All @@ -73,15 +84,17 @@ class InputSearch extends Component {
onBlur={() => this.handleFocus(false)}
onMouseEnter={() => this.handleHovering(true)}
onMouseLeave={() => this.handleHovering(false)}
onKeyUp={e => e.key === 'Enter' && this.handleSubmit(e)}
onKeyUp={e => this.pressedEnter(e) && this.handleSubmit(e)}
matheusps marked this conversation as resolved.
Show resolved Hide resolved
type="search"
suffix={
<div className="flex flex-row items-center">
{this.props.value && (
<span
tabIndex={0}
onClick={this.handleClickClear}
className="pointer mr4 c-muted-3"
onClick={this.handleClickClear}
onKeyPress={e => this.handleKeyPress(e, this.handleClickClear)}
role="button"
tabIndex={0}
>
<ClearIcon size={iconSize} />
</span>
Expand All @@ -96,7 +109,13 @@ class InputSearch extends Component {
InputSearch.separatorHeight.regular,
}}
/>
<span className="pointer pl4 c-link" onClick={this.handleSubmit}>
<span
className="pointer pl4 c-link"
onClick={this.handleSubmit}
onKeyPress={e => this.handleKeyPress(e, this.handleSubmit)}
role="button"
tabIndex={0}
>
<SearchIcon size={iconSize} />
</span>
</div>
Expand Down