twisty / formsy-react-components

Bootstrap components for a formsy-react form.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clicking labels does not focus form elements

aphillipo opened this issue · comments

htmlFor does not work because "for" in html links with id attributes and not name of the form. If id is not specified we could default it to be the name, possibly prefixed?

Just an idea anyway!

Good catch. I actually thought it did this already! Must get around to writing tests and docs!

Your idea of using the element name for an ID is sound, although this might get a bit hairy as I think name properties might be getting a bit more complex.

Where no ID is supplied, perhaps we should just generate a UID for the element ID?

Yeah I suppose you could do this... I hate the idea of UUIDs floating round in the markup but it's all change in the world of react :-D Go for it!

Be aware that I've had incredibly strange results in the past with duplicate ids in the same document so this must be something to avoid. I would personally just convert the name into something sensible i.e. addresses[0][street] -> address_0_street would be a fine and descriptive id and no need to come up with UUID generation stuff.

"addresses[0][street]".split('[').join('_').replace(']', ''); // might be all you need.

That should do it, thanks! Ideally I'd prepend a unique ID of the enclosing form, but not sure how to do that at the moment without making the parent-context mixin mandatory. So for now I'll append a hash of the component's props for added uniqueness.

I've got mixed feelings on the aesthetics of generated IDs in markup. Perhaps it's better that they look machine-generated so they're differentiated from hand crafted markup that's more likely to embody the intent of the author. Maybe having the name slug and bit of uniqueness is the happy medium!

Thanks for that! Works great 👍

Another option is to wrap the input with a label, without htmlFor and unique ID:

<label><span>Name</span> <input type="text" /></label>

It works for me! We try to generate code as close as possible to that in
the Bootstrap docs, so unlikely to change to your suggestion (although I'm
sure it works!). Maybe post a snippet of the generated code that isn't
working.

On Thu, 30 Jun 2016 at 19:06 Elad Ossadon notifications@github.com wrote:

Another option is to wrap the input with a label, without htmlFor and
unique ID:

Name


You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
#13 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAA_dGDc8INJ42WT0tY5ykM65l2cAxBsks5qRAWggaJpZM4FEtXd
.

Oops sorry @elado, excuse the curt reply. I read your comment in email and thought it was a new issue.

Clicking labels to focus the form control should work out-of-the-box with these components. If they don't, feel free to open another issue outlining the circumstances where there is a problem.

In some cases -- like checkbox groups -- we style a "fake label" to identify a group of form controls, in this case clicking the "fake label" won't focus the group, but individual labels within that group should work okay (I think these use the pattern you have outlined above).

Was this issue fixed? My form isn't focusing it when using React syntax.

<div className="form-group">
          <label htmlFor="email" className="h4">Target email:</label>
          <input type="email" name="email" className="form-control" />
</div>

Can you show us the react code you are using - the example there you are giving does not appear to use formsy-react-components but rather the internal base react versions that map to pure HTML elements.

If you are using formsy-react then you should try requiring or including say an Input element:

var FRC = require('formsy-react-components'),
    Input = FRC.Input;

Then in your render method we can do something like:

<Formsy.Form>
<Input type="email" name="email" label="Target Email:" validations="isEmail" required />
</Formsy.Form>

This will generate all the bootstrap stuff for you. Maybe read the docs :^)

I apologize, direct organic search brought me here and I didn't read what repo this was from.