@obosbbl/grunnmuren-react 0.1.1 → 0.2.2

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.
@@ -7,5 +7,7 @@ export interface TextAreaProps extends React.ComponentPropsWithoutRef<'textarea'
7
7
  error?: string;
8
8
  /** Label for the form control */
9
9
  label: string;
10
+ /** Automatically valdiate the form control using the HTML constraint validation API. @default true */
11
+ validate?: boolean;
10
12
  }
11
13
  export declare const TextArea: import("react").ForwardRefExoticComponent<TextAreaProps & import("react").RefAttributes<HTMLTextAreaElement>>;
@@ -8,5 +8,7 @@ export interface TextFieldProps extends React.ComponentPropsWithoutRef<'input'>
8
8
  /** Label for the form control */
9
9
  label: string;
10
10
  prefix?: string;
11
+ /** Automatically valdiate the form control using the HTML constraint validation API. @default true */
12
+ validate?: boolean;
11
13
  }
12
14
  export declare const TextField: import("react").ForwardRefExoticComponent<TextFieldProps & import("react").RefAttributes<HTMLInputElement>>;
@@ -248,25 +248,33 @@ function useMedia(query) {
248
248
  return matches;
249
249
  }
250
250
  const useScreenMaxWidthMd = () => useMedia("(max-width: 767.9px)");
251
- function useFormControlValidity(ref) {
251
+ function useFormControlValidity(ref, enabled = true) {
252
252
  const [validity, setValidity] = useState("indeterminate");
253
- const onBlur = (event) => {
253
+ const [validationMessage, setValidationMessage] = useState();
254
+ const onBlur = useCallback((event) => {
254
255
  const isValid = event.target.checkValidity();
255
- if (isValid) {
256
+ if (isValid && enabled) {
256
257
  setValidity("valid");
258
+ setValidationMessage(void 0);
257
259
  }
258
- };
259
- const onInput = (event) => {
260
- if (validity !== "indeterminate") {
260
+ }, [enabled]);
261
+ const onInput = useCallback((event) => {
262
+ if (enabled && validity !== "indeterminate") {
261
263
  const isValid = event.target.checkValidity();
262
264
  if (isValid) {
263
265
  setValidity("valid");
266
+ setValidationMessage(void 0);
264
267
  }
265
268
  }
266
- };
267
- const onInvalid = () => {
268
- setValidity("invalid");
269
- };
269
+ }, [enabled, validity]);
270
+ const onInvalid = useCallback((event) => {
271
+ if (enabled) {
272
+ event.preventDefault();
273
+ const message = event.target.validationMessage;
274
+ setValidationMessage(message);
275
+ setValidity("invalid");
276
+ }
277
+ }, [enabled]);
270
278
  useEffect(() => {
271
279
  const { current } = ref;
272
280
  current == null ? void 0 : current.addEventListener("blur", onBlur);
@@ -278,7 +286,7 @@ function useFormControlValidity(ref) {
278
286
  current == null ? void 0 : current.removeEventListener("invalid", onInvalid);
279
287
  };
280
288
  });
281
- return { validity };
289
+ return { validity, validationMessage };
282
290
  }
283
291
  function useFallbackId(id) {
284
292
  const generatedId = useId();
@@ -985,21 +993,25 @@ const TextArea = forwardRef((props, ref) => {
985
993
  error,
986
994
  id: idProp,
987
995
  label,
988
- required
996
+ required,
997
+ validate = true
989
998
  } = _a, rest = __objRest(_a, [
990
999
  "description",
991
1000
  "error",
992
1001
  "id",
993
1002
  "label",
994
- "required"
1003
+ "required",
1004
+ "validate"
995
1005
  ]);
996
1006
  const ownRef = useRef(null);
997
1007
  const {
998
- validity
999
- } = useFormControlValidity(ownRef);
1008
+ validity,
1009
+ validationMessage
1010
+ } = useFormControlValidity(ownRef, validate);
1000
1011
  const id = useFallbackId(idProp);
1001
- const helpTextId = id + "-help";
1002
- const errorMsgId = id + "-err-msg";
1012
+ const helpTextId = id + "help";
1013
+ const errorMsgId = id + "err";
1014
+ const errorMsg = error != null ? error : validationMessage;
1003
1015
  return /* @__PURE__ */ jsxs("div", {
1004
1016
  className: "grid gap-2",
1005
1017
  children: [/* @__PURE__ */ jsx(FormLabel, {
@@ -1020,9 +1032,9 @@ const TextArea = forwardRef((props, ref) => {
1020
1032
  })), description && /* @__PURE__ */ jsx(FormHelperText, {
1021
1033
  id: helpTextId,
1022
1034
  children: description
1023
- }), error && /* @__PURE__ */ jsx(FormErrorMessage, {
1035
+ }), errorMsg && /* @__PURE__ */ jsx(FormErrorMessage, {
1024
1036
  id: errorMsgId,
1025
- children: error
1037
+ children: errorMsg
1026
1038
  })]
1027
1039
  });
1028
1040
  });
@@ -1033,22 +1045,26 @@ const TextField = forwardRef((props, ref) => {
1033
1045
  id: idProp,
1034
1046
  label,
1035
1047
  required,
1036
- type = "text"
1048
+ type = "text",
1049
+ validate = true
1037
1050
  } = _a, rest = __objRest(_a, [
1038
1051
  "description",
1039
1052
  "error",
1040
1053
  "id",
1041
1054
  "label",
1042
1055
  "required",
1043
- "type"
1056
+ "type",
1057
+ "validate"
1044
1058
  ]);
1045
1059
  const ownRef = useRef(null);
1046
1060
  const {
1047
- validity
1048
- } = useFormControlValidity(ownRef);
1061
+ validity,
1062
+ validationMessage
1063
+ } = useFormControlValidity(ownRef, validate);
1049
1064
  const id = useFallbackId(idProp);
1050
- const helpTextId = id + "-help";
1051
- const errorMsgId = id + "-err";
1065
+ const helpTextId = id + "help";
1066
+ const errorMsgId = id + "err";
1067
+ const errorMsg = error != null ? error : validationMessage;
1052
1068
  return /* @__PURE__ */ jsxs("div", {
1053
1069
  className: "grid gap-2",
1054
1070
  children: [/* @__PURE__ */ jsx(FormLabel, {
@@ -1069,9 +1085,9 @@ const TextField = forwardRef((props, ref) => {
1069
1085
  })), description && /* @__PURE__ */ jsx(FormHelperText, {
1070
1086
  id: helpTextId,
1071
1087
  children: description
1072
- }), error && /* @__PURE__ */ jsx(FormErrorMessage, {
1088
+ }), errorMsg && /* @__PURE__ */ jsx(FormErrorMessage, {
1073
1089
  id: errorMsgId,
1074
- children: error
1090
+ children: errorMsg
1075
1091
  })]
1076
1092
  });
1077
1093
  });
@@ -8,7 +8,8 @@ declare type Validity = 'indeterminate' | 'invalid' | 'valid';
8
8
  */
9
9
  export declare function useFormControlValidity(ref: RefObject<HTMLElement & {
10
10
  checkValidity(): boolean;
11
- }>): {
11
+ }>, enabled?: boolean): {
12
12
  validity: Validity;
13
+ validationMessage: string | undefined;
13
14
  };
14
15
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@obosbbl/grunnmuren-react",
3
- "version": "0.1.1",
3
+ "version": "0.2.2",
4
4
  "description": "OBOS Grunnmuren design system React components",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -18,12 +18,12 @@
18
18
  "types": "./dist/index.d.ts",
19
19
  "devDependencies": {
20
20
  "@babel/core": "7.17.10",
21
- "@storybook/addon-controls": "6.4.22",
22
- "@storybook/addon-docs": "6.4.22",
21
+ "@storybook/addon-controls": "6.5.4",
22
+ "@storybook/addon-docs": "6.5.4",
23
23
  "@storybook/addon-postcss": "2.0.0",
24
- "@storybook/builder-webpack5": "6.4.22",
25
- "@storybook/manager-webpack5": "6.4.22",
26
- "@storybook/react": "6.4.22",
24
+ "@storybook/builder-webpack5": "6.5.4",
25
+ "@storybook/manager-webpack5": "6.5.4",
26
+ "@storybook/react": "6.5.4",
27
27
  "@types/react": "18.0.9",
28
28
  "@types/react-dom": "18.0.3",
29
29
  "@vitejs/plugin-react": "1.3.2",
@@ -31,11 +31,12 @@
31
31
  "react": "18.1.0",
32
32
  "react-dom": "18.1.0",
33
33
  "tailwindcss": "3.0.24",
34
- "vite": "2.9.9"
34
+ "vite": "2.9.9",
35
+ "webpack": "5.72.1"
35
36
  },
36
37
  "dependencies": {
37
38
  "@obosbbl/grunnmuren-icons": "^0.3.0",
38
- "@obosbbl/grunnmuren-tailwind": "^0.2.1",
39
+ "@obosbbl/grunnmuren-tailwind": "^0.3.1",
39
40
  "clsx": "1.1.1"
40
41
  },
41
42
  "peerDependencies": {