Skip to content

Commit

Permalink
Merge pull request #40 from oasisprotocol/csillag/more-form-improveme…
Browse files Browse the repository at this point in the history
…nts-2

More form improvements
  • Loading branch information
csillag authored Sep 24, 2024
2 parents 4953abc + c7c82bf commit ae8d5cc
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/InputFields/InputField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const InputField: FC<{ controls: InputFieldControls<any> }> = ({ controls
case 'oneOf':
return <SelectInput {...(controls as OneOfFieldControls<any>)} />
case 'label':
return <Label {...(controls as unknown as LabelControls)} />
return <Label {...(controls as unknown as LabelControls<any>)} />
case 'date':
return <DateInput {...(controls as DateFieldControls)} />
default:
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/InputFields/Label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { WithVisibility } from './WithVisibility'
import { WithLabelAndDescription } from './WithLabelAndDescription'
import { WithValidation } from './WithValidation'

export const Label: FC<LabelControls> = props => {
export const Label: FC<LabelControls<any>> = props => {
const { value, allProblems, formatter, renderer, classnames } = props

const formattedValue = formatter ? formatter(value) : value
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/InputFields/useInputField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export function useInputField<DataType>(
const [validatorProgress, setValidatorProgress] = useState<number>()
const [validationStatusMessage, setValidationStatusMessage] = useState<string | undefined>()

const validatorControls: ValidatorControls = {
const validatorControls: Pick<ValidatorControls, 'updateStatus'> = {
updateStatus: ({ progress, message }) => {
if (progress) setValidatorProgress(progress)
if (message) setValidationStatusMessage(message)
Expand Down Expand Up @@ -314,7 +314,7 @@ export function useInputField<DataType>(
: await validator(
cleanValue,
forceChange || !wasOK || lastValidatedData !== cleanValue,
validatorControls,
{ ...validatorControls, isStillFresh },
params.reason,
) // Execute the current validators

Expand Down
11 changes: 7 additions & 4 deletions frontend/src/components/InputFields/useLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export type FormatterFunction<DataType> = (rawValue: DataType) => string

export type RendererFunction<DataType> = (rawValue: DataType) => ReactNode

export type LabelProps = Pick<
InputFieldProps<string>,
export type LabelProps<DataType = string> = Pick<
InputFieldProps<DataType>,
| 'name'
| 'label'
| 'description'
Expand All @@ -24,13 +24,16 @@ export type LabelProps = Pick<
renderer?: RendererFunction<string>
}

export type LabelControls = Omit<InputFieldControls<string>, 'placeholder' | 'enabled' | 'whyDisabled'> & {
export type LabelControls<DataType> = Omit<
InputFieldControls<DataType>,
'placeholder' | 'enabled' | 'whyDisabled'
> & {
classnames: string[]
formatter: FormatterFunction<string> | undefined
renderer: RendererFunction<string> | undefined
}

export const useLabel = (props: LabelProps): LabelControls => {
export function useLabel<DataType = string>(props: LabelProps<DataType>): LabelControls<DataType> {
const { classnames = [], formatter, renderer } = props

const controls = useInputField(
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/InputFields/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function flatten<Data>(array: Data[][]): Data[] {
export type AllProblems = Record<string, Problem[]>

export type ValidatorControls = {
isStillFresh: () => boolean
updateStatus: (status: { message?: string; progress?: number }) => void
}

Expand Down

0 comments on commit ae8d5cc

Please sign in to comment.