@equinor/apollo-utils 0.1.3-beta.1 → 0.1.3-beta.3

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
@@ -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.3-beta.1",
5
+ "version": "0.1.3-beta.3",
6
6
  "license": "MIT",
7
7
  "scripts": {
8
8
  "build": "tsup src/index.ts --format esm,cjs --dts --external react",
@@ -27,11 +27,9 @@
27
27
  "react-dom": "^18.2.0",
28
28
  "react-hook-form": "^7.43.8",
29
29
  "@equinor/eds-core-react": "^0.27.0",
30
- "styled-components": "^5.3.7",
31
- "@equinor/apollo-components": "^3.1.0"
30
+ "styled-components": "^5.3.7"
32
31
  },
33
32
  "devDependencies": {
34
- "@equinor/apollo-components": "*",
35
33
  "@testing-library/react": "^13.4.0",
36
34
  "@testing-library/react-hooks": "^8.0.1",
37
35
  "@vitest/ui": "^0.28.5",
@@ -1,9 +1,9 @@
1
- import { TypographyCustom } from '@equinor/apollo-components'
2
1
  import { Autocomplete } from '@equinor/eds-core-react'
3
2
  import { CellContext } from '@tanstack/react-table'
4
3
  import { Controller, useFormContext } from 'react-hook-form'
5
4
  import styled from 'styled-components'
6
5
  import { FormMeta, useEditMode } from '../form-meta'
6
+ import { TypographyCustom } from './TypographyCustom'
7
7
 
8
8
  export interface Option {
9
9
  label: string
@@ -1,10 +1,10 @@
1
- import { TypographyCustom } from '@equinor/apollo-components'
2
1
  import { TextField } from '@equinor/eds-core-react'
3
2
  import { CellContext } from '@tanstack/react-table'
4
3
  import { ChangeEvent } from 'react'
5
4
  import { Controller, useFormContext } from 'react-hook-form'
6
5
  import styled from 'styled-components'
7
6
  import { FormMeta, useEditMode } from '../form-meta'
7
+ import { TypographyCustom } from './TypographyCustom'
8
8
  import { getHelperTextProps } from './utils'
9
9
 
10
10
  export function EditableNumberCell<T extends FormMeta>(context: CellContext<T, number>) {
@@ -1,11 +1,11 @@
1
- import { PopoverCell, stopPropagation } from '@equinor/apollo-components'
2
1
  import { Button, Dialog as EDS, Icon, TextField } from '@equinor/eds-core-react'
3
2
  import { CellContext } from '@tanstack/react-table'
4
3
  import { ChangeEvent, useState } from 'react'
5
4
  import { Controller, useFormContext } from 'react-hook-form'
6
5
  import styled from 'styled-components'
7
6
  import { FormMeta, useEditMode } from '../form-meta'
8
- import { getHelperTextProps } from './utils'
7
+ import { PopoverCell } from './PopoverCell'
8
+ import { getHelperTextProps, stopPropagation } from './utils'
9
9
 
10
10
  interface EdtiableTextAreaProps<T extends FormMeta> extends CellContext<T, string> {
11
11
  title: string
@@ -1,9 +1,9 @@
1
- import { TypographyCustom } from '@equinor/apollo-components'
2
1
  import { TextField } from '@equinor/eds-core-react'
3
2
  import { CellContext } from '@tanstack/react-table'
4
3
  import { Controller, FieldPath, useFormContext } from 'react-hook-form'
5
4
  import styled from 'styled-components'
6
5
  import { FormMeta, useEditMode } from '../form-meta'
6
+ import { TypographyCustom } from './TypographyCustom'
7
7
  import { getHelperTextProps } from './utils'
8
8
 
9
9
  export function EditableTextFieldCell<T extends FormMeta>(context: CellContext<T, unknown>) {
@@ -0,0 +1,42 @@
1
+ import { Popover, Typography } from '@equinor/eds-core-react'
2
+ import { ReactNode, useRef, useState } from 'react'
3
+ import { TypographyCustom } from './TypographyCustom'
4
+ import { stopPropagation } from './utils'
5
+
6
+ interface PopoverCellProps {
7
+ id: string
8
+ value: string
9
+ title?: string | JSX.Element | ReactNode
10
+ }
11
+
12
+ export const PopoverCell = (props: PopoverCellProps) => {
13
+ const [open, setOpen] = useState(false)
14
+ const anchorRef = useRef<HTMLDivElement>(null)
15
+ const handleClick = () => setOpen(!open)
16
+ const handleClose = () => setOpen(false)
17
+
18
+ return (
19
+ <div style={{ position: 'relative' }} ref={anchorRef}>
20
+ <TypographyCustom truncate onClick={stopPropagation(handleClick)}>
21
+ {String(props.value)}
22
+ </TypographyCustom>
23
+ <Popover
24
+ id={props.id}
25
+ open={open}
26
+ aria-controls="expand cell"
27
+ anchorEl={anchorRef.current}
28
+ onClose={handleClose}
29
+ placement={'bottom'}
30
+ >
31
+ {props.title && (
32
+ <Popover.Title>
33
+ <Popover.Header>{props.title}</Popover.Header>
34
+ </Popover.Title>
35
+ )}
36
+ <Popover.Content>
37
+ <Typography>{String(props.value)}</Typography>
38
+ </Popover.Content>
39
+ </Popover>
40
+ </div>
41
+ )
42
+ }
@@ -0,0 +1,62 @@
1
+ import {
2
+ Typography as EdsTypography,
3
+ TypographyProps as EdsTypographyProps,
4
+ } from '@equinor/eds-core-react'
5
+ import { tokens } from '@equinor/eds-tokens'
6
+ import { CSSProperties } from 'react'
7
+ import styled from 'styled-components'
8
+
9
+ export type TypographyProps = {
10
+ truncate?: boolean
11
+ } & EdsTypographyProps
12
+
13
+ const truncateStyle: CSSProperties = {
14
+ overflow: 'hidden',
15
+ whiteSpace: 'nowrap',
16
+ textOverflow: 'ellipsis',
17
+ }
18
+
19
+ export const TypographyCustom = (props: TypographyProps) => {
20
+ const { truncate, style: styleFromProps, ...edsTypographyProps } = props
21
+
22
+ if (truncate)
23
+ return (
24
+ <HoverCapture>
25
+ <EdsTypography
26
+ {...edsTypographyProps}
27
+ style={{
28
+ ...styleFromProps,
29
+ ...truncateStyle,
30
+ }}
31
+ />
32
+ </HoverCapture>
33
+ )
34
+
35
+ return <EdsTypography {...edsTypographyProps} style={styleFromProps} />
36
+ }
37
+
38
+ const HoverCapture = styled.div`
39
+ height: ${tokens.typography.table.cell_text.lineHeight};
40
+ background-color: inherit;
41
+
42
+ position: relative;
43
+ width: 100%;
44
+
45
+ &:hover {
46
+ z-index: 1;
47
+ }
48
+
49
+ & > * {
50
+ width: inherit;
51
+ position: absolute;
52
+ }
53
+
54
+ &:hover > * {
55
+ width: auto;
56
+ z-index: 1;
57
+ padding: 0.5em 1em;
58
+ margin: -0.5em -1em;
59
+
60
+ background-color: inherit;
61
+ }
62
+ `
@@ -1,6 +1,7 @@
1
1
  import { Icon } from '@equinor/eds-core-react'
2
2
  import { Variants } from '@equinor/eds-core-react/dist/types/components/types'
3
3
  import { error_filled, warning_filled } from '@equinor/eds-icons'
4
+ import { SyntheticEvent } from 'react'
4
5
 
5
6
  interface GetHelperTextPropsProps {
6
7
  error?: { message?: string }
@@ -38,3 +39,11 @@ export function getHelperTextProps({
38
39
  helperIcon: null,
39
40
  }
40
41
  }
42
+
43
+ /** Wrap an event handler and stop event propagation */
44
+ export function stopPropagation<T extends HTMLElement>(handler: (e: SyntheticEvent<T>) => void) {
45
+ return (e: SyntheticEvent<T>) => {
46
+ e.stopPropagation()
47
+ handler(e)
48
+ }
49
+ }