{"size": 3918, "ext": "js", "max_stars_repo_path": "wordpress/wp-content/plugins/alodokter-paf-assignment/vendor/htmlburger/carbon-fields/packages/core/fields/complex/group.js", "max_stars_repo_name": "orangerdev/alodokter-paf-assignment", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "wordpress/wp-content/plugins/alodokter-paf-assignment/vendor/htmlburger/carbon-fields/packages/core/fields/complex/group.js", "max_issues_repo_name": "orangerdev/alodokter-paf-assignment", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "wordpress/wp-content/plugins/alodokter-paf-assignment/vendor/htmlburger/carbon-fields/packages/core/fields/complex/group.js", "max_forks_repo_name": "orangerdev/alodokter-paf-assignment", "max_forks_repo_forks_event_min_datetime": "2022-01-06T12:17:48.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-06T12:17:48.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 21.4098360656, "max_line_length": 147, "alphanum_fraction": 0.5750382848}
/**
 * External dependencies.
 */
import cx from 'classnames';
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
 * Internal dependencies.
 */
import { getFieldType } from '../../registry/fields';

class ComplexGroup extends Component {
	/**
	 * Handles the click on the "Toggle" button.
	 *
	 * @return {void}
	 */
	handleToggleClick = () => {
		const {
			id,
			onToggle
		} = this.props;

		onToggle( id );
	}

	/**
	 * Handles the click on the "Clone" button.
	 *
	 * @return {void}
	 */
	handleCloneClick = () => {
		const {
			id,
			onClone
		} = this.props;

		onClone( id );
	}

	/**
	 * Handles the click on the "Remove" button.
	 *
	 * @return {void}
	 */
	handleRemoveClick = () => {
		const {
			id,
			onRemove
		} = this.props;

		onRemove( id );
	}

	/**
	 * Renders the component.
	 *
	 * @return {Object}
	 */
	render() {
		const {
			index,
			label,
			name,
			prefix,
			tabbed,
			hidden,
			dragged,
			collapsed,
			allowClone,
			fields,
			context,
			onFieldSetup
		} = this.props;

		const groupClasses = cx(
			'cf-complex__group',
			{
				'cf-complex__group--grid': ! tabbed,
				'cf-complex__group--tabbed': tabbed,
				'cf-complex__group--collapsed': collapsed,
				'cf-complex__group--dragged': dragged
			}
		);

		const toggleClasses = cx(
			'dashicons-before',
			'cf-complex__group-action-icon',
			{
				'dashicons-arrow-up': ! collapsed,
				'dashicons-arrow-down': collapsed
			}
		);

		const actionsClasses = cx(
			'cf-complex__group-actions',
			{
				'cf-complex__group-actions--grid': ! tabbed,
				'cf-complex__group-actions--tabbed': tabbed
			}
		);

		return (
			<div className={ groupClasses } hidden={ hidden }>
				{ name && (
					<input
						type="hidden"
						name={ `${ prefix }[value]` }
						value={ name }
					/>
				) }

				{ ! tabbed && (
					<div className="cf-complex__group-head">
						<span className="cf-complex__group-index">
							{ index + 1 }
						</span>

						<span className="cf-complex__group-title">
							{ label }
						</span>
					</div>
				) }

				{ ! dragged && (
					<div className="cf-complex__group-body" hidden={ ! tabbed && collapsed }>
						{ fields.map( ( field ) => {
							const FieldEdit = getFieldType( field.type, context );

							if ( ! FieldEdit ) {
								return null;
							}

							const [ Field, props ] = onFieldSetup( field, {}, this.props );

							return (
								// The `key` will be assigned via `onFieldSetup`.
								// eslint-disable-next-line react/jsx-key
								<Field { ...props }>
									<FieldEdit { ...props } />
								</Field>
							);
						} ) }
					</div>
				) }

				<div className={ actionsClasses }>
					{ allowClone && (
						<button type="button" title={ __( 'Duplicate', 'carbon-fields-ui' ) } className="cf-complex__group-action" onClick={ this.handleCloneClick }>
							<span className="dashicons-before dashicons-admin-page cf-complex__group-action-icon"></span>

							<span className="cf-complex__group-action-text">
								{ __( 'Duplicate', 'carbon-fields-ui' ) }
							</span>
						</button>
					) }

					<button type="button" title={ __( 'Remove', 'carbon-fields-ui' ) } className="cf-complex__group-action" onClick={ this.handleRemoveClick }>
						<span className="dashicons-before dashicons-trash cf-complex__group-action-icon"></span>

						<span className="cf-complex__group-action-text">
							{ __( 'Remove', 'carbon-fields-ui' ) }
						</span>
					</button>

					{ ! tabbed && (
						<button type="button" title={ __( 'Collapse', 'carbon-fields-ui' ) } className="cf-complex__group-action" onClick={ this.handleToggleClick }>
							<span className={ toggleClasses }></span>

							<span className="cf-complex__group-action-text">
								{ __( 'Collapse', 'carbon-fields-ui' ) }
							</span>
						</button>
					) }
				</div>
			</div>
		);
	}
}

export default ComplexGroup;
