@canonical/react-components 4.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,12 @@
1
1
  import { type ReactElement } from "react";
2
2
  import { type InputProps } from "../Input";
3
3
  import "./PrefixedInput.scss";
4
- import { PropsWithSpread } from "../../types";
5
- export type PrefixedInputProps = PropsWithSpread<{
4
+ export type PrefixedInputProps = {
6
5
  /**
7
6
  * The immutable text that appears at the beginning of the input field.
8
7
  * This text is not editable by the user and visually appears inside the input.
9
8
  */
10
9
  immutableText: string;
11
- }, Omit<InputProps, "type">>;
12
- declare const PrefixedInput: ({ immutableText, ...props }: PrefixedInputProps) => ReactElement;
10
+ } & Omit<InputProps, "type">;
11
+ declare const PrefixedInput: ({ immutableText, disabled, className, wrapperClassName, label, error, help, caution, success, id, required, ...props }: PrefixedInputProps) => ReactElement;
13
12
  export default PrefixedInput;
@@ -4,60 +4,69 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
+ var _propTypes = _interopRequireDefault(require("prop-types"));
7
8
  var _react = _interopRequireWildcard(require("react"));
8
- var _Input = _interopRequireDefault(require("../Input"));
9
9
  var _classnames = _interopRequireDefault(require("classnames"));
10
+ var _Input = _interopRequireDefault(require("../Input"));
11
+ var _Field = _interopRequireDefault(require("../Field"));
10
12
  require("./PrefixedInput.scss");
11
- var _useListener = require("../../hooks/useListener");
12
- const _excluded = ["immutableText"];
13
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
+ const _excluded = ["immutableText", "disabled", "className", "wrapperClassName", "label", "error", "help", "caution", "success", "id", "required"];
14
14
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
15
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
16
  function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
16
17
  function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
17
18
  function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
18
- // export type PrefixedInputProps = Omit<InputProps, "type"> & {
19
- // /**
20
- // * The immutable text that appears at the beginning of the input field.
21
- // * This text is not editable by the user and visually appears inside the input.
22
- // */
23
- // immutableText: string;
24
- // };
25
-
26
19
  const PrefixedInput = _ref => {
27
20
  let {
28
- immutableText
21
+ immutableText,
22
+ disabled,
23
+ className,
24
+ wrapperClassName,
25
+ label,
26
+ error,
27
+ help,
28
+ caution,
29
+ success,
30
+ id,
31
+ required
29
32
  } = _ref,
30
33
  props = _objectWithoutProperties(_ref, _excluded);
31
- const prefixTextRef = (0, _react.useRef)(null);
32
- const inputWrapperRef = (0, _react.useRef)(null);
33
- const updatePadding = (0, _react.useCallback)(() => {
34
- var _inputWrapperRef$curr;
35
- const prefixElement = prefixTextRef.current;
36
- const inputElement = (_inputWrapperRef$curr = inputWrapperRef.current) === null || _inputWrapperRef$curr === void 0 ? void 0 : _inputWrapperRef$curr.querySelector("input");
37
- if (prefixElement && inputElement) {
38
- // Adjust the left padding of the input to be the same width as the immutable text.
39
- // This displays the user input and the unchangeable text together as one combined string.
40
- const prefixWidth = prefixElement.getBoundingClientRect().width;
41
- inputElement.style.paddingLeft = "".concat(prefixWidth, "px");
42
- }
43
- }, []);
44
- (0, _useListener.useListener)(window, updatePadding, "resize", true);
45
- (0, _react.useLayoutEffect)(() => {
46
- updatePadding();
47
- }, [immutableText, props.label, updatePadding]);
48
- return /*#__PURE__*/_react.default.createElement("div", {
49
- className: (0, _classnames.default)("prefixed-input", {
50
- "prefixed-input--with-label": !!props.label
51
- })
34
+ const defaultId = (0, _react.useId)();
35
+ const inputId = id || defaultId;
36
+ return /*#__PURE__*/_react.default.createElement(_Field.default, {
37
+ label: label,
38
+ error: error,
39
+ help: help,
40
+ caution: caution,
41
+ success: success,
42
+ required: required,
43
+ forId: inputId,
44
+ className: (0, _classnames.default)("p-prefixed-input", wrapperClassName)
52
45
  }, /*#__PURE__*/_react.default.createElement("div", {
53
- className: "prefixed-input__text",
54
- ref: prefixTextRef
55
- }, immutableText), /*#__PURE__*/_react.default.createElement("div", {
56
- ref: inputWrapperRef
57
- }, /*#__PURE__*/_react.default.createElement(_Input.default, _extends({}, props, {
58
- className: (0, _classnames.default)("prefixed-input__input", props.className),
46
+ className: (0, _classnames.default)("p-prefixed-input__flex-container", {
47
+ "is-disabled": disabled,
48
+ "is-error": !!error,
49
+ "is-caution": !!caution,
50
+ "is-success": !!success
51
+ })
52
+ }, /*#__PURE__*/_react.default.createElement("span", {
53
+ className: "p-prefixed-input__prefix"
54
+ }, immutableText), /*#__PURE__*/_react.default.createElement(_Input.default, _extends({}, props, {
55
+ id: inputId,
56
+ disabled: disabled,
57
+ required: required
58
+ // IMPORTANT: pass nulls so Input doesn't trigger its own Field wrapper
59
+ ,
60
+ label: null,
61
+ error: null,
62
+ help: null,
63
+ caution: null,
64
+ success: null,
59
65
  type: "text",
60
- wrapperClassName: (0, _classnames.default)("prefixed-input__wrapper", props.wrapperClassName)
66
+ className: (0, _classnames.default)("p-prefixed-input__input-field", className)
61
67
  }))));
62
68
  };
69
+ PrefixedInput.propTypes = {
70
+ immutableText: _propTypes.default.string.isRequired
71
+ };
63
72
  var _default = exports.default = PrefixedInput;
@@ -1,22 +1,68 @@
1
+ @use "sass:map";
1
2
  @import "vanilla-framework";
3
+ @include vf-b-placeholders;
2
4
 
3
- .prefixed-input {
4
- position: relative;
5
+ .p-prefixed-input {
6
+ @include vf-b-forms;
5
7
 
6
- .prefixed-input__input {
7
- padding-top: 0.25rem;
8
+ &__flex-container {
9
+ @extend %vf-input-elements;
10
+ align-items: center;
11
+ display: flex;
12
+
13
+ &:focus-within {
14
+ outline: 0.1875rem solid var(--vf-focus-outline-color);
15
+ outline-offset: -0.1875rem;
16
+ }
17
+
18
+ &.is-disabled {
19
+ cursor: not-allowed;
20
+ opacity: 0.33;
21
+ }
22
+
23
+ &.is-error {
24
+ background-color: var(--vf-color-background-negative-default);
25
+ border-bottom-color: var(--vf-color-border-negative);
26
+ }
27
+
28
+ &.is-success {
29
+ background-color: var(--vf-color-background-positive-default);
30
+ border-bottom-color: var(--vf-color-border-positive);
31
+ }
32
+
33
+ &.is-caution {
34
+ background-color: var(--vf-color-background-caution-default);
35
+ border-bottom-color: var(--vf-color-border-caution);
36
+ }
8
37
  }
9
38
 
10
- .prefixed-input__text {
11
- padding-left: $spv--large;
12
- padding-top: 0.3rem;
39
+ &__prefix {
13
40
  pointer-events: none;
14
- position: absolute;
41
+ user-select: none;
42
+ white-space: nowrap;
43
+ }
44
+
45
+ .p-form-validation {
46
+ flex: 1;
15
47
  }
16
48
 
17
- &--with-label {
18
- .prefixed-input__text {
19
- top: 2.5rem;
49
+ // Reset the actual <input> rendered by the Input component
50
+ .p-prefixed-input__input-field.p-form-validation__input {
51
+ background: transparent !important;
52
+ border: none !important;
53
+ box-shadow: none !important;
54
+ flex: 1;
55
+ height: 100%;
56
+ margin: 0 !important;
57
+ min-width: 0;
58
+ padding: 0 !important;
59
+
60
+ &:focus {
61
+ outline: none !important;
62
+ }
63
+
64
+ &:disabled {
65
+ cursor: not-allowed;
20
66
  }
21
67
  }
22
68
  }
@@ -7,5 +7,7 @@ export declare const Default: Story;
7
7
  export declare const WithLabel: Story;
8
8
  export declare const Disabled: Story;
9
9
  export declare const WithError: Story;
10
+ export declare const WithSuccess: Story;
11
+ export declare const WithCaution: Story;
10
12
  export declare const WithHelpText: Story;
11
13
  export declare const Required: Story;
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = exports.WithLabel = exports.WithHelpText = exports.WithError = exports.Required = exports.Disabled = exports.Default = void 0;
6
+ exports.default = exports.WithSuccess = exports.WithLabel = exports.WithHelpText = exports.WithError = exports.WithCaution = exports.Required = exports.Disabled = exports.Default = void 0;
7
7
  var _PrefixedInput = _interopRequireDefault(require("./PrefixedInput"));
8
8
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
9
  const meta = {
@@ -40,6 +40,22 @@ const WithError = exports.WithError = {
40
40
  error: "Invalid URL format"
41
41
  }
42
42
  };
43
+ const WithSuccess = exports.WithSuccess = {
44
+ args: {
45
+ immutableText: "https://",
46
+ label: "Website URL",
47
+ placeholder: "example.com",
48
+ success: "Valid URL format"
49
+ }
50
+ };
51
+ const WithCaution = exports.WithCaution = {
52
+ args: {
53
+ immutableText: "https://",
54
+ label: "Website URL",
55
+ placeholder: "example.com",
56
+ caution: "This URL might be problematic"
57
+ }
58
+ };
43
59
  const WithHelpText = exports.WithHelpText = {
44
60
  args: {
45
61
  immutableText: "User ID:",
@@ -1,13 +1,12 @@
1
1
  import { type ReactElement } from "react";
2
2
  import { type InputProps } from "../Input";
3
3
  import "./PrefixedInput.scss";
4
- import { PropsWithSpread } from "../../types";
5
- export type PrefixedInputProps = PropsWithSpread<{
4
+ export type PrefixedInputProps = {
6
5
  /**
7
6
  * The immutable text that appears at the beginning of the input field.
8
7
  * This text is not editable by the user and visually appears inside the input.
9
8
  */
10
9
  immutableText: string;
11
- }, Omit<InputProps, "type">>;
12
- declare const PrefixedInput: ({ immutableText, ...props }: PrefixedInputProps) => ReactElement;
10
+ } & Omit<InputProps, "type">;
11
+ declare const PrefixedInput: ({ immutableText, disabled, className, wrapperClassName, label, error, help, caution, success, id, required, ...props }: PrefixedInputProps) => ReactElement;
13
12
  export default PrefixedInput;
@@ -1,57 +1,64 @@
1
- var _excluded = ["immutableText"];
1
+ import _pt from "prop-types";
2
+ var _excluded = ["immutableText", "disabled", "className", "wrapperClassName", "label", "error", "help", "caution", "success", "id", "required"];
2
3
  function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
3
4
  function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
4
5
  function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
5
- import React from "react";
6
- import { useLayoutEffect, useRef, useCallback } from "react";
7
- import Input from "../Input";
6
+ import React, { useId } from "react";
8
7
  import classNames from "classnames";
8
+ import Input from "../Input";
9
+ import Field from "../Field";
9
10
  import "./PrefixedInput.scss";
10
- import { useListener } from "../../hooks/useListener";
11
-
12
- // export type PrefixedInputProps = Omit<InputProps, "type"> & {
13
- // /**
14
- // * The immutable text that appears at the beginning of the input field.
15
- // * This text is not editable by the user and visually appears inside the input.
16
- // */
17
- // immutableText: string;
18
- // };
19
-
20
11
  var PrefixedInput = _ref => {
21
12
  var {
22
- immutableText
13
+ immutableText,
14
+ disabled,
15
+ className,
16
+ wrapperClassName,
17
+ label,
18
+ error,
19
+ help,
20
+ caution,
21
+ success,
22
+ id,
23
+ required
23
24
  } = _ref,
24
25
  props = _objectWithoutProperties(_ref, _excluded);
25
- var prefixTextRef = useRef(null);
26
- var inputWrapperRef = useRef(null);
27
- var updatePadding = useCallback(() => {
28
- var _inputWrapperRef$curr;
29
- var prefixElement = prefixTextRef.current;
30
- var inputElement = (_inputWrapperRef$curr = inputWrapperRef.current) === null || _inputWrapperRef$curr === void 0 ? void 0 : _inputWrapperRef$curr.querySelector("input");
31
- if (prefixElement && inputElement) {
32
- // Adjust the left padding of the input to be the same width as the immutable text.
33
- // This displays the user input and the unchangeable text together as one combined string.
34
- var prefixWidth = prefixElement.getBoundingClientRect().width;
35
- inputElement.style.paddingLeft = "".concat(prefixWidth, "px");
36
- }
37
- }, []);
38
- useListener(window, updatePadding, "resize", true);
39
- useLayoutEffect(() => {
40
- updatePadding();
41
- }, [immutableText, props.label, updatePadding]);
42
- return /*#__PURE__*/React.createElement("div", {
43
- className: classNames("prefixed-input", {
44
- "prefixed-input--with-label": !!props.label
45
- })
26
+ var defaultId = useId();
27
+ var inputId = id || defaultId;
28
+ return /*#__PURE__*/React.createElement(Field, {
29
+ label: label,
30
+ error: error,
31
+ help: help,
32
+ caution: caution,
33
+ success: success,
34
+ required: required,
35
+ forId: inputId,
36
+ className: classNames("p-prefixed-input", wrapperClassName)
46
37
  }, /*#__PURE__*/React.createElement("div", {
47
- className: "prefixed-input__text",
48
- ref: prefixTextRef
49
- }, immutableText), /*#__PURE__*/React.createElement("div", {
50
- ref: inputWrapperRef
51
- }, /*#__PURE__*/React.createElement(Input, _extends({}, props, {
52
- className: classNames("prefixed-input__input", props.className),
38
+ className: classNames("p-prefixed-input__flex-container", {
39
+ "is-disabled": disabled,
40
+ "is-error": !!error,
41
+ "is-caution": !!caution,
42
+ "is-success": !!success
43
+ })
44
+ }, /*#__PURE__*/React.createElement("span", {
45
+ className: "p-prefixed-input__prefix"
46
+ }, immutableText), /*#__PURE__*/React.createElement(Input, _extends({}, props, {
47
+ id: inputId,
48
+ disabled: disabled,
49
+ required: required
50
+ // IMPORTANT: pass nulls so Input doesn't trigger its own Field wrapper
51
+ ,
52
+ label: null,
53
+ error: null,
54
+ help: null,
55
+ caution: null,
56
+ success: null,
53
57
  type: "text",
54
- wrapperClassName: classNames("prefixed-input__wrapper", props.wrapperClassName)
58
+ className: classNames("p-prefixed-input__input-field", className)
55
59
  }))));
56
60
  };
61
+ PrefixedInput.propTypes = {
62
+ immutableText: _pt.string.isRequired
63
+ };
57
64
  export default PrefixedInput;
@@ -1,22 +1,68 @@
1
+ @use "sass:map";
1
2
  @import "vanilla-framework";
3
+ @include vf-b-placeholders;
2
4
 
3
- .prefixed-input {
4
- position: relative;
5
+ .p-prefixed-input {
6
+ @include vf-b-forms;
5
7
 
6
- .prefixed-input__input {
7
- padding-top: 0.25rem;
8
+ &__flex-container {
9
+ @extend %vf-input-elements;
10
+ align-items: center;
11
+ display: flex;
12
+
13
+ &:focus-within {
14
+ outline: 0.1875rem solid var(--vf-focus-outline-color);
15
+ outline-offset: -0.1875rem;
16
+ }
17
+
18
+ &.is-disabled {
19
+ cursor: not-allowed;
20
+ opacity: 0.33;
21
+ }
22
+
23
+ &.is-error {
24
+ background-color: var(--vf-color-background-negative-default);
25
+ border-bottom-color: var(--vf-color-border-negative);
26
+ }
27
+
28
+ &.is-success {
29
+ background-color: var(--vf-color-background-positive-default);
30
+ border-bottom-color: var(--vf-color-border-positive);
31
+ }
32
+
33
+ &.is-caution {
34
+ background-color: var(--vf-color-background-caution-default);
35
+ border-bottom-color: var(--vf-color-border-caution);
36
+ }
8
37
  }
9
38
 
10
- .prefixed-input__text {
11
- padding-left: $spv--large;
12
- padding-top: 0.3rem;
39
+ &__prefix {
13
40
  pointer-events: none;
14
- position: absolute;
41
+ user-select: none;
42
+ white-space: nowrap;
43
+ }
44
+
45
+ .p-form-validation {
46
+ flex: 1;
15
47
  }
16
48
 
17
- &--with-label {
18
- .prefixed-input__text {
19
- top: 2.5rem;
49
+ // Reset the actual <input> rendered by the Input component
50
+ .p-prefixed-input__input-field.p-form-validation__input {
51
+ background: transparent !important;
52
+ border: none !important;
53
+ box-shadow: none !important;
54
+ flex: 1;
55
+ height: 100%;
56
+ margin: 0 !important;
57
+ min-width: 0;
58
+ padding: 0 !important;
59
+
60
+ &:focus {
61
+ outline: none !important;
62
+ }
63
+
64
+ &:disabled {
65
+ cursor: not-allowed;
20
66
  }
21
67
  }
22
68
  }
@@ -7,5 +7,7 @@ export declare const Default: Story;
7
7
  export declare const WithLabel: Story;
8
8
  export declare const Disabled: Story;
9
9
  export declare const WithError: Story;
10
+ export declare const WithSuccess: Story;
11
+ export declare const WithCaution: Story;
10
12
  export declare const WithHelpText: Story;
11
13
  export declare const Required: Story;
@@ -33,6 +33,22 @@ export var WithError = {
33
33
  error: "Invalid URL format"
34
34
  }
35
35
  };
36
+ export var WithSuccess = {
37
+ args: {
38
+ immutableText: "https://",
39
+ label: "Website URL",
40
+ placeholder: "example.com",
41
+ success: "Valid URL format"
42
+ }
43
+ };
44
+ export var WithCaution = {
45
+ args: {
46
+ immutableText: "https://",
47
+ label: "Website URL",
48
+ placeholder: "example.com",
49
+ caution: "This URL might be problematic"
50
+ }
51
+ };
36
52
  export var WithHelpText = {
37
53
  args: {
38
54
  immutableText: "User ID:",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonical/react-components",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "author": {
@@ -96,7 +96,7 @@
96
96
  "tsc-alias": "1.8.10",
97
97
  "typescript": "5.7.3",
98
98
  "typescript-eslint": "8.24.1",
99
- "vanilla-framework": "4.45.0",
99
+ "vanilla-framework": "4.46.0",
100
100
  "wait-on": "9.0.3",
101
101
  "webpack": "5.105.0"
102
102
  },