2022-11-10 20:13:15 +01:00
|
|
|
import React from 'react';
|
2022-10-26 00:23:18 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
2022-11-10 20:13:15 +01:00
|
|
|
import { SIZES } from '../../../helpers/constants/design-system';
|
2022-10-26 00:23:18 +02:00
|
|
|
|
2022-11-10 20:13:15 +01:00
|
|
|
import { ICON_NAMES } from '../icon';
|
|
|
|
import { ButtonIcon } from '../button-icon';
|
2022-10-26 00:23:18 +02:00
|
|
|
|
|
|
|
import { TextFieldBase } from '../text-field-base';
|
|
|
|
|
|
|
|
export const TextField = ({
|
|
|
|
className,
|
2022-11-10 20:13:15 +01:00
|
|
|
showClearButton, // only works with a controlled input
|
|
|
|
clearButtonOnClick,
|
2022-10-26 00:23:18 +02:00
|
|
|
clearButtonProps,
|
|
|
|
rightAccessory,
|
|
|
|
inputProps,
|
2022-11-10 20:13:15 +01:00
|
|
|
value,
|
|
|
|
onChange,
|
2022-10-26 00:23:18 +02:00
|
|
|
...props
|
2022-11-10 20:13:15 +01:00
|
|
|
}) => (
|
|
|
|
<TextFieldBase
|
|
|
|
className={classnames('mm-text-field', className)}
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
rightAccessory={
|
|
|
|
value && showClearButton ? (
|
|
|
|
<>
|
|
|
|
<ButtonIcon
|
|
|
|
className="mm-text-field__button-clear"
|
|
|
|
ariaLabel="Clear" // TODO: i18n
|
2022-12-06 20:51:48 +01:00
|
|
|
iconName={ICON_NAMES.CLOSE_OUTLINE}
|
2022-11-10 20:13:15 +01:00
|
|
|
size={SIZES.SM}
|
|
|
|
onClick={clearButtonOnClick}
|
|
|
|
{...clearButtonProps}
|
|
|
|
/>
|
|
|
|
{rightAccessory}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
rightAccessory
|
|
|
|
)
|
|
|
|
}
|
|
|
|
inputProps={{
|
|
|
|
marginRight: showClearButton ? 6 : 0,
|
|
|
|
...inputProps,
|
|
|
|
}}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
2022-10-26 00:23:18 +02:00
|
|
|
|
|
|
|
TextField.propTypes = {
|
2022-11-10 20:13:15 +01:00
|
|
|
/**
|
|
|
|
* The value af the TextField
|
|
|
|
*/
|
2022-11-15 17:49:02 +01:00
|
|
|
value: TextFieldBase.propTypes.value,
|
2022-11-10 20:13:15 +01:00
|
|
|
/**
|
|
|
|
* The onChange handler af the TextField
|
|
|
|
*/
|
2022-11-15 17:49:02 +01:00
|
|
|
onChange: TextFieldBase.propTypes.onChange,
|
2022-10-26 00:23:18 +02:00
|
|
|
/**
|
|
|
|
* An additional className to apply to the text-field
|
|
|
|
*/
|
|
|
|
className: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* Show a clear button to clear the input
|
|
|
|
*/
|
2022-11-10 20:13:15 +01:00
|
|
|
showClearButton: PropTypes.bool,
|
2022-10-26 00:23:18 +02:00
|
|
|
/**
|
2022-11-10 20:13:15 +01:00
|
|
|
* The onClick handler for the clear button
|
2022-10-26 00:23:18 +02:00
|
|
|
*/
|
2022-11-10 20:13:15 +01:00
|
|
|
clearButtonOnClick: PropTypes.func,
|
2022-10-26 00:23:18 +02:00
|
|
|
/**
|
|
|
|
* The props to pass to the clear button
|
|
|
|
*/
|
2022-11-15 17:49:02 +01:00
|
|
|
clearButtonProps: PropTypes.shape(ButtonIcon.PropTypes),
|
2022-10-26 00:23:18 +02:00
|
|
|
/**
|
|
|
|
* TextField accepts all the props from TextFieldBase and Box
|
|
|
|
*/
|
|
|
|
...TextFieldBase.propTypes,
|
|
|
|
};
|