HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vm8 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: afleverb (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //var/www/drakkar_site_dev/wp-content/themes/blocksy/static/js/options/options/ct-spacing/input.js
import { createElement, Fragment } from '@wordpress/element'
import InputWithValidCssExpression from '../../components/InputWithValidCssExpression'

import { __ } from 'ct-i18n'

import cls from 'classnames'

import {
	SPACING_STATE_CUSTOM,
	SPACING_STATE_LINKED,
	SPACING_STATE_INDEPENDENT,
} from '../ct-spacing'

import { getNumericKeyboardEvents } from '../../helpers/getNumericKeyboardEvents'

const SpacingInput = ({ value, option, onChange, currentUnit }) => {
	if (value.state === SPACING_STATE_CUSTOM) {
		return (
			<span>
				<InputWithValidCssExpression
					type="text"
					placeholder=""
					value={value.custom}
					onChange={(v) => {
						onChange({
							...value,
							custom: v,
						})
					}}
					{...option.inputAttr}
					shouldPropagateEmptyValue={true}
				/>
			</span>
		)
	}

	const handleChange = (futureValue, sideIndex) => {
		if (value.state === SPACING_STATE_LINKED) {
			onChange({
				...value,
				values: value.values.map((v, i) => {
					if (v.value === 'auto') {
						return v
					}

					return {
						...v,
						value: futureValue,
						unit: currentUnit,
					}
				}),
			})

			return
		}

		onChange({
			...value,
			values: value.values.map((v, i) => {
				if (i === sideIndex) {
					return {
						...v,
						value: futureValue,
						unit: currentUnit,
					}
				}

				return v
			}),
		})
	}

	return (
		<Fragment>
			{['top', 'right', 'bottom', 'left'].map((side, index) => (
				<span key={side}>
					<input
						type="number"
						step={1}
						value={
							value.values[index].value === 'auto'
								? ''
								: value.values[index].value
						}
						onChange={({ target: { value: inputValue } }) => {
							handleChange(inputValue, index)
						}}
						className={cls({
							inactive: value.values[index].value === 'auto',
						})}
						{...{
							placeholder:
								value.values[index].value === 'auto'
									? 'auto'
									: '',
							...option.inputAttr,
						}}
						{...getNumericKeyboardEvents({
							value: value.values[index].value,
							onChange: (inputValue) => {
								handleChange(inputValue, index)
							},
						})}
					/>

					<small>
						{
							{
								top: __('Top', 'blocksy'),
								bottom: __('Bottom', 'blocksy'),
								left: __('Left', 'blocksy'),
								right: __('Right', 'blocksy'),
							}[side]
						}
					</small>
				</span>
			))}
		</Fragment>
	)
}

export default SpacingInput