@atlaskit/textfield
Version:
A text field is an input that allows a user to write or edit text.
179 lines (175 loc) • 8.62 kB
JavaScript
/* text-field.tsx generated by @compiled/babel-plugin v3.0.2 */
import _extends from "@babel/runtime/helpers/extends";
import _typeof from "@babel/runtime/helpers/typeof";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["appearance", "className", "elemAfterInput", "elemBeforeInput", "isCompact", "isDisabled", "isInvalid", "isMonospaced", "isReadOnly", "isRequired", "name", "onBlur", "onChange", "onFocus", "onMouseDown", "placeholder", "testId", "width"];
import "./text-field.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React, { forwardRef, useCallback, useRef } from 'react';
import { usePlatformLeafEventHandler } from '@atlaskit/analytics-next/usePlatformLeafEventHandler';
import { fg } from '@atlaskit/platform-feature-flags/fg';
var containerMedia = {
invalid: "_msj6gir2",
disabled: "_6up518qt"
};
var inputMediaDisabled = null;
var analyticsParams = {
componentName: 'textField',
packageName: "@atlaskit/textfield",
packageVersion: "10.2.3"
};
var disabledStyle = {
standard: "_1h6d187o _bfhk187o _syaz1gmx _80om13gf",
subtle: "_1h6d1j28 _bfhk1j28 _syaz1gmx _80om13gf",
none: "_1h6d1j28 _bfhk1j28 _syaz1gmx _80om13gf"
};
var invalidStyle = null;
var focusWithinStyle = {
standard: "_1p9x1v1w _qao9r01l _q433vyy1",
subtle: "_1p9x1v1w _qao9r01l _q433vyy1",
none: "_1p9x1j28 _qao91j28 _q4333sij"
};
var hoverStyle = {
standard: "_4cvxrsbi _irr3l4ek",
subtle: "_4cvxrsbi _irr3l4ek",
none: "_4cvx1j28 _irr31j28"
};
var getContainerTextBgAndBorderColor = {
standard: "_1h6drsbi _1dqonqa1 _syazi7uo _80om1kdv _bfhk1j9a _msj6gir2",
subtle: "_1h6d1j28 _1dqonqa1 _syazi7uo _80om1kdv _16hg1j28 _msj6gir2",
none: "_1h6d1j28 _1dqoglyw _syazi7uo _80om1kdv _16hg1j28 _msj6gir2"
};
var widthMap = {
xsmall: 80,
small: 160,
medium: 240,
large: 320,
xlarge: 480
};
var getMaxWidth = function getMaxWidth(width) {
return !width ? "100%" : width in widthMap ? "".concat(widthMap[width], "px") : "".concat(+width, "px");
};
var containerStyleAppearance = {
standard: "_1dqonqa1 _1q51e4h9 _85i5e4h9 _bozgidpf _y4tiidpf",
subtle: "_1dqonqa1 _1q51e4h9 _85i5e4h9 _bozgidpf _y4tiidpf",
none: "_1dqoglyw"
};
var containerStyles = null;
var inputDisabledStyle = null;
var inputCompactStyle = null;
var inputMonospacedStyle = null;
// iOS Safari automatically zooms into form inputs on focus when the font size is less than 16px.
// To prevent this zoom behaviour on mobile devices, the textfield uses font.body.large (16px) by default,
// then switches to the smaller font.body on screens wider than 30rem (desktop).
// @see: https://medium.com/@rares.popescu/2-ways-to-avoid-the-automatic-zoom-in-on-input-fields-8a71479e542e
var inputStyle = null;
var finessePlaceholderStyle = null;
/**
* __Textfield__
*
* A text field is an input that allows a user to write or edit text.
*
* - [Examples](https://atlassian.design/components/textfield/examples)
* - [Code](https://atlassian.design/components/textfield/code)
* - [Usage](https://atlassian.design/components/textfield/usage)
*/
var Textfield = /*#__PURE__*/forwardRef(function (props, ref) {
var _props$appearance = props.appearance,
appearance = _props$appearance === void 0 ? 'standard' : _props$appearance,
className = props.className,
elemAfterInput = props.elemAfterInput,
elemBeforeInput = props.elemBeforeInput,
_props$isCompact = props.isCompact,
isCompact = _props$isCompact === void 0 ? false : _props$isCompact,
_props$isDisabled = props.isDisabled,
isDisabled = _props$isDisabled === void 0 ? false : _props$isDisabled,
_props$isInvalid = props.isInvalid,
isInvalid = _props$isInvalid === void 0 ? false : _props$isInvalid,
_props$isMonospaced = props.isMonospaced,
isMonospaced = _props$isMonospaced === void 0 ? false : _props$isMonospaced,
_props$isReadOnly = props.isReadOnly,
isReadOnly = _props$isReadOnly === void 0 ? false : _props$isReadOnly,
_props$isRequired = props.isRequired,
isRequired = _props$isRequired === void 0 ? false : _props$isRequired,
name = props.name,
onBlur = props.onBlur,
onChange = props.onChange,
onFocus = props.onFocus,
onMouseDown = props.onMouseDown,
placeholder = props.placeholder,
testId = props.testId,
width = props.width,
spreadProps = _objectWithoutProperties(props, _excluded);
var inputRef = useRef(null);
var handleOnFocus = usePlatformLeafEventHandler(_objectSpread({
fn: function fn(event) {
onFocus && onFocus(event);
},
action: 'focused'
}, analyticsParams));
var handleOnBlur = usePlatformLeafEventHandler(_objectSpread({
fn: function fn(event) {
onBlur && onBlur(event);
},
action: 'blurred'
}, analyticsParams));
var handleOnMouseDown = useCallback(function (event) {
// Running e.preventDefault() on the INPUT prevents double click behaviour
// Sadly we needed this cast as the target type is being correctly set
var target = event.target;
if (target.tagName !== 'INPUT') {
event.preventDefault();
}
if (inputRef && inputRef.current && !isDisabled && document.activeElement !== inputRef.current) {
inputRef.current.focus();
}
onMouseDown && onMouseDown(event);
}, [onMouseDown, isDisabled]);
var setInputRef = useCallback(function (inputElement) {
inputRef.current = inputElement;
if (!ref) {
return;
}
if (_typeof(ref) === 'object') {
ref.current = inputElement;
}
if (typeof ref === 'function') {
ref(inputElement);
}
}, [ref]);
return /*#__PURE__*/React.createElement("div", {
role: "presentation",
"data-disabled": isDisabled ? isDisabled : undefined,
"data-invalid": isInvalid ? isInvalid : undefined,
"data-ds--text-field--container": true,
"data-testid": testId && "".concat(testId, "-container"),
onMouseDown: handleOnMouseDown,
style: {
maxWidth: "".concat(getMaxWidth(width))
},
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
className: ax(["_16jlkb7n _1o9zkb7n _i0dl1osq _11c8fhey _1reo15vq _18m915vq _v564r5cv _189ee4h9 _1e0c1txw _vchhusvi _4cvr1h6o _1bah1yb4 _lcxv1wug _s7n4jp4b _slp31hna _1tnq1qi0", getContainerTextBgAndBorderColor[appearance], containerStyleAppearance[appearance], !isDisabled && focusWithinStyle[appearance], !isDisabled && hoverStyle[appearance], isDisabled && containerMedia.disabled, isDisabled && disabledStyle[appearance], isInvalid && containerMedia.invalid, isInvalid && "_1h6d1bqt _4cvx1bqt _16qsizbr _1dbzizbr _uk1sr01l", className])
}, elemBeforeInput, /*#__PURE__*/React.createElement("input", _extends({}, spreadProps, {
"aria-invalid": isInvalid ? isInvalid : undefined
// TODO: When removing legacy theming fix this.
,
"data-compact": isCompact ? isCompact : undefined,
"data-ds--text-field--input": true,
"data-monospaced": isMonospaced ? isMonospaced : undefined,
"data-testid": testId,
disabled: isDisabled,
name: name,
onBlur: handleOnBlur,
onChange: onChange,
onFocus: handleOnFocus,
placeholder: placeholder,
readOnly: isReadOnly,
ref: setInputRef,
required: isRequired,
className: ax(["_19itidpf _11c81d4k _12ji1r31 _1qu2glyw _12y31o36 _vchhusvi _1bsb1osq _1ul9idpf _bfhk1j28 _syaz1kw7 _80om1kw7 _14ji12x7 _olc612x7 _1kod12x7 _7ba012x7 _1goxglyw _13xeglyw _1n7e1l2s _1idr1rpy _1tn2fhey _l9oiu2gc _549yu2gc _1h5w12x7 _124212x7", fg('platform-dst-tokens-finesse') && "_1idrazsu", isMonospaced && "_ect4zzfg _14mdzzfg", isCompact && "_11zpv77o _1l02v77o _daj712x7 _141f12x7 _1srd1b66 _oi9h1b66 _1e3012x7 _1txv12x7", isDisabled && "_syaz1gmx _1idr1gmx", isDisabled && "_1fnx18qt"])
})), elemAfterInput);
});
export default Textfield;