@equinor/apollo-utils 0.1.2 → 0.1.3-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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @equinor/apollo-utils
2
2
 
3
+ ## 0.1.3
4
+
5
+ ### Patch Changes
6
+
7
+ - e1354f0: Move edit cells to apollo-utils
8
+ - Updated dependencies [e1354f0]
9
+ - @equinor/apollo-components@3.1.2
10
+
3
11
  ## 0.1.2
4
12
 
5
13
  ### Patch Changes
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@equinor/apollo-utils",
3
3
  "main": "src/index.ts",
4
4
  "types": "src/index.ts",
5
- "version": "0.1.2",
5
+ "version": "0.1.3-beta.0",
6
6
  "license": "MIT",
7
7
  "scripts": {
8
8
  "build": "tsup src/index.ts --format esm,cjs --dts --external react",
@@ -17,7 +17,6 @@
17
17
  "access": "public"
18
18
  },
19
19
  "dependencies": {
20
- "apollo-common": "*",
21
20
  "jotai": "^2.0.1",
22
21
  "tsup": "^6.6.3",
23
22
  "typescript": "^4.9.5",
@@ -26,9 +25,11 @@
26
25
  "peerDependencies": {
27
26
  "react": "^18.2.0",
28
27
  "react-dom": "^18.2.0",
29
- "react-hook-form": "^7.43.8"
28
+ "react-hook-form": "^7.43.8",
29
+ "@equinor/apollo-components": "^3.1.0"
30
30
  },
31
31
  "devDependencies": {
32
+ "@equinor/apollo-components": "*",
32
33
  "@testing-library/react": "^13.4.0",
33
34
  "@testing-library/react-hooks": "^8.0.1",
34
35
  "@vitest/ui": "^0.28.5",
@@ -1,6 +1,6 @@
1
+ import { TypographyCustom } from '@equinor/apollo-components'
1
2
  import { TextField } from '@equinor/eds-core-react'
2
3
  import { CellContext } from '@tanstack/react-table'
3
- import { TypographyCustom } from 'apollo-common'
4
4
  import { ChangeEvent, useMemo } from 'react'
5
5
  import { Controller, useFormContext } from 'react-hook-form'
6
6
  import styled from 'styled-components'
@@ -1,6 +1,6 @@
1
+ import { TypographyCustom } from '@equinor/apollo-components'
1
2
  import { Autocomplete } from '@equinor/eds-core-react'
2
3
  import { CellContext } from '@tanstack/react-table'
3
- import { TypographyCustom } from 'apollo-common'
4
4
  import { Controller, useFormContext } from 'react-hook-form'
5
5
  import styled from 'styled-components'
6
6
  import { FormMeta, useEditMode } from '../form-meta'
@@ -1,6 +1,6 @@
1
+ import { TypographyCustom } from '@equinor/apollo-components'
1
2
  import { TextField } from '@equinor/eds-core-react'
2
3
  import { CellContext } from '@tanstack/react-table'
3
- import { TypographyCustom } from 'apollo-common'
4
4
  import { ChangeEvent } from 'react'
5
5
  import { Controller, useFormContext } from 'react-hook-form'
6
6
  import styled from 'styled-components'
@@ -1,6 +1,6 @@
1
+ import { PopoverCell, stopPropagation } from '@equinor/apollo-components'
1
2
  import { Button, Dialog as EDS, Icon, TextField } from '@equinor/eds-core-react'
2
3
  import { CellContext } from '@tanstack/react-table'
3
- import { PopoverCell, stopPropagation } from 'apollo-common'
4
4
  import { ChangeEvent, useState } from 'react'
5
5
  import { Controller, useFormContext } from 'react-hook-form'
6
6
  import styled from 'styled-components'
@@ -8,11 +8,11 @@ import { FormMeta, useEditMode } from '../form-meta'
8
8
  import { getHelperTextProps } from './utils'
9
9
 
10
10
  interface EdtiableTextAreaProps<T extends FormMeta> extends CellContext<T, string> {
11
- dialogTitle: string
11
+ title: string
12
12
  }
13
13
 
14
14
  export function EditableTextAreaCell<T extends FormMeta>(props: EdtiableTextAreaProps<T>) {
15
- const { dialogTitle, ...context } = props
15
+ const { title, ...context } = props
16
16
 
17
17
  const [textareaValue, setTextareaValue] = useState<string>(context.getValue())
18
18
  const [open, setOpen] = useState(false)
@@ -22,7 +22,7 @@ export function EditableTextAreaCell<T extends FormMeta>(props: EdtiableTextArea
22
22
  const name = context.column.id
23
23
 
24
24
  if (!editMode)
25
- return <PopoverCell id={name + 'popover'} value={context.getValue()} title="Comment" />
25
+ return <PopoverCell id={name + 'popover'} value={context.getValue()} title={title} />
26
26
 
27
27
  const openDialog = () => setOpen(true)
28
28
  const closeDialog = () => setOpen(false)
@@ -64,7 +64,7 @@ export function EditableTextAreaCell<T extends FormMeta>(props: EdtiableTextArea
64
64
  style={{ width: 'min(50rem, calc(100vw - 4rem))' }}
65
65
  >
66
66
  <EDS.Header>
67
- <EDS.Title>{dialogTitle}</EDS.Title>
67
+ <EDS.Title>{title}</EDS.Title>
68
68
  </EDS.Header>
69
69
  <EDS.Content>
70
70
  <TextField
@@ -1,6 +1,6 @@
1
+ import { TypographyCustom } from '@equinor/apollo-components'
1
2
  import { TextField } from '@equinor/eds-core-react'
2
3
  import { CellContext } from '@tanstack/react-table'
3
- import { TypographyCustom } from 'apollo-common'
4
4
  import { Controller, FieldPath, useFormContext } from 'react-hook-form'
5
5
  import styled from 'styled-components'
6
6
  import { FormMeta, useEditMode } from '../form-meta'
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
- export * from 'apollo-common'
1
+ export * from './edit-cells'
2
+ export * from './form-meta'
2
3
  export * from './jotai-form'
3
4
  export * from './zod-validation'
@@ -1,14 +0,0 @@
1
- $ tsup src/index.ts --format esm,cjs --dts --external react
2
- CLI Building entry: src/index.ts
3
- CLI Using tsconfig: tsconfig.json
4
- CLI tsup v6.6.3
5
- CLI Target: es6
6
- ESM Build start
7
- CJS Build start
8
- CJS dist/index.js 4.83 KB
9
- CJS ⚡️ Build success in 103ms
10
- ESM dist/index.mjs 3.53 KB
11
- ESM ⚡️ Build success in 107ms
12
- DTS Build start
13
- DTS ⚡️ Build success in 6256ms
14
- DTS dist/index.d.ts 2.03 KB
package/dist/index.d.ts DELETED
@@ -1,47 +0,0 @@
1
- export * from 'apollo-common';
2
- import { PrimitiveAtom } from 'jotai';
3
- import { AtomFamily } from 'jotai/vanilla/utils/atomFamily';
4
- import { z } from 'zod';
5
-
6
- type ValidationErrorMap<T> = Map<keyof T, {
7
- message: string;
8
- code: string;
9
- }>;
10
-
11
- declare function createValidator<S extends z.ZodTypeAny>(schema: S): {
12
- validate: <E extends z.TypeOf<S>>(entity: E) => ValidationErrorMap<E> | undefined;
13
- validateAsync: <E_1 extends z.TypeOf<S>>(entity: z.infer<typeof schema>) => Promise<ValidationErrorMap<E_1> | undefined>;
14
- getSchema(): S;
15
- };
16
-
17
- type FormState<T> = {
18
- status: 'editing' | 'pending';
19
- values: T;
20
- errors?: ValidationErrorMap<T>;
21
- isValid?: boolean;
22
- };
23
- type FormFamilyParam = {
24
- id: number | string;
25
- } & Record<string, unknown>;
26
-
27
- declare function createFormFamily<E extends Record<string, unknown>>(): AtomFamily<FormFamilyParam, PrimitiveAtom<FormState<E> | undefined> & {
28
- init: FormState<E> | undefined;
29
- }>;
30
- type FormFamily<T> = AtomFamily<FormFamilyParam, PrimitiveAtom<FormState<T> | undefined>>;
31
- type FormValidator = ReturnType<typeof createValidator>;
32
- type UseFormFamilyUtilsOptions = {
33
- validator?: FormValidator;
34
- };
35
- declare function useFormFamilyUtils<T>(family: FormFamily<T>, options?: UseFormFamilyUtilsOptions): {
36
- useFormStateAtom: (param: FormFamilyParam) => [FormState<T> | undefined, (args_0: FormState<T> | ((prev: FormState<T> | undefined) => FormState<T> | undefined) | undefined) => void];
37
- useFormState: (param: FormFamilyParam) => FormState<T> | undefined;
38
- useUpdateFormMutation: (param: FormFamilyParam) => (update: Partial<T>) => void;
39
- useFormMutations(param: FormFamilyParam): {
40
- update: (update: Partial<T>) => void;
41
- initializeForm: (entity: T) => void;
42
- resetForm: () => void;
43
- };
44
- };
45
- declare function useFormFamilyMutation<T>(family: FormFamily<T>, param: FormFamilyParam, validator?: FormValidator): (update: Partial<T>) => void;
46
-
47
- export { FormFamilyParam, FormState, ValidationErrorMap, createFormFamily, createValidator, useFormFamilyMutation, useFormFamilyUtils };
package/dist/index.js DELETED
@@ -1,149 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __spreadValues = (a, b) => {
10
- for (var prop in b || (b = {}))
11
- if (__hasOwnProp.call(b, prop))
12
- __defNormalProp(a, prop, b[prop]);
13
- if (__getOwnPropSymbols)
14
- for (var prop of __getOwnPropSymbols(b)) {
15
- if (__propIsEnum.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- }
18
- return a;
19
- };
20
- var __export = (target, all) => {
21
- for (var name in all)
22
- __defProp(target, name, { get: all[name], enumerable: true });
23
- };
24
- var __copyProps = (to, from, except, desc) => {
25
- if (from && typeof from === "object" || typeof from === "function") {
26
- for (let key of __getOwnPropNames(from))
27
- if (!__hasOwnProp.call(to, key) && key !== except)
28
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
29
- }
30
- return to;
31
- };
32
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
33
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
34
- var __async = (__this, __arguments, generator) => {
35
- return new Promise((resolve, reject) => {
36
- var fulfilled = (value) => {
37
- try {
38
- step(generator.next(value));
39
- } catch (e) {
40
- reject(e);
41
- }
42
- };
43
- var rejected = (value) => {
44
- try {
45
- step(generator.throw(value));
46
- } catch (e) {
47
- reject(e);
48
- }
49
- };
50
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
51
- step((generator = generator.apply(__this, __arguments)).next());
52
- });
53
- };
54
-
55
- // src/index.ts
56
- var src_exports = {};
57
- __export(src_exports, {
58
- createFormFamily: () => createFormFamily,
59
- createValidator: () => createValidator,
60
- useFormFamilyMutation: () => useFormFamilyMutation,
61
- useFormFamilyUtils: () => useFormFamilyUtils
62
- });
63
- module.exports = __toCommonJS(src_exports);
64
- __reExport(src_exports, require("apollo-common"), module.exports);
65
-
66
- // src/jotai-form/formUtils.ts
67
- var import_jotai = require("jotai");
68
- var import_utils = require("jotai/utils");
69
- function createFormFamily() {
70
- return (0, import_utils.atomFamily)(
71
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
72
- (_param) => (0, import_jotai.atom)(void 0),
73
- (a, b) => a.id === b.id
74
- );
75
- }
76
- function useFormFamilyUtils(family, options = {}) {
77
- return {
78
- useFormStateAtom: (param) => (0, import_jotai.useAtom)(family(param)),
79
- useFormState: (param) => (0, import_jotai.useAtomValue)(family(param)),
80
- useUpdateFormMutation: (param) => {
81
- return useFormFamilyMutation(family, param, options.validator);
82
- },
83
- useFormMutations(param) {
84
- const mutateAtom = (0, import_jotai.useSetAtom)(family(param));
85
- return {
86
- update: useFormFamilyMutation(family, param, options.validator),
87
- initializeForm: (entity) => mutateAtom({
88
- status: "editing",
89
- values: entity,
90
- isValid: true
91
- }),
92
- resetForm: () => mutateAtom(void 0)
93
- };
94
- }
95
- };
96
- }
97
- function useFormFamilyMutation(family, param, validator) {
98
- const mutate = (0, import_jotai.useSetAtom)(family(param));
99
- return (update) => {
100
- return mutate((previous) => {
101
- if (!previous)
102
- return;
103
- const updatedValues = __spreadValues(__spreadValues({}, previous.values), update);
104
- const errors = validator == null ? void 0 : validator.validate(updatedValues);
105
- return {
106
- status: "editing",
107
- values: updatedValues,
108
- errors,
109
- isValid: !errors
110
- };
111
- });
112
- };
113
- }
114
-
115
- // src/zod-validation/utils.ts
116
- function createValidator(schema) {
117
- return {
118
- validate: (entity) => {
119
- const validation = schema.safeParse(entity);
120
- if (validation.success)
121
- return void 0;
122
- return prepareErrors(validation);
123
- },
124
- validateAsync: (entity) => __async(this, null, function* () {
125
- const validation = yield schema.safeParseAsync(entity);
126
- if (validation.success)
127
- return void 0;
128
- return prepareErrors(validation);
129
- }),
130
- getSchema() {
131
- return schema;
132
- }
133
- };
134
- }
135
- function prepareErrors(errorValidation) {
136
- return new Map(
137
- errorValidation.error.errors.map((error) => [
138
- error.path[0],
139
- { message: error.message, code: error.code }
140
- ])
141
- );
142
- }
143
- // Annotate the CommonJS export names for ESM import in node:
144
- 0 && (module.exports = {
145
- createFormFamily,
146
- createValidator,
147
- useFormFamilyMutation,
148
- useFormFamilyUtils
149
- });
package/dist/index.mjs DELETED
@@ -1,123 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
- var __hasOwnProp = Object.prototype.hasOwnProperty;
4
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __spreadValues = (a, b) => {
7
- for (var prop in b || (b = {}))
8
- if (__hasOwnProp.call(b, prop))
9
- __defNormalProp(a, prop, b[prop]);
10
- if (__getOwnPropSymbols)
11
- for (var prop of __getOwnPropSymbols(b)) {
12
- if (__propIsEnum.call(b, prop))
13
- __defNormalProp(a, prop, b[prop]);
14
- }
15
- return a;
16
- };
17
- var __async = (__this, __arguments, generator) => {
18
- return new Promise((resolve, reject) => {
19
- var fulfilled = (value) => {
20
- try {
21
- step(generator.next(value));
22
- } catch (e) {
23
- reject(e);
24
- }
25
- };
26
- var rejected = (value) => {
27
- try {
28
- step(generator.throw(value));
29
- } catch (e) {
30
- reject(e);
31
- }
32
- };
33
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
34
- step((generator = generator.apply(__this, __arguments)).next());
35
- });
36
- };
37
-
38
- // src/index.ts
39
- export * from "apollo-common";
40
-
41
- // src/jotai-form/formUtils.ts
42
- import { atom, useAtom, useAtomValue, useSetAtom } from "jotai";
43
- import { atomFamily } from "jotai/utils";
44
- function createFormFamily() {
45
- return atomFamily(
46
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
47
- (_param) => atom(void 0),
48
- (a, b) => a.id === b.id
49
- );
50
- }
51
- function useFormFamilyUtils(family, options = {}) {
52
- return {
53
- useFormStateAtom: (param) => useAtom(family(param)),
54
- useFormState: (param) => useAtomValue(family(param)),
55
- useUpdateFormMutation: (param) => {
56
- return useFormFamilyMutation(family, param, options.validator);
57
- },
58
- useFormMutations(param) {
59
- const mutateAtom = useSetAtom(family(param));
60
- return {
61
- update: useFormFamilyMutation(family, param, options.validator),
62
- initializeForm: (entity) => mutateAtom({
63
- status: "editing",
64
- values: entity,
65
- isValid: true
66
- }),
67
- resetForm: () => mutateAtom(void 0)
68
- };
69
- }
70
- };
71
- }
72
- function useFormFamilyMutation(family, param, validator) {
73
- const mutate = useSetAtom(family(param));
74
- return (update) => {
75
- return mutate((previous) => {
76
- if (!previous)
77
- return;
78
- const updatedValues = __spreadValues(__spreadValues({}, previous.values), update);
79
- const errors = validator == null ? void 0 : validator.validate(updatedValues);
80
- return {
81
- status: "editing",
82
- values: updatedValues,
83
- errors,
84
- isValid: !errors
85
- };
86
- });
87
- };
88
- }
89
-
90
- // src/zod-validation/utils.ts
91
- function createValidator(schema) {
92
- return {
93
- validate: (entity) => {
94
- const validation = schema.safeParse(entity);
95
- if (validation.success)
96
- return void 0;
97
- return prepareErrors(validation);
98
- },
99
- validateAsync: (entity) => __async(this, null, function* () {
100
- const validation = yield schema.safeParseAsync(entity);
101
- if (validation.success)
102
- return void 0;
103
- return prepareErrors(validation);
104
- }),
105
- getSchema() {
106
- return schema;
107
- }
108
- };
109
- }
110
- function prepareErrors(errorValidation) {
111
- return new Map(
112
- errorValidation.error.errors.map((error) => [
113
- error.path[0],
114
- { message: error.message, code: error.code }
115
- ])
116
- );
117
- }
118
- export {
119
- createFormFamily,
120
- createValidator,
121
- useFormFamilyMutation,
122
- useFormFamilyUtils
123
- };