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

I created two TextInputs, both update simultaneously #36

Closed
anotherchrisatwork opened this issue Jun 27, 2019 · 3 comments
Closed

I created two TextInputs, both update simultaneously #36

anotherchrisatwork opened this issue Jun 27, 2019 · 3 comments

Comments

@anotherchrisatwork
Copy link

Both are rendered, but when the code runs, there is a cursor in each one, and whatever I type shows up in both. There also doesn't seem to be any way to change focus (sorry, I'm new to Ink!).

Here's the code. There are two files, index.js:

import React from 'react'
import { render } from 'ink'

import App from './App'

try {
  render(<App />)
} catch (err) {
  console.log('index.js main caught', err)
}

And App.js, where the two TextInputs are created and used:

import React from 'react'
import { Box } from 'ink'
import TextInput from 'ink-text-input'

class NumberInput extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      entry: '',
    }

    this.handleChange = this.handleChange.bind(this)
  }

  render() {
    return (
      <Box>
        <Box marginRight={1}>{this.props.title}</Box>

        <TextInput value={this.state.entry} onChange={this.handleChange} />
      </Box>
    )
  }

  handleChange(entry) {
    this.setState({ entry })
    try {
      const num = Number(entry)
      if (!Number.isNaN(num)) {
        this.props.setValue(num)
      }
    } catch (err) {
      console.log('NumberInput.handleChange caught', err)
    }
  }
}

class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      first: 0,
      second: 0,
    }

    this.setFirst = this.setFirst.bind(this)
    this.setSecond = this.setSecond.bind(this)
  }

  setFirst(val) {
    this.setState({ first: val })
  }

  setSecond(val) {
    this.setState({ second: val })
  }

  render() {
    return (
      <Box flexDirection="column">
        <Box>Adder</Box>
        <NumberInput
          key={'first'}
          setValue={this.setFirst}
          title="First number:"
        />
        <NumberInput
          key={'second'}
          setValue={this.setSecond}
          title="Second number:"
        />

        <Box>Sum: {this.state.first + this.state.second}</Box>
      </Box>
    )
  }
}

export default App
@anotherchrisatwork anotherchrisatwork changed the title I created two TextInputs, both update simultaenously I created two TextInputs, both update simultaneously Jun 27, 2019
@vadimdemedes
Copy link
Owner

vadimdemedes commented Jun 28, 2019

There's actually focus={true/false} prop, which I think got erased from readme accidentally:

<TextInput focus={false}/>

If you pass false, user input will be ignored for this component. This will allow you to have multiple text inputs at the same time.

@anotherchrisatwork
Copy link
Author

Cool, thanks, that works! And I guess I have to go to rawmode to allow watching for a tab character (or figure out some other way) to allow the user to switch focus?

@vadimdemedes
Copy link
Owner

Exactly, user input without raw mode isn't possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants