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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Form.Check.Label
spacing
#5983
Fix Form.Check.Label
spacing
#5983
Conversation
Another thing I just thought of is that react-bootstrap/src/FormCheck.tsx Lines 181 to 186 in 3ff951b
...since the presence of any children will prevent a prop-generated label from rendering. Is that something we want to support? |
Wonder if we should allow bools for @jquense this might be a cleaner approach? |
Do you mean requiring the user to pass |
Also I just thought of one way to allow users to use a |
Yeah, so when using custom rendering, setting
|
Right on. As a user, I'd prefer |
@jquense your thoughts on this? |
And FWIW, only the changes from the first two commits are required to fix the |
Currently, when using a `Form.Check.Label` component to customize `Form.Check` rendering, there will be no space between the checkbox and the label. This is because `Form.Check` is currently relying on the presence of a `label` prop to apply the `form-check` class name to the wrapping `<div>`, because checkboxes without labels [don't need the wrapping element to have the `form-check` class name][1]. This commit adds a utility to check whether an element has a child of a certain type. It then uses that utility to check if a `Form.Check` element has a `Form.Check.Label` child and takes that into account when determining whether the checkbox has a label. Adding a special property (currently called `typeName`, but that can certainly change) to components for this utility is necessary because React minifies the `displayName` property in production. [1]: react-bootstrap#5938 (comment)
Currently, `Form.Check` doesn't allow users to use a custom `Form.Check.Input` with a label generated by the `label` prop (and requires that a custom `Form.Check.Input` be used if a custom `Form.Check.Label` is also used) because the presence of _any_ children in `Form.Check` prevents automatic inputs and labels (and feedback) from generating. This approach allows mixing and matching by extracting any custom input, label, and feedback components from `Form.Check`'s `children` prop, then separately for each, using the custom component if provided or an automatic component otherwise.
5822d94
to
3b7a225
Compare
I see a stable v2.0.0 was released with @kyletsang, @jquense, is there anything I can do to help get this fix out the door? I'm willing to refactor to make I've rebased this PR onto the latest |
@jquense, could we check something like |
The `type` property of React elements have reference equality with matching imported component variables (`JSXElementConstructor` objects, to be precise)! For some reason I was not expecting that, but it makes sense to me now, since these constructor objects are being imported from a singular source, so there's no reason React would create multiple instances of them. So `child.type === type` all we need to check in `getChildOfType`; the `typeName` prop I added earlier is not at all needed.
Just as a convenience. I can revert this commit if these scripts aren't desired.
ce25d71
to
5343b0a
Compare
@kyletsang You know what, I just learned to my surprise that the I've pushed a commit greatly simplifying |
src/FormCheck.tsx
Outdated
)} | ||
</> | ||
)} | ||
{inputElement} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would force the elements to render in a specific order even if the user specified otherwise. For ex:
<Form.Check type={type} id={`check-api-${type}`}>
<Form.Control.Feedback type="valid">You did it!</Form.Control.Feedback>
<Form.Check.Label>{`Custom api ${type}`}</Form.Check.Label>
<Form.Check.Input type={type} isValid />
</Form.Check>
would render
<div class="form-check">
<input type="radio" id="check-api-radio" class="form-check-input is-valid">
<label for="check-api-radio" class="form-check-label">Custom api radio</label>
<div class="valid-feedback">You did it!</div>
</div>
You could probably simplify this further to just check the for the label node if children is specified. Maybe like this?
|
Checking type on an element is a bit fraught. It doesn't end up working as nicely as you'd expect. We actually used to do this in a number of components. The problem is that lots of things wrap components.
As a "best effort" its ok but it's not consistent |
Thanks for the great feedback, @kyletsang and @jquense! How do things look after this latest commit? I've stopped trying to force the custom render components into a certain order, and now the only thing this PR adds to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be ok since it covers the most general case?
Your call @jquense 馃槃
@TyMick can you remove the changes in both the package.json files? We don't need those. I'll merge after |
sorry i've been so absent. seems ok with me, certainly don't block on me |
@kyletsang Sounds good! I found them useful for testing the production build, but they certainly aren't necessary. 馃憤馃徏 |
Thanks! And sorry for the delays 馃槄 |
No worries; plenty of those delays were on my end anyway. 馃槉 |
Fixes #5938. Currently, when using a
Form.Check.Label
component to customizeForm.Check
rendering, there will be no space between the checkbox and the label. This is becauseForm.Check
is currently relying on the presence of alabel
prop to apply theform-check
class name to the wrapping<div>
, because checkboxes without labels don't need the wrapping element to have theform-check
class name.This PR adds a utility to check whether an element has a child of a certain type. It then uses that utility to check if a
Form.Check
element has aForm.Check.Label
child and takes that into account when determining whether the checkbox has a label.Adding a special property (currently called
typeName
, but that can certainly change) to components for this utility is necessary because React minifies thedisplayName
property in production.Here's a side-by-side screenshot of the current bug as shown in the documentation site, compared to the fixed version when I run the docs locally after this fix:
EDIT: Only the changes from the first two commits are required to fix this spacing. The third commit adds a related feature that I maybe should've saved for a separate PR. 馃檲