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

[CORL-1080] Final Form Array Fix #2965

Merged
merged 2 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import TextFieldWithValidation from "../../TextFieldWithValidation";
import styles from "./SlackChannel.css";

interface Props {
channel: any;
channel: string;
disabled: boolean;
index: number;
onRemoveClicked: (index: number) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,28 @@ interface Props {

const SlackConfigContainer: FunctionComponent<Props> = ({ form, settings }) => {
const onAddChannel = useCallback(() => {
const mutators = form.mutators;
mutators.insert("slack.channels", 0, {
// We use push here because final form still has issues tracking new items
// being inserted at the old array index.
//
// Ref: https://github.com/final-form/final-form-arrays/issues/44
//
form.mutators.push("slack.channels", {
enabled: true,
name: "",
hookURL: "",
triggers: {
allComments: false,
reportedComments: false,
pendingComments: false,
featuredComments: false,
staffComments: false,
},
});
}, [form]);

const onRemoveChannel = useCallback(
(index: number) => {
const mutators = form.mutators;
mutators.remove("slack.channels", index);
form.mutators.remove("slack.channels", index);
},
[form]
);
Expand All @@ -58,9 +65,11 @@ const SlackConfigContainer: FunctionComponent<Props> = ({ form, settings }) => {
name: "",
hookURL: "",
triggers: {
allComments: false,
reportedComments: false,
pendingComments: false,
featuredComments: false,
staffComments: false,
},
},
],
Expand Down Expand Up @@ -107,16 +116,20 @@ const SlackConfigContainer: FunctionComponent<Props> = ({ form, settings }) => {
</Button>
<FieldArray name="slack.channels">
{({ fields }) =>
fields.map((channel: any, index: number) => (
<div key={index}>
fields
.map((channel, index) => (
<SlackChannel
key={index}
channel={channel}
disabled={false}
index={index}
onRemoveClicked={onRemoveChannel}
/>
</div>
))
))
// We're reversing here because we wanted the order of new items
// added to be at the front, and we can only use `.push` and not
// `.insert` or `.unshift`.
.reverse()
}
</FieldArray>
</ConfigBox>
Expand Down