HEX
Server: Apache/2.4.68 (Debian)
System: Linux as-cs-widget-demo-us-central1 6.1.0-44-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.164-1 (2026-03-09) x86_64
User: root (0)
PHP: 8.2.32
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/wp-recipe-maker/assets/js/admin-modal/select/index.js
import React, { Component, Fragment } from 'react';

import '../../../css/admin/modal/select.scss';

import { __wprm } from 'Shared/Translations';
import Header from '../general/Header';
import Footer from '../general/Footer';

import SelectRecipe from './SelectRecipe';
import SelectList from './SelectList';

const firstRecipeOnPage = {
    id: 0,
    text: __wprm( 'First recipe on page' ),
};

export default class Select extends Component {
    constructor(props) {
        super(props);

        let type = 'recipe';
        if ( props.args.hasOwnProperty( 'type' ) ) {
            type = props.args.type;
        }

        let selection = false;
        if ( 'recipe' === type && props.args.fields.recipe.showFirst ) {
            selection = firstRecipeOnPage;
        }
    
        this.state = {
            type,
            selection,
        };

        this.selectRecipeRef = React.createRef();
    }

    componentDidMount() {
        // Focus the dropdown when the modal opens
        if (this.selectRecipeRef.current && this.selectRecipeRef.current.focus) {
            // Use setTimeout to ensure the component is fully rendered
            setTimeout(() => {
                this.selectRecipeRef.current.focus();
            }, 100);
        }
    }

    selectionsMade() {
        return false !== this.state.selection;
    }

    render() {
        return (
            <Fragment>
                <Header
                    onCloseModal={ this.props.maybeCloseModal }
                >
                    {
                        this.props.args.title
                        ?
                        this.props.args.title
                        :
                        'WP Recipe Maker'
                    }
                </Header>
                <div className="wprm-admin-modal-select-container">
                    {
                        'recipe' === this.state.type
                        &&
                        <Fragment>
                            {
                                this.props.args.fields.recipe
                                ?
                                <SelectRecipe
                                    ref={this.selectRecipeRef}
                                    options={
                                        this.props.args.fields.recipe.showFirst
                                        ?
                                        [firstRecipeOnPage]
                                        :
                                        []
                                    }
                                    value={ this.state.selection }
                                    onValueChange={(selection) => {
                                        this.setState({ selection });
                                    }}
                                />
                                :
                                null
                            }
                        </Fragment>
                    }
                    {
                        'list' === this.state.type
                        &&
                        <Fragment>
                            <SelectList
                                options={ [] }
                                value={ this.state.selection }
                                onValueChange={(selection) => {
                                    this.setState({ selection });
                                }}
                            />
                        </Fragment>
                    }
                </div>
                <Footer
                    savingChanges={ false }
                >
                    <button
                        className="button button-primary button-compact"
                        onClick={ () => {
                            let data = {};

                            switch ( this.state.type ) {
                                case 'list':
                                    data.list = this.state.selection;
                                    break;
                                default:
                                    data.recipe = this.state.selection;
                            }

                            if ( 'function' === typeof this.props.args.nextStepCallback ) {
                                this.props.args.nextStepCallback( data );
                            } else {
                                if ( 'function' === typeof this.props.args.insertCallback ) {
                                    this.props.args.insertCallback( data );
                                }
                                this.props.maybeCloseModal();
                            }
                        } }
                        disabled={ ! this.selectionsMade() }
                    >
                        {
                            this.props.args.button
                            ?
                            this.props.args.button
                            :
                            __wprm( 'Select' )
                        }
                    </button>
                </Footer>
            </Fragment>
        );
    }
}