import * as React from 'react' import cx from 'classnames' import { TextControl, type IconType, } from '@wordpress/components' import { closeSmall, } from '@wordpress/icons' import { Button, } from '@ska/components' import type { TextControlProps, } from '@wordpress/components/build-types/text-control/types' import './style.scss' export interface ActionInputProps extends Omit, 'onChange' | 'onPaste' | 'onSubmit'> { id?: string label?: string hideLabelFromVision?: boolean placeholder?: string actionLabel?: string clearLabel?: string value: string onChange: (nextValue: string) => void onSubmit?: (value: string) => void onClear?: () => void onPaste?: (e: React.ClipboardEvent) => void inputProps?: Partial buttonProps?: typeof Button disabled?: boolean /** Disable and swap icon while loading. */ loading?: boolean /** Disable the action button when the value is empty. */ disabledWhenBlank?: boolean /** Highlight the input because it requires action. */ actionable?: boolean /** Highlight the input because it requires a value. */ required?: boolean icon?: IconType } const disabledProps = (isDisabled: boolean) => ({ disabled: isDisabled, ...(isDisabled && { 'aria-disabled': true, }), }) const ActionInput: React.FC = ({ id, className, label = '', hideLabelFromVision = true, placeholder = '', actionLabel = '', clearLabel = 'Clear value', value, onChange, onSubmit, onClear, onPaste, disabled = false, loading = false, disabledWhenBlank = true, actionable = false, required = false, inputProps = {}, buttonProps = {}, icon, ...props }) => { const buttonIsDisabled = loading || disabled || (disabledWhenBlank && !value) const hasActionButton = actionLabel && !icon const clearable = onClear && value return (
{ e.preventDefault() if(onSubmit) { onSubmit(value) } }} {...props} >
{icon && (
{hasActionButton && (