Skip to content

Commit

Permalink
ERM-3429 Set up RefdataSelect component
Browse files Browse the repository at this point in the history
* first try
  • Loading branch information
CalamityC committed Dec 11, 2024
1 parent c3e4e56 commit 826a9a7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export { default as SerialCoverage } from './lib/SerialCoverage';
export { default as Tags } from './lib/Tags';
export { default as TitleOnPlatformLink } from './lib/TitleOnPlatformLink';
export { default as ViewOrganizationCard } from './lib/ViewOrganizationCard';
export { default as RefdataSelect } from './lib/RefdataSelect';

// Shared registry components/functions
export { default as InternalContactsArrayDisplay } from './lib/InternalContactsArrayDisplay';
Expand Down
66 changes: 66 additions & 0 deletions lib/RefdataSelect/RefdataSelect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Select } from '@folio/stripes/components';

const RefdataSelect = ({
input,
meta,
dataOptions,
onChange,
value,
format = (v) => v?.value || '', // Format function to transform the value for the UI
parse = (v) => ({ value: v }), // Parse function to transform the value for the API
...rest
}) => {
const currentValue = input ? format(input.value) : format(value);

const handleChange = (event) => {
const parsedValue = parse(event.target.value);
if (input) {
input.onChange(parsedValue);
}
if (onChange) {
onChange(parsedValue);
}
};

return (
<Select
{...rest}
value={currentValue}
onChange={handleChange}

Check failure on line 31 in lib/RefdataSelect/RefdataSelect.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

Props should be sorted alphabetically
dataOptions={dataOptions}

Check failure on line 32 in lib/RefdataSelect/RefdataSelect.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

Props should be sorted alphabetically
error={meta?.touched && meta?.error}

Check failure on line 33 in lib/RefdataSelect/RefdataSelect.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

Props should be sorted alphabetically
/>
);
};

RefdataSelect.propTypes = {
input: PropTypes.shape({
value: PropTypes.any,

Check warning on line 40 in lib/RefdataSelect/RefdataSelect.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

Prop type "any" is forbidden
onChange: PropTypes.func,
}),
meta: PropTypes.shape({
touched: PropTypes.bool,
error: PropTypes.string,
}),
dataOptions: PropTypes.arrayOf(
PropTypes.shape({
value: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
})
).isRequired,
onChange: PropTypes.func,
value: PropTypes.any,

Check warning on line 54 in lib/RefdataSelect/RefdataSelect.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

Prop type "any" is forbidden
format: PropTypes.func,
parse: PropTypes.func,
};

RefdataSelect.defaultProps = {
input: undefined,
meta: undefined,
onChange: undefined,
value: undefined,
};

export default RefdataSelect;
1 change: 1 addition & 0 deletions lib/RefdataSelect/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './RefdataSelect';

0 comments on commit 826a9a7

Please sign in to comment.