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

Forwarding refs to <Input>-component in ffe-form-react #231

Closed
paandahl opened this issue May 14, 2018 · 5 comments
Closed

Forwarding refs to <Input>-component in ffe-form-react #231

paandahl opened this issue May 14, 2018 · 5 comments
Assignees

Comments

@paandahl
Copy link
Contributor

paandahl commented May 14, 2018

Which package is this issue related to: ffe-form-react

Describe your issue (screenshots welcome!)

This is more of a technical feature request. I am working on another internal component at SB1, that adds managed formatting to an input. I want to use the Input from ffe-form, but need to access the DOM ref to manage selection/cursor position. I currently achieve this with ReactDOM.findDOMNode().

There are two potential ways to improve this:

  1. Give Input a new property inputRef, that takes a callback and passes it to ref on the contained .

  2. Upgrade to React 16.3.0 and use the new forwardRef API.

If there is consensus on such a change, I could make a PR.

BR, Preben Ludviksen

@paandahl paandahl changed the title Forwarding refs to <Input>-component in ffe-form Forwarding refs to <Input>-component in ffe-form-react May 14, 2018
@wkillerud
Copy link
Contributor

Input spreads all given props except className, inline, and textLike (which are used to build the className value) to the input element, so I think what you need is supported already.

const Input = ({ className, inline, textLike, ...rest }) => {
    return (
        <input
            className={classNames(
                'ffe-input-field',
                { 'ffe-input-field--inline': inline },
                { 'ffe-input-field--text-like': textLike },
                className,
            )}
            {...rest} {/* <-- This stuff right here */}
        />
    );
};

So if you set a ref property on Input in your code you should get the ref for the input element.

<Input
   inline={true}
   textLike={true}
   ref={_ref => { this._refs.input = _ref; }}
/>

@ivarni
Copy link
Contributor

ivarni commented May 14, 2018

I thought the ref prop was kinda magic and never actually passed to the component, like key?

@wkillerud
Copy link
Contributor

Oh, I see.

Then it would be nice to use the new forwardRef feature from React to avoid reinventing the wheel with a custom prop. That means a breaking change, but it's a no-hassle upgrade so it should be fine I'd say :)

@selbekk
Copy link
Contributor

selbekk commented May 22, 2018

+1 on the forwardRef approach. I'll submit a PR shortly - I have a few minutes to spare anyways, and I've longed for an excuse to try out this API =)

@selbekk selbekk self-assigned this May 22, 2018
selbekk added a commit that referenced this issue May 22, 2018
BREAKING CHANGE: Requires React 16.3.0 and above!

This commit lets the consumer pass refs down to the `Input` and
`TextArea` components, to handle focus et al.

The only breaking change is the fact that React 16.3.0 is required.

Fixes #231.
selbekk added a commit that referenced this issue May 22, 2018
BREAKING CHANGE: Requires React 16.3.0 and above!

This commit lets the consumer pass refs down to the `Input` and
`TextArea` components, to handle focus et al.

The only breaking change is the fact that React 16.3.0 is required.

Fixes #231.
selbekk added a commit that referenced this issue May 31, 2018
BREAKING CHANGE: Requires React 16.4.0 and above!

This commit lets the consumer pass refs down to the `Input` and
`TextArea` components, to handle focus et al.

The only breaking change is the fact that React 16.4.0 is required.

This commit also disables two test suites, since Enzyme doesn't yet support
React's newest versions. For more information, please refer to this
issue:

Fixes #231.
@selbekk
Copy link
Contributor

selbekk commented May 31, 2018

So I've fixed this in the PR, but enzyme (our React testing framework) doesn't support the feature yet.

I've fixed this temporarily by disabling tests for two components, but I'm not sure if this is good enough for a merge. The fix is merged to master, but not released yet (hasn't been for 3 months).

selbekk added a commit that referenced this issue Jun 6, 2018
BREAKING CHANGE: Requires React 16.4.0 and above!

This commit lets the consumer pass refs down to the `Input` and
`TextArea` components, to handle focus et al.

The only breaking change is the fact that React 16.4.0 is required.

This commit also disables two test suites, since Enzyme doesn't yet support
React's newest versions. For more information, please refer to this
issue:

Fixes #231.
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

4 participants