Skip to content

Commit

Permalink
fix(FormCheck)!: use feedbackType to control feedback type (#6015)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: FormCheck feedback type is now controlled by `feedbackType` instead of `isValid` and `isInvalid`.
  • Loading branch information
kyletsang committed Sep 6, 2021
1 parent 0765f8a commit ab9e080
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/Feedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import * as React from 'react';
import PropTypes from 'prop-types';
import { AsProp, BsPrefixRefForwardingComponent } from './helpers';

export type FeedbackType = 'valid' | 'invalid';

export interface FeedbackProps
extends AsProp,
React.HTMLAttributes<HTMLElement> {
// I think this is because we use BsPrefixRefForwardingComponent below
// which includes bsPrefix.
bsPrefix?: never;
type?: 'valid' | 'invalid';
type?: FeedbackType;
tooltip?: boolean;
}

Expand Down
11 changes: 5 additions & 6 deletions src/FormCheck.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import classNames from 'classnames';
import PropTypes from 'prop-types';
import * as React from 'react';
import { useContext, useMemo } from 'react';
import Feedback from './Feedback';
import Feedback, { FeedbackType } from './Feedback';
import FormCheckInput from './FormCheckInput';
import FormCheckLabel from './FormCheckLabel';
import FormContext from './FormContext';
Expand All @@ -22,6 +22,7 @@ export interface FormCheckProps
isInvalid?: boolean;
feedbackTooltip?: boolean;
feedback?: React.ReactNode;
feedbackType?: FeedbackType;
bsSwitchPrefix?: string;
}

Expand Down Expand Up @@ -127,6 +128,7 @@ const FormCheck: BsPrefixRefForwardingComponent<'input', FormCheckProps> =
isInvalid = false,
feedbackTooltip = false,
feedback,
feedbackType,
className,
style,
title = '',
Expand Down Expand Up @@ -181,11 +183,8 @@ const FormCheck: BsPrefixRefForwardingComponent<'input', FormCheckProps> =
{hasLabel && (
<FormCheckLabel title={title}>{label}</FormCheckLabel>
)}
{(isValid || isInvalid) && (
<Feedback
type={isValid ? 'valid' : 'invalid'}
tooltip={feedbackTooltip}
>
{feedback && (
<Feedback type={feedbackType} tooltip={feedbackTooltip}>
{feedback}
</Feedback>
)}
Expand Down
20 changes: 16 additions & 4 deletions test/FormCheckSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ describe('<FormCheck>', () => {
});

it('Should render valid feedback properly', () => {
const wrapper = mount(<FormCheck label="My label" isValid />);
const wrapper = mount(
<FormCheck label="My label" feedbackType="valid" feedback="test" />,
);
const feedback = wrapper.find('Feedback');

expect(feedback.prop('type')).to.equal('valid');
Expand All @@ -129,7 +131,7 @@ describe('<FormCheck>', () => {

it('Should render invalid feedback properly', () => {
const wrapper = mount(
<FormCheck label="My label" isValid={false} isInvalid />,
<FormCheck label="My label" feedbackType="invalid" feedback="test" />,
);
const feedback = wrapper.find('Feedback');

Expand All @@ -139,7 +141,12 @@ describe('<FormCheck>', () => {

it('Should render valid feedback tooltip properly', () => {
const wrapper = mount(
<FormCheck label="My label" isValid feedbackTooltip />,
<FormCheck
label="My label"
feedbackType="valid"
feedback="test"
feedbackTooltip
/>,
);
const feedback = wrapper.find('Feedback');

Expand All @@ -149,7 +156,12 @@ describe('<FormCheck>', () => {

it('Should render invalid feedback tooltip properly', () => {
const wrapper = mount(
<FormCheck label="My label" isValid={false} isInvalid feedbackTooltip />,
<FormCheck
label="My label"
feedbackType="invalid"
feedback="test"
feedbackTooltip
/>,
);
const feedback = wrapper.find('Feedback');

Expand Down
1 change: 1 addition & 0 deletions www/src/examples/Form/ValidationFormik.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function FormExample() {
onChange={handleChange}
isInvalid={!!errors.terms}
feedback={errors.terms}
feedbackType="invalid"
id="validationFormik0"
/>
</Form.Group>
Expand Down
1 change: 1 addition & 0 deletions www/src/examples/Form/ValidationNative.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function FormExample() {
required
label="Agree to terms and conditions"
feedback="You must agree before submitting."
feedbackType="invalid"
/>
</Form.Group>
<Button type="submit">Submit form</Button>
Expand Down
1 change: 1 addition & 0 deletions www/src/examples/Form/ValidationTooltips.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ function FormExample() {
onChange={handleChange}
isInvalid={!!errors.terms}
feedback={errors.terms}
feedbackType="invalid"
id="validationFormik106"
feedbackTooltip
/>
Expand Down
1 change: 1 addition & 0 deletions www/src/pages/migrating.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Below is a _rough_ account of the breaking API changes as well as the minimal ch
### FormCheck

- removed `bsCustomPrefix` and `custom` in favor of `bsSwitchPrefix`.
- feedback type is now controlled by `feedbackType` instead of `isValid` and `isInvalid`.

#### FormCheckInput

Expand Down

0 comments on commit ab9e080

Please sign in to comment.