2018-12-12 15:20:17 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-12-12 19:23:41 +00:00
|
|
|
import { BaseControl } from '@wordpress/components';
|
|
|
|
import { withInstanceId } from '@wordpress/compose';
|
2019-12-13 17:35:29 +00:00
|
|
|
import classnames from 'classnames';
|
2018-12-12 15:20:17 +00:00
|
|
|
|
2018-12-13 16:21:47 +00:00
|
|
|
/**
|
|
|
|
* This component is essentially a wrapper (really a reimplementation) around the
|
|
|
|
* TextControl component that adds support for affixes, i.e. the ability to display
|
|
|
|
* a fixed part either at the beginning or at the end of the text input.
|
|
|
|
*/
|
2018-12-12 15:20:17 +00:00
|
|
|
class TextControlWithAffixes extends Component {
|
2018-12-13 16:30:20 +00:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
label,
|
|
|
|
value,
|
|
|
|
help,
|
|
|
|
className,
|
|
|
|
instanceId,
|
|
|
|
onChange,
|
|
|
|
prefix,
|
|
|
|
suffix,
|
|
|
|
type,
|
2019-12-13 17:35:29 +00:00
|
|
|
disabled,
|
2018-12-13 16:30:20 +00:00
|
|
|
...props
|
|
|
|
} = this.props;
|
2018-12-12 15:20:17 +00:00
|
|
|
|
2018-12-13 16:30:20 +00:00
|
|
|
const id = `inspector-text-control-with-affixes-${ instanceId }`;
|
|
|
|
const onChangeValue = ( event ) => onChange( event.target.value );
|
2018-12-13 17:04:57 +00:00
|
|
|
const describedby = [];
|
|
|
|
if ( help ) {
|
|
|
|
describedby.push( `${ id }__help` );
|
|
|
|
}
|
|
|
|
if ( prefix ) {
|
|
|
|
describedby.push( `${ id }__prefix` );
|
|
|
|
}
|
|
|
|
if ( suffix ) {
|
|
|
|
describedby.push( `${ id }__suffix` );
|
|
|
|
}
|
2018-12-12 15:20:17 +00:00
|
|
|
|
2019-12-13 17:35:29 +00:00
|
|
|
const affixesClasses = classnames( 'text-control-with-affixes', {
|
|
|
|
'text-control-with-prefix': prefix,
|
|
|
|
'text-control-with-suffix': suffix,
|
|
|
|
disabled,
|
|
|
|
} );
|
|
|
|
|
2018-12-13 16:30:20 +00:00
|
|
|
return (
|
|
|
|
<BaseControl label={ label } id={ id } help={ help } className={ className }>
|
2019-12-13 17:35:29 +00:00
|
|
|
<div className={ affixesClasses }>
|
2018-12-13 17:04:57 +00:00
|
|
|
{ prefix && (
|
|
|
|
<span
|
|
|
|
id={ `${ id }__prefix` }
|
|
|
|
className="text-control-with-affixes__prefix"
|
|
|
|
>
|
|
|
|
{ prefix }
|
|
|
|
</span>
|
|
|
|
) }
|
2018-12-12 15:20:17 +00:00
|
|
|
|
2018-12-13 17:04:57 +00:00
|
|
|
<input
|
|
|
|
className="components-text-control__input"
|
2018-12-13 16:30:20 +00:00
|
|
|
type={ type }
|
|
|
|
id={ id }
|
|
|
|
value={ value }
|
|
|
|
onChange={ onChangeValue }
|
2018-12-13 17:04:57 +00:00
|
|
|
aria-describedby={ describedby.join( ' ' ) }
|
2019-12-13 17:35:29 +00:00
|
|
|
disabled={ disabled }
|
2018-12-13 16:30:20 +00:00
|
|
|
{ ...props }
|
|
|
|
/>
|
2018-12-12 19:23:41 +00:00
|
|
|
|
2018-12-13 17:04:57 +00:00
|
|
|
{ suffix && (
|
|
|
|
<span
|
|
|
|
id={ `${ id }__suffix` }
|
|
|
|
className="text-control-with-affixes__suffix"
|
|
|
|
>
|
|
|
|
{ suffix }
|
|
|
|
</span>
|
|
|
|
) }
|
2018-12-13 16:30:20 +00:00
|
|
|
</div>
|
|
|
|
</BaseControl>
|
|
|
|
);
|
|
|
|
}
|
2018-12-12 15:20:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 16:21:47 +00:00
|
|
|
TextControlWithAffixes.defaultProps = {
|
2018-12-13 16:30:20 +00:00
|
|
|
type: 'text',
|
2018-12-13 16:21:47 +00:00
|
|
|
};
|
|
|
|
|
2018-12-12 15:20:17 +00:00
|
|
|
TextControlWithAffixes.propTypes = {
|
2018-12-13 16:30:20 +00:00
|
|
|
/**
|
|
|
|
* If this property is added, a label will be generated using label property as the content.
|
|
|
|
*/
|
|
|
|
label: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* If this property is added, a help text will be generated using help property as the content.
|
|
|
|
*/
|
|
|
|
help: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* Type of the input element to render. Defaults to "text".
|
|
|
|
*/
|
|
|
|
type: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* The current value of the input.
|
|
|
|
*/
|
|
|
|
value: PropTypes.string.isRequired,
|
|
|
|
/**
|
|
|
|
* The class that will be added with "components-base-control" to the classes of the wrapper div.
|
|
|
|
* If no className is passed only components-base-control is used.
|
|
|
|
*/
|
|
|
|
className: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* A function that receives the value of the input.
|
|
|
|
*/
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
/**
|
|
|
|
* Markup to be inserted at the beginning of the input.
|
|
|
|
*/
|
|
|
|
prefix: PropTypes.node,
|
|
|
|
/**
|
|
|
|
* Markup to be appended at the end of the input.
|
|
|
|
*/
|
|
|
|
suffix: PropTypes.node,
|
2019-12-13 17:35:29 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the input is disabled.
|
|
|
|
*/
|
|
|
|
disabled: PropTypes.bool,
|
2018-12-12 15:20:17 +00:00
|
|
|
};
|
|
|
|
|
2018-12-12 19:23:41 +00:00
|
|
|
export default withInstanceId( TextControlWithAffixes );
|