Skip to content

Commit

Permalink
Merge pull request #278 from creative-commoners/pulls/4.0/single-fiel…
Browse files Browse the repository at this point in the history
…d-on-elemental

FIX Handle a string "0" value
  • Loading branch information
GuySartorelli committed May 3, 2024
2 parents 3a607ba + e5cc291 commit 7893dbb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion client/src/components/LinkField/LinkField.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ const LinkField = ({
})
);

// The value for a has_one LinkField will start off as a numeric 0
// If a parent object containing a child LinkField without a value is submitted,
// using FormSchema, then the FormSchema response may contain a string '0' instead
// Ensure that a numeric 0 is used
if (value === '0') {
value = 0;
}

// Ensure we have a valid array
let linkIDs = value;
if (!Array.isArray(linkIDs)) {
Expand Down Expand Up @@ -498,7 +506,7 @@ const LinkField = ({
};

LinkField.propTypes = {
value: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.number), PropTypes.number]),
value: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.number), PropTypes.number, PropTypes.string]),
onChange: PropTypes.func,
types: PropTypes.object.isRequired,
actions: PropTypes.object.isRequired,
Expand Down
9 changes: 9 additions & 0 deletions client/src/components/LinkField/tests/LinkField-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ test('LinkField returns list of links if they exist', async () => {
expect(container.querySelectorAll('.link-picker__button.font-icon-email')[0]).toHaveTextContent('Email title');
});

test('LinkField can handle a string "0" value', async () => {
const { container } = render(<LinkField {...makeProps({
value: '0'
})}
/>);
await screen.findByText('Add Link');
expect(container.querySelectorAll('.link-picker')).toHaveLength(1);
});

test('LinkField will render disabled state if disabled is true', async () => {
const { container } = render(<LinkField {...makeProps({
ownerID: 1,
Expand Down

0 comments on commit 7893dbb

Please sign in to comment.