-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
196 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { MultiSelectFieldFF, ReactFinalForm, MultiSelectField } from '@dhis2/ui' | ||
import i18n from '@dhis2/d2-i18n' | ||
import { useParameterOption } from '../../hooks/parameter-options' | ||
|
||
const { Field } = ReactFinalForm | ||
|
||
// This field has options that have both an id and a label. | ||
const ListFieldMulti = ({ label, name, parameterName }) => { | ||
const { loading, error, data } = useParameterOption(parameterName) | ||
const disabledProps = { disabled: true, label } | ||
|
||
if (loading) { | ||
return ( | ||
<MultiSelectField | ||
{...disabledProps} | ||
helpText={i18n.t('Loading options')} | ||
/> | ||
) | ||
} | ||
|
||
if (error) { | ||
return ( | ||
<MultiSelectField | ||
{...disabledProps} | ||
helpText={i18n.t('Something went wrong whilst loading options')} | ||
/> | ||
) | ||
} | ||
|
||
if (data.length === 0) { | ||
return ( | ||
<MultiSelectField | ||
{...disabledProps} | ||
helpText={i18n.t('No options available')} | ||
/> | ||
) | ||
} | ||
|
||
const labeledOptions = data.map(({ id, displayName }) => ({ | ||
value: id, | ||
label: displayName, | ||
})) | ||
|
||
return ( | ||
<Field | ||
name={name} | ||
component={MultiSelectFieldFF} | ||
options={labeledOptions} | ||
label={label} | ||
/> | ||
) | ||
} | ||
|
||
const { string } = PropTypes | ||
|
||
ListFieldMulti.propTypes = { | ||
label: string.isRequired, | ||
name: string.isRequired, | ||
parameterName: string.isRequired, | ||
} | ||
|
||
export default ListFieldMulti |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from 'react' | ||
import { mount } from 'enzyme' | ||
import { ReactFinalForm } from '@dhis2/ui' | ||
import { useParameterOption } from '../../hooks/parameter-options' | ||
import ListFieldMulti from './ListFieldMulti' | ||
|
||
const { Form } = ReactFinalForm | ||
|
||
jest.mock('../../hooks/parameter-options', () => ({ | ||
useParameterOption: jest.fn(), | ||
})) | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
describe('<ListFieldMulti>', () => { | ||
it('shows a message when there are no options', () => { | ||
useParameterOption.mockImplementation(() => ({ | ||
loading: false, | ||
error: undefined, | ||
data: [], | ||
})) | ||
const props = { | ||
label: 'label', | ||
name: 'name', | ||
parameterName: 'parameterName', | ||
} | ||
const wrapper = mount( | ||
<Form onSubmit={() => {}}> | ||
{() => ( | ||
<form> | ||
<ListFieldMulti {...props} /> | ||
</form> | ||
)} | ||
</Form> | ||
) | ||
|
||
const actual = wrapper | ||
.find({ | ||
'data-test': 'dhis2-uiwidgets-multiselectfield-help', | ||
}) | ||
.text() | ||
|
||
expect(actual).toEqual(expect.stringContaining('No options available')) | ||
}) | ||
|
||
it('renders the field when there are options', () => { | ||
useParameterOption.mockImplementation(() => ({ | ||
loading: false, | ||
error: undefined, | ||
data: [{ id: 'id', displayName: 'displayName' }], | ||
})) | ||
const props = { | ||
label: 'label', | ||
name: 'fieldName', | ||
parameterName: 'parameterName', | ||
} | ||
const wrapper = mount( | ||
<Form onSubmit={() => {}}> | ||
{() => ( | ||
<form> | ||
<ListFieldMulti {...props} /> | ||
</form> | ||
)} | ||
</Form> | ||
) | ||
|
||
const actual = wrapper.find('ListFieldMulti') | ||
|
||
expect(actual).toHaveLength(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.