Skip to content

Commit

Permalink
PF3->Carbon: converted select component
Browse files Browse the repository at this point in the history
  • Loading branch information
skateman committed Jan 18, 2021
1 parent 087260c commit 8454084
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 1,347 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const createSchema = (fields, promise, edit, loadSchema) => ({
validate: [{ type: validatorTypes.REQUIRED }],
isRequired: true,
isDisabled: edit,
includeEmpty: true,
onChange: value => promise.then(loadSchema(value)),
loadOptions: () =>
promise.then(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ export const providersNetworkTypes = [
{ label: __('VXLAN'), value: 'vxlan' },
];

const getTenants = id => API.get(`/api/providers/${id}/cloud_tenants?expand=resources&attributes=id,name`).then(data => [
{ label: `<${__('Choose')}>` },
...data.resources.map(({ id, name }) => ({ value: id, label: name })),
]);
const getTenants = (id) => API.get(`/api/providers/${id}/cloud_tenants?expand=resources&attributes=id,name`).then((data) =>
data.resources.map(({ id, name }) => ({ value: id, label: name })));

function createSchema(ems, cloudNetworkId) {
const dynamicPlacement = ems.map((tenant => ({
Expand All @@ -31,6 +29,7 @@ function createSchema(ems, cloudNetworkId) {
}],
isDisabled: !!cloudNetworkId,
loadOptions: () => getTenants(tenant.id),
includeEmpty: true,
condition: {
when: 'ems_id',
is: tenant.id,
Expand Down Expand Up @@ -208,23 +207,20 @@ function createSchema(ems, cloudNetworkId) {
id: 'cloud_network_external_facing',
name: 'external_facing',
label: __('External Router'),
bsSize: 'mini',
onText: __('Yes'),
offText: __('No'),
}, {
component: componentTypes.SWITCH,
id: 'cloud_network_enabled',
name: 'enabled',
label: __('Administrative State'),
bsSize: 'mini',
onText: __('Up'),
offText: __('Down'),
}, {
component: componentTypes.SWITCH,
id: 'cloud_network_shared',
name: 'shared',
label: __('Shared'),
bsSize: 'mini',
onText: __('Yes'),
offText: __('No'),
}],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const orchestrationFormSchema = (isEditing = false, isCopying = false, initialVa
label: __('Provider'),
component: componentTypes.SELECT,
loadOptions: getManagers,
placeholder: `<${__('Choose')}>`,
includeEmpty: true,
isRequired: true,
validateOnMount: true,
clearOnUnmount: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const createSchema = (emsId, setEmsId) => ({
label: __('Provider:'),
isRequired: true,
loadOptions: loadProviders,
includeEmpty: true,
onChange: value => setEmsId(value),
validate: [{ type: validatorTypes.REQUIRED }],
},
Expand All @@ -42,6 +43,7 @@ const createSchema = (emsId, setEmsId) => ({
isRequired: true,
validate: [{ type: validatorTypes.REQUIRED }],
loadOptions: () => (emsId ? loadFamilies(emsId) : Promise.resolve([])),
includeEmpty: true,
key: emsId,
condition: {
when: 'ems_id',
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/components/provider-form/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const commonFields = [
id: 'zone_id',
name: 'zone_id',
label: __('Zone'),
includeEmpty: true,
loadOptions: () =>
API.get('/api/zones?expand=resources&attributes=id,name,visible&filter[]=visible!=false&sort_by=name')
.then(({ resources }) => resources.map(({ id: value, name: label }) => ({ value, label }))),
Expand Down Expand Up @@ -64,6 +65,7 @@ const typeSelectField = (edit, filter, setState) => ({
label: __('Type'),
kind: filter,
isDisabled: edit,
includeEmpty: true,
loadOptions: () =>
API.options('/api/providers').then(({ data: { supported_providers } }) => supported_providers // eslint-disable-line camelcase
.filter(({ kind }) => kind === filter)
Expand Down
27 changes: 25 additions & 2 deletions app/javascript/components/select/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { components } from '@data-driven-forms/carbon-component-mapper';

import { useFieldApi } from '@@ddf';

const SelectWithOnChange = ({ onChange, ...props }) => {
const SelectWithOnChange = ({
includeEmpty,
loadOptions: _loadOptions,
onChange,
placeholder,
...props
}) => {
if (onChange) {
const { input: { value } } = useFieldApi(props);

Expand All @@ -15,15 +21,32 @@ const SelectWithOnChange = ({ onChange, ...props }) => {
}, [value]);
}

return <components.Select placeholder={`<${__('Choose')}>`} {...props} />;
// Add a dummy placeholder field to the list of the dynamically loaded options
const loadOptions = includeEmpty !== true ? _loadOptions : ((...args) => _loadOptions(...args).then((items) => [
{
label: placeholder,
value: undefined,
},
...items,
]));

return <components.Select placeholder={placeholder} loadOptions={loadOptions} {...props} />;
};

SelectWithOnChange.propTypes = {
includeEmpty: PropTypes.bool,
isDisabled: PropTypes.bool,
loadOptions: PropTypes.func,
onChange: PropTypes.func,
placeholder: PropTypes.string,
};

SelectWithOnChange.defaultProps = {
includeEmpty: undefined,
isDisabled: undefined,
loadOptions: undefined,
onChange: undefined,
placeholder: `<${__('Choose')}>`,
};

export default SelectWithOnChange;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const createSchema = (promise) => ({
id: 'serverId',
label: __('Select Server:'),
placeholder: `<${__('Not a Server')}>`,
includeEmpty: true,
loadOptions: () => promise.then(({ resources }) => resources.map(({ id, name }) => ({ label: name, value: id }))),
}],
});
Expand Down
Loading

0 comments on commit 8454084

Please sign in to comment.