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

Width problem for placeholder #73

Open
mhlmhl opened this issue Jan 10, 2017 · 6 comments
Open

Width problem for placeholder #73

mhlmhl opened this issue Jan 10, 2017 · 6 comments

Comments

@mhlmhl
Copy link

mhlmhl commented Jan 10, 2017

I'm using AutosizeInput with TagsInput and these inputProps:

inputProps={{
  className: 'react-tagsinput-input',
   placeholder: 'Add a theme',
}}

The props that are actually passed to AutosizeInput are:

type='text'
onChange={onChange}
value={value}
className : "react-tagsinput-input"
onBlur: ()
onFocus: ()
onKeyDown: ()
onPaste: ()
placeholder: "Add a theme"
ref: "input"

My problem: on initial render, when 'value' is empty, the input box is only a couple of pixels wide. I believe this is because the computation of 'sizerValue' in the AutosizeInput render function does not consider the placeholder.

As a temporary fix, I added the following. But it's a hack because (a) it does not consider that the font could be wider or narrower, and (b) it defeats the autosizing function for tags that are shorter than 90 px.

minWidth: 90

@cgat
Copy link

cgat commented Oct 2, 2017

I noticed this happening when my element was originally being rendered with display: none. In this case, the placeholder does not have a length and the element length fails to get set properly.

@ericgio
Copy link

ericgio commented Oct 27, 2017

I'm seeing this issue as well, both when a parent is initially rendered with display: none and when props change that should affect the input size. Even when forcing a prop change to the input, however, I'm not seeing it update as I would expect.

@JedWatson
Copy link
Owner

If someone can provide a link (e.g. to codesandbox) or adds an example that reproduces this I'd be happy to take a look at it

@ericgio
Copy link

ericgio commented Nov 28, 2017

@JedWatson: From what I can tell, the issue is due to input styles only being set in componentDidMount. Moving that line here seems to solve the problem.

Happy to submit a PR for this, just wasn't sure if there was a specific reason you were only setting the styles once.

@thomaslc66
Copy link

thomaslc66 commented Apr 27, 2019

Hello @JedWatson I have the same problem. Here you can find some of my implementation.
It's a really wierd behavior.

I have 2 lists that used the same method to render a input. They Both called renderColumn
First list is ok, second list isn't. I use a tab to switch between first list (password) and second (licence).
All state is in redux as i use redux-form.

const renderColumn = props => {
  const {
    index,
    renderToaster,
    input: { name, onChange, value },
    placeholder,
    ...restProps
  } = props;
  return (
    <Column
      onChange={onChange}
      renderToaster={renderToaster}
      text={value}
      maxLength={50}
      name={name}
      placeholder={placeholder}
      restProps={restProps}
    />
  );
};
<MDBTableBody>
       {fields.map((data, index) => {
            return (
              <tr key={"key_" + index}>
                <DeleteRow deleteARow={() => fields.remove(index)} />
                <Field
                  name={`${data}.login`}
                  component={renderColumn}
                  renderToaster={renderToaster}
                  placeholder={LOGIN}
                />
                <Field
                  name={`${data}.password`}
                  component={renderColumn}
                  renderToaster={renderToaster}
                  placeholder={PASSWORD}
                />
                <Field
                  name={`${data}.account`}
                  component={renderColumn}
                  renderToaster={renderToaster}
                  placeholder={ACCOUNT}
                />
              </tr>
            );
          })}
</MDBTableBody>
<MDBTableBody>
          {fields.map((data, index) => {
            return (
              <tr key={"key_" + index}>
                <DeleteRow deleteARow={() => fields.remove(index)} />
                <Field
                  name={`${data}.buyDate`}
                  component={renderColumn}
                  renderToaster={renderToaster}
                  placeholder={BUYDATE}
                />
                <Field
                  name={`${data}.key`}
                  component={renderColumn}
                  renderToaster={renderToaster}
                  placeholder={KEY}
                />
                <Field
                  name={`${data}.program`}
                  component={renderColumn}
                  renderToaster={renderToaster}
                  placeholder={PROGRAM}
                />
                <Field
                  name={`${data}.computer`}
                  component={renderColumn}
                  renderToaster={renderToaster}
                  placeholder={COMPUTER}
                />
              </tr>
            );
          })}
</MDBTableBody>
//render method of the Column Component
render() {
    const {
      text,
      indexPath,
      status,
      name,
      index,
      maxLength,
      onClick,
      placeholder
    } = this.props;
    return (
      <td className='align-vertical'>
        <AutosizeInput
          className='list_input'
          disabled={status}
          onClick={onClick}
          value={text}
          name={name}
          onChange={e => {
            this.props.onChange(e, indexPath);
          }}
          index={index}
          maxLength={maxLength}
          placeholder={placeholder}
        />
        <MDBIcon
          far
          icon='clone'
          className={
            this.state.isHovered ? "icon_clone_hover" : "icon_clone text-muted"
          }
          onClick={() => this.copyText(text)}
          onMouseEnter={this.handleHover}
          onMouseLeave={this.handleHover}
        />
      </td>
    );
  }
}

Here you can see, first line is not ok, but when i click on "Nouveau" button, the new line is ok.

image

Don't know if this can help you. But when i move my mouse hover the copy icon, then the autosize input take the good size. (my hover method only change the cursor of the mouse to display a hand cursor)

Hope it will help.

Edit:
Here is a picture of the state of the component:
image

@ozluy
Copy link

ozluy commented Mar 6, 2020

I just created a new NPM module please check it out:

  • 252 byte size only 10% of react-input-autosize (2.3KB)

https://github.com/ozluy/calculate-text-width
image

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

6 participants