@chem-po/react-web 0.0.28 → 0.0.30

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": "@chem-po/react-web",
3
- "version": "0.0.28",
3
+ "version": "0.0.30",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -16,8 +16,8 @@
16
16
  "author": "",
17
17
  "license": "ISC",
18
18
  "dependencies": {
19
- "@chem-po/core": "0.0.28",
20
- "@chem-po/react": "0.0.28",
19
+ "@chem-po/core": "0.0.30",
20
+ "@chem-po/react": "0.0.30",
21
21
  "@chakra-ui/anatomy": "^2.3.4",
22
22
  "@chakra-ui/icons": "^2.2.4",
23
23
  "@chakra-ui/react": "^2.10.7",
@@ -1,8 +1,15 @@
1
1
  import { CheckIcon, CloseIcon, EditIcon } from '@chakra-ui/icons'
2
2
  import { Flex, IconButton } from '@chakra-ui/react'
3
3
  import { palette } from '@chem-po/core'
4
- import { EditableProps, Field, useColorModeValue, useEditable } from '@chem-po/react'
5
- import React, { CSSProperties, useEffect, useMemo } from 'react'
4
+ import {
5
+ CustomEditableViewProps,
6
+ EditableProps,
7
+ Field,
8
+ useColorModeValue,
9
+ useEditable,
10
+ } from '@chem-po/react'
11
+ import React, { CSSProperties, useCallback, useEffect, useMemo } from 'react'
12
+ import { CustomInputProps } from '../../../types/forms'
6
13
  import { LoadingOverlay } from '../../loading/Loading'
7
14
  import { UploadProgress } from '../UploadProgress'
8
15
  import { FieldView } from '../view'
@@ -16,7 +23,11 @@ export const Editable = <T extends Field>({
16
23
  style,
17
24
  onEditClose,
18
25
  onEditOpen,
19
- }: EditableProps<CSSProperties, T>) => {
26
+ CustomInput,
27
+ CustomView,
28
+ containerStyle,
29
+ size,
30
+ }: EditableProps<CSSProperties, CSSProperties, T>) => {
20
31
  const {
21
32
  inputRef,
22
33
  setValue,
@@ -38,7 +49,10 @@ export const Editable = <T extends Field>({
38
49
  onEditOpen,
39
50
  onEditClose,
40
51
  })
41
- const alwaysEditing = useMemo(() => field._type === 'file' || field._type === 'boolean', [field])
52
+ const alwaysEditing = useMemo(
53
+ () => field && (field._type === 'file' || field._type === 'boolean'),
54
+ [field],
55
+ )
42
56
  useEffect(() => {
43
57
  if (isEditing) {
44
58
  inputRef.current?.focus()
@@ -47,12 +61,35 @@ export const Editable = <T extends Field>({
47
61
 
48
62
  const editingBorderColor = useColorModeValue('#00000055', '#ffffff55')
49
63
 
64
+ const RenderInput = useCallback(
65
+ (props: CustomInputProps<T['defaultValue']>) => {
66
+ if (CustomInput) {
67
+ return <CustomInput {...props} />
68
+ }
69
+ if (!field) throw new Error('Editable component must have a field OR a CustomInput')
70
+ return <StandaloneInput field={field} {...props} />
71
+ },
72
+ [CustomInput, field],
73
+ )
74
+
75
+ const RenderView = useCallback(
76
+ (props: CustomEditableViewProps<T['defaultValue']>) => {
77
+ if (CustomView) {
78
+ return <CustomView {...props} />
79
+ }
80
+ if (!field) throw new Error('Editable component must have a field OR a CustomView')
81
+ return <FieldView style={style} field={field} value={value} {...props} />
82
+ },
83
+ [CustomView, field, style, value],
84
+ )
85
+
50
86
  return (
51
87
  <Flex
52
88
  borderRadius={4}
53
89
  border={`1px dashed ${editHovered && !isEditing ? editingBorderColor : 'transparent'}`}
54
90
  flexFlow="column"
55
- w="100%">
91
+ w="100%"
92
+ style={containerStyle}>
56
93
  <Flex align="center" px={1} position="relative" w="100%">
57
94
  <Flex
58
95
  opacity={isLoading ? 0 : 1}
@@ -62,16 +99,17 @@ export const Editable = <T extends Field>({
62
99
  flex={1}
63
100
  minW="0">
64
101
  {isEditing || alwaysEditing ? (
65
- <StandaloneInput
102
+ <RenderInput
66
103
  ref={inputRef}
67
104
  value={value}
68
105
  inEditable
69
- field={field}
70
106
  style={{ padding: 0, ...style }}
107
+ size={size}
71
108
  onChange={alwaysEditing ? v => submitValue(v) : setValue}
109
+ inputStyle={style}
72
110
  />
73
111
  ) : (
74
- <FieldView style={style} field={field} value={value} />
112
+ <RenderView inEditable size={size} value={value} />
75
113
  )}
76
114
  </Flex>
77
115
  {alwaysEditing ? null : (
@@ -1,4 +1,4 @@
1
1
  import { BaseFieldProps, Field } from '@chem-po/react'
2
2
  import { CSSProperties } from 'react'
3
3
 
4
- export type FieldProps<T extends Field> = BaseFieldProps<CSSProperties, T>
4
+ export type FieldProps<T extends Field> = BaseFieldProps<CSSProperties, CSSProperties, T>
package/src/index.ts CHANGED
@@ -2,3 +2,4 @@ export * from './components'
2
2
  export * from './contexts'
3
3
  export * from './hooks'
4
4
  export * from './store'
5
+ export * from './types'
@@ -0,0 +1,5 @@
1
+ import { BaseCustomInput, BaseCustomInputProps } from '@chem-po/react'
2
+ import { CSSProperties } from 'react'
3
+
4
+ export type CustomInput<Value> = BaseCustomInput<Value, CSSProperties, CSSProperties>
5
+ export type CustomInputProps<Value> = BaseCustomInputProps<Value, CSSProperties, CSSProperties>
@@ -0,0 +1 @@
1
+ export * from './forms'