@contember/create 2.1.0-alpha.29 → 2.1.0-alpha.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,10 +1,10 @@
1
1
  {
2
2
  "name": "@contember/create",
3
- "version": "2.1.0-alpha.29",
3
+ "version": "2.1.0-alpha.30",
4
4
  "license": "Apache-2.0",
5
5
  "bin": "dist/production/run.js",
6
6
  "dependencies": {
7
- "@contember/cli-common": "2.1.0-alpha.29",
7
+ "@contember/cli-common": "2.1.0-alpha.30",
8
8
  "chalk": "^5.3.0",
9
9
  "degit": "^2.8.4",
10
10
  "fs-extra": "^11.2.0",
@@ -1,20 +1,22 @@
1
1
  import { PersistTrigger } from '@contember/interface'
2
- import { ReactNode } from 'react'
2
+ import { ComponentProps, ReactNode } from 'react'
3
3
  import { dict } from '../dict'
4
4
  import { Button } from '../ui/button'
5
5
  import { Loader } from '../ui/loader'
6
6
  import { usePersistSuccessHandler } from './hooks'
7
+ import { cn } from '../utils'
7
8
 
8
- export const PersistButton = ({ label }: {
9
+ export interface PersistButtonProps extends ComponentProps<typeof Button> {
9
10
  label?: ReactNode
10
- }) => {
11
+ }
12
+
13
+ export const PersistButton = ({ label, className, ...buttonProps }: PersistButtonProps) => {
11
14
  return (
12
15
  <PersistTrigger onPersistSuccess={usePersistSuccessHandler()}>
13
- <Button className="group">
16
+ <Button className={cn('group', className)} {...buttonProps}>
14
17
  <Loader size="sm" position="absolute" className="hidden group-data-[loading]:block" />
15
18
  {label ?? dict.persist.persistButton}
16
19
  </Button>
17
20
  </PersistTrigger>
18
21
  )
19
22
  }
20
-
@@ -6,6 +6,7 @@ import {
6
6
  EntityAccessor,
7
7
  Field,
8
8
  HasOne,
9
+ LabelMiddlewareProvider,
9
10
  StaticRender,
10
11
  SugaredQualifiedEntityList,
11
12
  SugaredRelativeSingleEntity,
@@ -14,14 +15,14 @@ import {
14
15
  useEntity,
15
16
  } from '@contember/interface'
16
17
  import { DataView, DataViewEachRow, DataViewLoaderState, DataViewSortingDirections, useDataViewEntityListAccessor } from '@contember/react-dataview'
17
- import * as React from 'react'
18
18
  import { ReactNode, useMemo } from 'react'
19
19
  import { CheckIcon } from 'lucide-react'
20
20
  import { Loader } from '../ui/loader'
21
21
  import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover'
22
22
  import { Button } from '../ui/button'
23
+ import { DimensionLabelUI, DimensionLabelWrapperUI } from '../ui/dimension'
23
24
 
24
- export interface DimensionsSwitcherProps {
25
+ export type DimensionsSwitcherProps = {
25
26
  options: SugaredQualifiedEntityList['entities']
26
27
  orderBy?: DataViewSortingDirections
27
28
  dimension: string
@@ -30,56 +31,51 @@ export interface DimensionsSwitcherProps {
30
31
  isMulti?: boolean
31
32
  }
32
33
 
33
- export const DimensionsSwitcher = Component(({ options, dimension, children, slugField, orderBy, isMulti }: DimensionsSwitcherProps) => {
34
- return (
35
- <DataView entities={options} initialSorting={orderBy}>
36
- <DataViewLoaderState initial refreshing>
37
- <Loader position={'static'} />
38
- </DataViewLoaderState>
39
- <DataViewLoaderState loaded>
40
-
41
- <Popover>
42
- <PopoverTrigger asChild>
43
- <Button variant={'outline'} size="sm">
44
- <DimensionSwitcherCurrentValues dimension={dimension} slugField={slugField}>
34
+ export const DimensionsSwitcher = Component(({ options, dimension, children, slugField, orderBy, isMulti }: DimensionsSwitcherProps) => (
35
+ <DataView entities={options} initialSorting={orderBy}>
36
+ <DataViewLoaderState initial refreshing>
37
+ <Loader position="static" />
38
+ </DataViewLoaderState>
39
+ <DataViewLoaderState loaded>
40
+ <Popover>
41
+ <PopoverTrigger asChild>
42
+ <Button variant="outline" size="sm">
43
+ <DimensionSwitcherCurrentValues dimension={dimension} slugField={slugField}>
44
+ {children}
45
+ </DimensionSwitcherCurrentValues>
46
+ </Button>
47
+ </PopoverTrigger>
48
+ <PopoverContent className="p-2" align="start">
49
+ <div className="flex flex-col gap-1">
50
+ <DataViewEachRow>
51
+ <DimensionSwitcherItem dimension={dimension} slugField={slugField} isMulti={isMulti}>
45
52
  {children}
46
- </DimensionSwitcherCurrentValues>
47
- </Button>
48
- </PopoverTrigger>
49
- <PopoverContent className="p-2" align="start">
50
- <div className="flex flex-col gap-1">
51
- <DataViewEachRow>
52
- <DimensionSwitcherItem dimension={dimension} slugField={slugField} isMulti={isMulti}>
53
- {children}
54
- <StaticRender>
55
- <Field field={slugField} />
56
- </StaticRender>
57
- </DimensionSwitcherItem>
58
- </DataViewEachRow>
59
- </div>
60
- </PopoverContent>
61
- </Popover>
62
- </DataViewLoaderState>
63
- </DataView>
64
- )
65
- })
53
+ <StaticRender>
54
+ <Field field={slugField} />
55
+ </StaticRender>
56
+ </DimensionSwitcherItem>
57
+ </DataViewEachRow>
58
+ </div>
59
+ </PopoverContent>
60
+ </Popover>
61
+ </DataViewLoaderState>
62
+ </DataView>
63
+ ))
66
64
 
67
65
  const DimensionSwitcherCurrentValues = ({ children, dimension, slugField }: { children: ReactNode; dimension: string; slugField: SugaredRelativeSingleField['field'] }) => {
68
66
  const entitiesBySlug = useDimensionEntitiesBySlug(slugField)
69
-
70
67
  const currentDimensionValue = useDimensionState({
71
68
  dimension,
72
69
  defaultValue: Object.keys(entitiesBySlug)[0],
73
70
  storage: 'local',
74
71
  })
75
-
76
72
  const values = useMemo(() => currentDimensionValue.map(it => entitiesBySlug[it]).filter(Boolean), [currentDimensionValue, entitiesBySlug])
77
73
 
78
74
  return (
79
75
  <div className="flex gap-1">
80
76
  {values.map(it => (
81
77
  <Entity key={it.key} accessor={it}>
82
- <div className={'gap-1 group text-black text-left inline-flex items-center px-1 text-sm border-b border-gray-200'}>
78
+ <div className="gap-1 group text-black text-left inline-flex items-center px-1 text-sm border-b border-gray-200">
83
79
  <span>{children}</span>
84
80
  </div>
85
81
  </Entity>
@@ -88,48 +84,64 @@ const DimensionSwitcherCurrentValues = ({ children, dimension, slugField }: { ch
88
84
  )
89
85
  }
90
86
 
91
-
92
87
  const DimensionSwitcherItem = ({ children, dimension, slugField, isMulti }: { children: ReactNode; dimension: string; slugField: SugaredRelativeSingleField['field']; isMulti?: boolean }) => {
93
88
  const entity = useEntity()
94
89
  const slugValue = entity.getField<string>(slugField).value
95
- if (!slugValue) {
96
- return null
97
- }
90
+
91
+ if (!slugValue) return null
98
92
 
99
93
  return (
100
94
  <DimensionLink dimension={dimension} value={slugValue} action={isMulti ? 'toggle' : 'set'}>
101
- <a className={'gap-1 group text-gray-800 text-left inline-flex items-center px-1 py-1 text-sm rounded-sm transition-all hover:bg-accent hover:text-accent-foreground group data-[active]:text-black'}>
102
- <CheckIcon className={'w-3 h-3 hidden group-data-[active]:block'} />
103
- <span className={'w-3 h-3 group-data-[active]:hidden'} />
95
+ <a className="gap-1 group text-gray-800 text-left inline-flex items-center px-1 py-1 text-sm rounded transition-all hover:bg-accent hover:text-accent-foreground group data-[active]:text-black">
96
+ <CheckIcon className="w-3 h-3 hidden group-data-[active]:block" />
97
+ <span className="w-3 h-3 group-data-[active]:hidden" />
104
98
  <span>{children}</span>
105
99
  </a>
106
100
  </DimensionLink>
107
101
  )
108
102
  }
109
103
 
110
- export interface SideDimensionsProps {
104
+ export const DimensionLabel = ({ label, dimensionValue }: { label: ReactNode; dimensionValue: ReactNode }) => (
105
+ <DimensionLabelWrapperUI>
106
+ {label}<DimensionLabelUI>{dimensionValue}</DimensionLabelUI>
107
+ </DimensionLabelWrapperUI>
108
+ )
109
+
110
+ export type RenderLabelProps = {
111
+ label: ReactNode
112
+ dimensionValue: string | null
113
+ }
114
+
115
+ export type SideDimensionsProps = {
111
116
  dimension: string
112
117
  as: string
113
118
  field: SugaredRelativeSingleEntity['field']
114
119
  children: ReactNode
120
+ renderLabel?: ({ label, dimensionValue }: RenderLabelProps) => ReactNode
115
121
  }
116
122
 
117
- export const SideDimensions = Component<SideDimensionsProps>(({ dimension, children, as, field }) => {
118
- return (
119
- <div className="flex mt-4 gap-4">
120
- <DimensionRenderer dimension={dimension} as={as}>
123
+ export const SideDimensions = Component<SideDimensionsProps>(({ dimension, children, as, field, renderLabel }) => (
124
+ <div className="flex mt-4 gap-4">
125
+ <DimensionRenderer dimension={dimension} as={as}>
126
+ <LabelMiddlewareProvider value={(label, environment) => {
127
+ const dimensionValue = environment.getVariableOrElse(as, null) as string | null
128
+ const Component = renderLabel || DimensionLabel
129
+
130
+ return <Component label={label} dimensionValue={dimensionValue} />
131
+ }}>
121
132
  <HasOne field={field}>
122
- <div className="flex-1">
123
- {children}
124
- </div>
133
+ <div className="flex-1">{children}</div>
125
134
  </HasOne>
126
- </DimensionRenderer>
127
- </div>
128
- )
129
- })
130
-
135
+ </LabelMiddlewareProvider>
136
+ </DimensionRenderer>
137
+ </div>
138
+ ))
131
139
 
132
140
  const useDimensionEntitiesBySlug = (slugField: SugaredRelativeSingleField['field']): Record<string, EntityAccessor> => {
133
141
  const accessor = useDataViewEntityListAccessor()
134
- return useMemo(() => Object.fromEntries(Array.from(accessor ?? []).map(it => [it.getField<string>(slugField).value, it])), [accessor, slugField])
142
+
143
+ return useMemo(
144
+ () => Object.fromEntries(Array.from(accessor ?? []).map(it => [it.getField<string>(slugField).value, it])),
145
+ [accessor, slugField],
146
+ )
135
147
  }
@@ -1,12 +1,11 @@
1
- import * as React from 'react'
2
1
  import { ReactNode } from 'react'
3
2
  import { FormContainerUI, FormDescriptionUI, FormErrorUI, FormLabelUI, FormLabelWrapperUI } from './ui'
4
3
  import { useErrorFormatter } from '../errors'
5
- import { Component, ErrorAccessor } from '@contember/interface'
4
+ import { Component, ErrorAccessor, useLabelMiddleware } from '@contember/interface'
6
5
  import { FormError, FormFieldStateProvider, FormLabel, useFormFieldState } from '@contember/react-form'
7
6
  import { FormFieldLabel } from './labels'
8
7
 
9
- export interface FormContainerProps {
8
+ export type FormContainerProps = {
10
9
  label?: ReactNode
11
10
  description?: ReactNode
12
11
  children: ReactNode
@@ -14,44 +13,52 @@ export interface FormContainerProps {
14
13
  required?: boolean
15
14
  }
16
15
 
16
+ export const FormContainer = Component(
17
+ ({ children, description, label, required, errors }: FormContainerProps) => {
18
+ const errorsNode = Array.isArray(errors) ? undefined : errors
19
+ const errorsList = Array.isArray(errors) ? errors : []
20
+ const state = useFormFieldState()
21
+ const labelMiddleware = useLabelMiddleware()
22
+ label ??= <FormFieldLabel />
23
+ const errorFormatter = useErrorFormatter()
17
24
 
18
- export const FormContainer = Component(({ children, description, label, required, errors }: FormContainerProps) => {
19
- const errorsNode = Array.isArray(errors) ? undefined : errors
20
- const errorsList = Array.isArray(errors) ? errors : []
21
- const state = useFormFieldState()
22
- label ??= <FormFieldLabel />
23
- const errorFormatter = useErrorFormatter()
25
+ const inner = (
26
+ <FormContainerUI>
27
+ <FormLabelWrapperUI>
28
+ {label && labelMiddleware(<>
29
+ <FormLabel>
30
+ <FormLabelUI required={required}>
31
+ {label}
32
+ </FormLabelUI>
33
+ </FormLabel>
34
+ </>)}
35
+ </FormLabelWrapperUI>
36
+ <div>
37
+ {children}
38
+ </div>
39
+ {(description || errorsNode || state?.errors?.length || errorsList?.length) ? <div>
40
+ {description && <FormDescriptionUI>{description}</FormDescriptionUI>}
24
41
 
25
- const inner = <>
26
- <FormContainerUI>
27
- <FormLabelWrapperUI>
28
- {label && <FormLabel>
29
- <FormLabelUI required={required}>
30
- {label}
31
- </FormLabelUI>
32
- </FormLabel>}
33
- </FormLabelWrapperUI>
34
- <div>
35
- {children}
36
- </div>
37
- {(description || errorsNode || state?.errors?.length || errorsList?.length) ? <div>
38
- {description && <FormDescriptionUI>{description}</FormDescriptionUI>}
42
+ <FormError formatter={errorFormatter}>
43
+ <FormErrorUI />
44
+ </FormError>
45
+ {errorsNode}
46
+ </div> : null}
47
+ </FormContainerUI>
48
+ )
39
49
 
40
- <FormError formatter={errorFormatter}>
41
- <FormErrorUI />
42
- </FormError>
43
- {errorsNode}
44
- </div> : null}
45
- </FormContainerUI>
46
- </>
47
- return state !== undefined
48
- ? inner
49
- : <FormFieldStateProvider required={required} errors={errorsList} dirty={false}>{inner}</FormFieldStateProvider>
50
- }, ({ children, label, description }) => <>
51
- {label}
52
- {children}
53
- {description}
54
- </>)
50
+ return state !== undefined
51
+ ? inner
52
+ : <FormFieldStateProvider required={required} errors={errorsList} dirty={false}>{inner}</FormFieldStateProvider>
53
+ },
54
+ ({ children, label, description }) => (
55
+ <>
56
+ {label}
57
+ {children}
58
+ {description}
59
+ </>
60
+ ),
61
+ )
55
62
 
56
63
  /**
57
64
  * @deprecated use `FormContainer` instead
@@ -1,7 +1,5 @@
1
- import * as React from 'react'
2
- import { ComponentProps, ReactNode } from 'react'
1
+ import { ComponentProps, ReactNode, useMemo } from 'react'
3
2
  import { CheckboxInput, Input, RadioInput } from '../ui/input'
4
- import { cn } from '../utils'
5
3
  import { TextareaAutosize } from '../ui/textarea'
6
4
  import { FormLabelUI } from './ui'
7
5
  import {
@@ -33,7 +31,7 @@ export const InputField = Component(({ field, label, description, inputProps, is
33
31
  <FormFieldScope field={field}>
34
32
  <FormContainer description={description} label={label} required={required}>
35
33
  <FormInput field={field} isNonbearing={isNonbearing} defaultValue={defaultValue} parseValue={parseValue} formatValue={formatValue}>
36
- <Input required={required} {...(inputProps ?? {})} className={cn('max-w-md', inputProps?.className)} />
34
+ <Input required={required} {...(inputProps ?? {})} />
37
35
  </FormInput>
38
36
  </FormContainer>
39
37
  </FormFieldScope>
@@ -51,7 +49,7 @@ export const TextareaField = Component(({ field, label, description, inputProps,
51
49
  <FormFieldScope field={field}>
52
50
  <FormContainer description={description} label={label} required={required}>
53
51
  <FormInput field={field} isNonbearing={isNonbearing} defaultValue={defaultValue}>
54
- <TextareaAutosize required={required} {...(inputProps ?? {})} className={cn('max-w-md', inputProps?.className)} />
52
+ <TextareaAutosize required={required} {...(inputProps ?? {})} />
55
53
  </FormInput>
56
54
  </FormContainer>
57
55
  </FormFieldScope>
@@ -112,7 +110,7 @@ const RadioEnumFieldInner: React.FC<RadioEnumFieldInnerProps> = ({ field, inputP
112
110
  throw new Error('RadioEnumField: options are required')
113
111
  }
114
112
 
115
- const normalizedOptions = React.useMemo(() => {
113
+ const normalizedOptions = useMemo(() => {
116
114
  return Array.isArray(options) ? options : Object.entries(options).map(([value, label]) => ({ value, label }))
117
115
  }, [options])
118
116
 
@@ -15,7 +15,7 @@ import {
15
15
  import * as React from 'react'
16
16
  import { useCallback, useMemo } from 'react'
17
17
  import { FormContainer, FormContainerProps } from './container'
18
- import { FormFieldScope, FormHasManyRelationScope, FormHasOneRelationScope, useFormFieldId, useFormFieldState } from '@contember/react-form'
18
+ import { FormFieldScope, FormHasManyRelationScope, FormHasOneRelationScope, useFormFieldState } from '@contember/react-form'
19
19
  import { Component, Field, RecursionTerminator, SugaredRelativeSingleField, useEntityBeforePersist, useField } from '@contember/interface'
20
20
  import { Popover, PopoverTrigger } from '../ui/popover'
21
21
  import { ChevronDownIcon, ChevronUpIcon } from 'lucide-react'
@@ -98,33 +98,43 @@ export const SelectEnumField = Component<SelectEnumFieldProps>(
98
98
  'SelectEnumField',
99
99
  )
100
100
 
101
+ const useNormalizedOptions = (options: SelectEnumFieldProps['options']) => {
102
+ return useMemo(() => {
103
+ if (!options) return []
104
+ return Array.isArray(options)
105
+ ? options
106
+ : Object.entries(options).map(([value, label]) => ({ value, label }))
107
+ }, [options])
108
+ }
109
+
101
110
  const SelectEnumFieldInner = ({ field, options, placeholder, required }: SelectEnumFieldProps) => {
102
111
  const [open, setOpen] = React.useState(false)
103
112
  const fieldAccessor = useField(field)
104
113
  const fieldAccessorGetter = fieldAccessor.getAccessor
114
+
105
115
  useEntityBeforePersist(useCallback(() => {
106
116
  if (!required) {
107
117
  return
108
118
  }
119
+
109
120
  const field = fieldAccessorGetter()
110
121
  if (!field.value) {
111
122
  field.addError(dict.errors.required)
112
123
  }
113
124
  }, [fieldAccessorGetter, required]))
114
- const id = useFormFieldId()
125
+
126
+ const fieldState = useFormFieldState()
115
127
  const enumLabelsFormatter = useEnumOptionsFormatter()
116
- const enumName = useFormFieldState()?.field?.enumName
128
+ const id = fieldState?.htmlId
129
+ const enumName = fieldState?.field?.enumName
130
+
117
131
  options ??= enumName ? enumLabelsFormatter(enumName) : undefined
118
132
  if (!options) {
119
133
  throw new Error('SelectEnumFields: options are required')
120
134
  }
121
- const normalizedOptions = useMemo(() => {
122
- return Array.isArray(options) ? options : Object.entries(options).map(([value, label]) => ({ value, label }))
123
- }, [options])
124
- const selectedValue = useMemo(() => {
125
- return normalizedOptions.find(it => it.value === fieldAccessor.value)
126
- }, [fieldAccessor.value, normalizedOptions])
127
135
 
136
+ const normalizedOptions = useNormalizedOptions(options)
137
+ const selectedValue = useMemo(() => normalizedOptions.find(it => it.value === fieldAccessor.value), [fieldAccessor.value, normalizedOptions])
128
138
 
129
139
  return (
130
140
  <Popover open={open} onOpenChange={setOpen}>
@@ -2,7 +2,7 @@ import { uic } from '../utils/uic'
2
2
  import { Label } from '../ui/label'
3
3
 
4
4
  export const FormLayout = uic('div', {
5
- baseClass: 'flex flex-col gap-2 mx-4',
5
+ baseClass: 'flex flex-col gap-2 mx-4 max-w-lg',
6
6
  displayName: 'FormLayout',
7
7
  })
8
8
 
@@ -0,0 +1,9 @@
1
+ import { uic } from '../utils'
2
+
3
+ export const DimensionLabelWrapperUI = uic('div', {
4
+ baseClass: 'flex justify-between items-center',
5
+ })
6
+
7
+ export const DimensionLabelUI = uic('span', {
8
+ baseClass: 'ml-1 bg-gray-100 border border-gray-300 px-1 rounded text-xs pointer-events-none',
9
+ })
@@ -1,16 +1,17 @@
1
1
  import { uic, uiconfig } from '../utils'
2
2
 
3
3
  export const inputConfig = uiconfig({
4
- baseClass: `
5
- flex w-full bg-background-input
6
- file:border-0 file:bg-transparent file:text-sm file:font-medium
7
- placeholder:text-muted-foreground
8
- focus-visible:outline-hidden
9
- disabled:cursor-not-allowed disabled:opacity-50
10
- read-only:bg-gray-100
11
- ` /*data-[dirty]:border-yellow-500 focus-visible:data-[dirty]:border-input*/ + `
12
- data-[invalid]:border-destructive data-[invalid]:ring-destructive
13
- `,
4
+ baseClass: [
5
+ 'flex w-full bg-background',
6
+ 'file:border-0 file:bg-transparent file:text-sm file:font-medium',
7
+ 'placeholder:text-muted-foreground',
8
+ 'focus-visible:outline-none',
9
+ 'disabled:cursor-not-allowed disabled:opacity-50',
10
+ 'read-only:bg-gray-100',
11
+ // Uncomment this if you want to see inputs in dirty state
12
+ // 'data-[dirty]:border-yellow-500 focus-visible:data-[dirty]:border-input',
13
+ 'data-[invalid]:border-destructive data-[invalid]:ring-destructive',
14
+ ],
14
15
  variants: {
15
16
  inputSize: {
16
17
  default: 'h-10 rounded-md p-2 text-sm',
@@ -34,14 +35,13 @@ export const Input = uic('input', {
34
35
  })
35
36
 
36
37
  export const InputLike = uic('div', {
37
- baseClass: `
38
- flex items-center min-h-10 w-full rounded-md border border-input bg-background p-2 text-sm ring-offset-background max-w-md
39
- file:border-0 file:bg-transparent file:text-sm file:font-medium
40
- placeholder:text-muted-foreground
41
- focus-within:outline-hidden focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2
42
- disabled:cursor-not-allowed
43
- disabled:opacity-50
44
- `,
38
+ baseClass: [
39
+ 'flex items-center min-h-10 w-full rounded-md border border-input bg-background p-2 text-sm ring-offset-background',
40
+ 'file:border-0 file:bg-transparent file:text-sm file:font-medium',
41
+ 'placeholder:text-muted-foreground',
42
+ 'focus-within:outline-none focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2',
43
+ 'disabled:cursor-not-allowed disabled:opacity-50',
44
+ ],
45
45
  displayName: 'InputLike',
46
46
  })
47
47
 
@@ -53,7 +53,6 @@ export const InputBare = uic('input', {
53
53
  displayName: 'InputBare',
54
54
  })
55
55
 
56
-
57
56
  export const CheckboxInput = uic('input', {
58
57
  baseClass: 'w-4 h-4',
59
58
  defaultProps: {