Skip to content

Commit

Permalink
fix: some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Nov 6, 2024
1 parent 591c04d commit dc46f01
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 178 deletions.
49 changes: 0 additions & 49 deletions src/lib/form/formDescriptor.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export { validate, createFormValidate } from './validate'
export { useOnSubmitEdit, useOnSubmitNew } from './useOnSubmit'
export { modelFormSchemas } from './modelFormSchemas'
export * from './sectionedForm'
export * from './formDescriptor'
export * from './sectionedForm/sectionedFormDescriptor'
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { createContext, useState } from 'react'
import { SectionDescriptor, SectionedFormDescriptor } from '../formDescriptor'
import { DataSetFormDescriptor } from '../../../pages/dataSets/form/formDescriptor'
import {
SectionDescriptor,
SectionedFormDescriptor,
} from './sectionedFormDescriptor'

/* Some of the types in this file may look complex.
However they are here to help type-safety and autocommpletion for consumers.
Expand Down Expand Up @@ -49,7 +51,6 @@ function createContextValue<T extends SectionedFormDescriptor>(descriptor: T) {
},
}
}
const con = createContextValue(DataSetFormDescriptor)
type SectionFormContextValue<T extends SectionedFormDescriptor> = ReturnType<
typeof createContextValue<T>
>
Expand Down
129 changes: 4 additions & 125 deletions src/lib/form/types.ts
Original file line number Diff line number Diff line change
@@ -1,138 +1,17 @@
import { Field } from '@dhis2/ui'
import { DataSet } from '../../types/generated'

// type AllowAnyString
type FieldDescriptor = {
label: string
}

type SectionDescriptor<T> = {
type SectionDescriptor<TModel = unknown> = {
label: string
// keyof T | (string & {}) allows auto-completion for fields, while also allowing
// any other string to be used as a key
fields: Partial<Record<keyof T | (string & {}), FieldDescriptor>>
fields: Partial<Record<keyof TModel | (string & {}), FieldDescriptor>>
// fields: Partial<Record<keyof T | string, FieldDescriptor>>
}

type FormDescriptor<T = unknown> = {
type FormDescriptor<TModel = unknown> = {
name: string
sections: Record<string, SectionDescriptor<T>>
}

const DataSetDescriptor = {
name: 'DataSet',
sections: {
basic: {
label: 'Basic information',
fields: {
name: {
label: 'Name',
},
code: {
label: 'Code',
},
someOtherField: {
label: 'hello',
},
access: {
label: 'Access',
},
style: {
label: 'Style',
},
someOtherField: {
label: 'Some other field',
},
},
},
},
} as const satisfies FormDescriptor

type FieldDescriptorAlt<T> = {
label: string
// keyof T | (string & {}) allows auto-completion for keys of T, while also allowing
// any other string to be used as a key. This allows fields that not necessarily map to the model-property
name: keyof T | (string & {})
}

type SectionDescriptorAlt<T> = {
label: string
name: string
// keyof T | (string & {}) allows auto-completion for fields, while also allowing
// any other string to be used as a key
fields: FieldDescriptorAlt<T>[]
sections: Record<string, SectionDescriptor<TModel>>
}

type DescriptorAlt<T = unknown> = {
name: string
label: string
sections: SectionDescriptorAlt<T>[]
}

const DataSetDescriptorAlt = {
name: 'DataSet',
label: 'Data Set',
sections: [
{
name: 'basic',
label: 'Basic information',
fields: [
{
name: 'name',
label: 'Name',
},
{
name: 'code',
label: 'Code',
},
{
name: 'sharing',
label: 'Access',
},
{
name: 'style',
label: 'Style',
},
{
name: 'someOtherField',
label: 'Some other field',
},
],
},
],
} satisfies DescriptorAlt<DataSet>

type ExtractFieldNames<T> = T extends {
sections: { fields: { name: infer N }[] }[]
}
? N
: never

const getLabelForField = <
T extends DescriptorAlt,
F extends ExtractFieldNames<T>
>(
field: F,
descriptor: T,
section?: string
) => {
if (section) {
const sectionDescriptor = descriptor.sections.find(
(s) => s.name === section
)
if (sectionDescriptor) {
const fieldDescriptor = sectionDescriptor.fields.find(
(f) => f.name === field
)
if (fieldDescriptor) {
return fieldDescriptor.label
}
}
}
return descriptor.sections
.flatMap((s) => s.fields)
.find((f) => f.name === field)?.label
}
const label = getLabelForField('style', DataSetDescriptorAlt)

const nameLabel = DataSetDescriptor.sections.basic.fields.name.label

0 comments on commit dc46f01

Please sign in to comment.