import { isEqual } from 'lodash'; import memoizeOne from 'memoize-one'; import React from 'react'; import type { Option } from 'react-select'; interface IStringOptionsProps { strings?: string[]; render?: (options: Array>) => React.ReactNode; children?: (options: Array>) => React.ReactNode; } interface IStringOptionsState { options: Array>; } const makeOptions = (options: string[]): Array> => { options = options || []; return options.map((str) => ({ label: str, value: str })); }; /** * Converts an array of strings to an array of Options[] suitable for use with react-select * * Example: * * {options => } * */ export class StringsAsOptions extends React.Component { private memoizedMakeOptions = memoizeOne(makeOptions, isEqual); public render() { const options = this.memoizedMakeOptions(this.props.strings); const render = this.props.render || this.props.children; return render(options); } }