diff --git a/js/src/components/ads-account-select-control/index.js b/js/src/components/ads-account-select-control/index.js new file mode 100644 index 0000000000..ccb26c3447 --- /dev/null +++ b/js/src/components/ads-account-select-control/index.js @@ -0,0 +1,28 @@ +/** + * Internal dependencies + */ +import AppSelectControl from '.~/components/app-select-control'; +import useExistingGoogleAdsAccounts from '.~/hooks/useExistingGoogleAdsAccounts'; + +/** + * @param {Object} props The component props + * @return {JSX.Element} An enhanced AppSelectControl component. + */ +const AdsAccountSelectControl = ( props ) => { + const { existingAccounts } = useExistingGoogleAdsAccounts(); + + const options = existingAccounts?.map( ( acc ) => ( { + value: acc.id, + label: `${ acc.name } (${ acc.id })`, + } ) ); + + return ( + + ); +}; + +export default AdsAccountSelectControl; diff --git a/js/src/components/app-select-control/index.js b/js/src/components/app-select-control/index.js index a0233d4905..351bedd844 100644 --- a/js/src/components/app-select-control/index.js +++ b/js/src/components/app-select-control/index.js @@ -2,7 +2,9 @@ * External dependencies */ import { SelectControl } from '@wordpress/components'; +import { useEffect, useRef } from '@wordpress/element'; import classNames from 'classnames'; +import { noop } from 'lodash'; /** * Internal dependencies @@ -12,18 +14,60 @@ import './index.scss'; /** * Renders a `@wordpress/components`'s `SelectControl` with margin-bottom removed. * - * If you provide `className` via props, - * it will be added to the container div's `className`, - * so that you can further control its style. - * - * @param {*} props + * @param {*} props The component props. + * @param {Array} [props.options=[]] Array of options for the select dropdown. Each option should be an object containing `label` and `value` properties. + * @param {string} [props.className] Additional classname to further control the style of the component. + * @param {Function} [props.onChange=noop] Callback function triggered when the selected value changes. Receives the new value as an argument. + * @param {string} [props.value] The currently selected value. This component should be used as a controlled component. A special case is that after mounting, when `autoSelectFirstOption` is true and `value` is undefined, it tries to call back `onChange` once to select the first option so that the `value` can be consistent with the `