Skip to content

Commit

Permalink
fix(Checkbox): add positioning to checkbox input for native validatio…
Browse files Browse the repository at this point in the history
…n alignment (#10142)

* feat: add positioning to checkbox input

* feat: add validation example

Co-authored-by: Alessandra Davila <[email protected]>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 29, 2021
1 parent 9408054 commit 56cec53
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/components/src/components/checkbox/_checkbox.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
// to match the specs.
.#{$prefix}--checkbox {
@include hidden;
//even though its hidden, positioning is for native validation to be aligned to checkbox
top: 1.25rem;
left: 0.7rem;
}

// The label corresponds to the content inside of the `label` tag. Since we're
Expand Down
26 changes: 26 additions & 0 deletions packages/react/src/components/Checkbox/Checkbox-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import classNames from 'classnames';
import { action } from '@storybook/addon-actions';
import { withKnobs, boolean, text } from '@storybook/addon-knobs';
import Checkbox from '../Checkbox';
import Button from '../../components/Button';
import CheckboxSkeleton from '../Checkbox/Checkbox.Skeleton';
import { settings } from 'carbon-components';
import mdx from './Checkbox.mdx';
Expand All @@ -31,6 +32,31 @@ export default {
},
};

export const checkboxNativeValidation = () => {
function onSubmit(e) {
e.preventDefault();
var cb = e.target.querySelector('input[type=checkbox]');
if (cb.checked === false) {
cb.setCustomValidity('Privacy policy has to be accepted.');
cb.reportValidity();
return;
}

alert('submitted');
}

function onChange(e) {
e.target.setCustomValidity('');
}
return (
<form style={{ margin: '2em' }} onSubmit={onSubmit} onChange={onChange}>
<legend className={`${prefix}--label`}>Validation example</legend>
<Checkbox id="cb" labelText="Accept privacy policy" />
<Button type="submit">Submit</Button>
</form>
);
};

export const checkbox = () => {
return (
<fieldset className={`${prefix}--fieldset`}>
Expand Down
3 changes: 3 additions & 0 deletions packages/styles/scss/components/checkbox/_checkbox.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
// to match the specs.
.#{$prefix}--checkbox {
@include visually-hidden;
//even though its hidden, positioning is for native validation to be aligned to checkbox
top: 1.25rem;
left: 0.7rem;
}

// The label corresponds to the content inside of the `label` tag. Since we're
Expand Down

0 comments on commit 56cec53

Please sign in to comment.