@thecb/components 10.7.3-beta.0 → 10.7.4-beta.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thecb/components",
3
- "version": "10.7.3-beta.0",
3
+ "version": "10.7.4-beta.0",
4
4
  "description": "Common lib for CityBase react components",
5
5
  "main": "dist/index.cjs.js",
6
6
  "typings": "dist/index.d.ts",
@@ -1,4 +1,4 @@
1
- import React, { useState, useContext } from "react";
1
+ import React, { useState, useContext, useEffect } from "react";
2
2
  import styled, { ThemeContext, css } from "styled-components";
3
3
  import { FormattedInput } from "formatted-input";
4
4
  import { fallbackValues } from "./FormLayouts.theme.js";
@@ -120,22 +120,35 @@ const FormInput = ({
120
120
  ...props
121
121
  }) => {
122
122
  const [showPassword, setShowPassword] = useState(false);
123
+ const [initialValue, setInitialValue] = useState(field?.rawValue || "");
123
124
  const { isMobile } = useContext(ThemeContext);
124
125
 
125
- const setValue = value => {
126
- if (removeFromValue !== undefined) {
127
- return fieldActions.set(value.replace(removeFromValue, ""));
126
+ const setValue = currentValue => {
127
+ if (currentValue !== initialValue) {
128
+ setInitialValue(currentValue);
128
129
  }
129
- return fieldActions.set(value);
130
130
  };
131
131
 
132
- const handleOnBlur = value => {
133
- // Sets the empty value to trigger a validation error if it's required.
134
- if (isRequired && value === "") {
135
- setValue("");
132
+ const handleBlur = currentValue => {
133
+ if (currentValue === "" || currentValue !== initialValue) {
134
+ if (currentValue && removeFromValue !== undefined) {
135
+ fieldActions.set(currentValue.replace(removeFromValue, ""));
136
+ } else {
137
+ fieldActions.set(currentValue);
138
+ }
136
139
  }
137
140
  };
138
141
 
142
+ useEffect(() => {
143
+ if (initialValue) {
144
+ if (removeFromValue !== undefined) {
145
+ fieldActions.set(initialValue.replace(removeFromValue, ""));
146
+ } else {
147
+ fieldActions.set(initialValue);
148
+ }
149
+ }
150
+ }, [initialValue]);
151
+
139
152
  return (
140
153
  <Stack childGap="0.25rem">
141
154
  <Box padding="0">
@@ -211,8 +224,9 @@ const FormInput = ({
211
224
  (field.dirty && field.hasErrors) ||
212
225
  (field.hasErrors && showErrors)
213
226
  }
227
+ onFocus={e => setValue(e.target?.value)}
214
228
  onChange={value => setValue(value)}
215
- onBlur={e => handleOnBlur(e.target.value)}
229
+ onBlur={e => handleBlur(e.target.value)}
216
230
  type={type}
217
231
  value={field.rawValue}
218
232
  pattern={isNum ? "[0-9]*" : ""}
@@ -240,7 +254,7 @@ const FormInput = ({
240
254
  (field.hasErrors && showErrors)
241
255
  }
242
256
  onChange={e => setValue(e.target.value)}
243
- onBlur={e => handleOnBlur(e.target.value)}
257
+ onBlur={e => handleBlur(e.target.value)}
244
258
  type={type === "password" && showPassword ? "text" : type}
245
259
  value={field.rawValue}
246
260
  pattern={isNum ? "[0-9]*" : ""}
@@ -134,6 +134,7 @@ const PaymentFormCard = ({
134
134
  onKeyDown={e => e.key === "Enter" && handleSubmit(e)}
135
135
  isNum
136
136
  autocompleteValue="cc-number"
137
+ name="cc-number"
137
138
  isRequired={true}
138
139
  />
139
140
  <FormInputRow