mirror of
https://github.com/status-im/spiff-arena.git
synced 2025-02-28 17:30:35 +00:00
fixed some of the form widgets w/ burnettk cullerton
This commit is contained in:
parent
67d6a01161
commit
6d782278e5
@ -90,7 +90,7 @@ export default function BaseInputTemplate<
|
|||||||
let errorMessageForField = null;
|
let errorMessageForField = null;
|
||||||
if (rawErrors && rawErrors.length > 0) {
|
if (rawErrors && rawErrors.length > 0) {
|
||||||
invalid = true;
|
invalid = true;
|
||||||
errorMessageForField = `${labelToUse} ${rawErrors[0]}`;
|
errorMessageForField = `${labelToUse.replace(/\*$/, '')} ${rawErrors[0]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -17,6 +17,7 @@ function SelectWidget({
|
|||||||
onBlur,
|
onBlur,
|
||||||
onFocus,
|
onFocus,
|
||||||
uiSchema,
|
uiSchema,
|
||||||
|
placeholder,
|
||||||
rawErrors = [],
|
rawErrors = [],
|
||||||
}: WidgetProps) {
|
}: WidgetProps) {
|
||||||
const { enumOptions, enumDisabled } = options;
|
const { enumOptions, enumDisabled } = options;
|
||||||
@ -44,12 +45,20 @@ function SelectWidget({
|
|||||||
labelToUse = `${labelToUse}*`;
|
labelToUse = `${labelToUse}*`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let invalid = false;
|
||||||
|
let errorMessageForField = null;
|
||||||
|
if (rawErrors && rawErrors.length > 0) {
|
||||||
|
invalid = true;
|
||||||
|
errorMessageForField = `${labelToUse.replace(/\*$/, '')} ${rawErrors[0]}`;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Select
|
<Select
|
||||||
id={id}
|
id={id}
|
||||||
name={id}
|
name={id}
|
||||||
labelText={labelToUse}
|
labelText={labelToUse}
|
||||||
select
|
select
|
||||||
|
helperText={placeholder}
|
||||||
value={typeof value === 'undefined' ? emptyValue : value}
|
value={typeof value === 'undefined' ? emptyValue : value}
|
||||||
disabled={disabled || readonly}
|
disabled={disabled || readonly}
|
||||||
autoFocus={autofocus}
|
autoFocus={autofocus}
|
||||||
@ -57,6 +66,8 @@ function SelectWidget({
|
|||||||
onChange={_onChange}
|
onChange={_onChange}
|
||||||
onBlur={_onBlur}
|
onBlur={_onBlur}
|
||||||
onFocus={_onFocus}
|
onFocus={_onFocus}
|
||||||
|
invalid={invalid}
|
||||||
|
invalidText={errorMessageForField}
|
||||||
InputLabelProps={{
|
InputLabelProps={{
|
||||||
shrink: true,
|
shrink: true,
|
||||||
}}
|
}}
|
||||||
|
@ -1,20 +1,98 @@
|
|||||||
import React from 'react';
|
import React, { FocusEvent, useCallback } from 'react';
|
||||||
import { getTemplate, WidgetProps } from '@rjsf/utils';
|
// @ts-ignore
|
||||||
|
import { TextArea } from '@carbon/react';
|
||||||
|
import {
|
||||||
|
FormContextType,
|
||||||
|
RJSFSchema,
|
||||||
|
StrictRJSFSchema,
|
||||||
|
WidgetProps,
|
||||||
|
} from '@rjsf/utils';
|
||||||
|
|
||||||
function TextareaWidget(props: WidgetProps) {
|
/** The `TextareaWidget` is a widget for rendering input fields as textarea.
|
||||||
const { options, registry } = props;
|
*
|
||||||
const BaseInputTemplate = getTemplate<'BaseInputTemplate'>(
|
* @param props - The `WidgetProps` for this component
|
||||||
'BaseInputTemplate',
|
*/
|
||||||
registry,
|
function TextareaWidget<
|
||||||
options
|
T = any,
|
||||||
|
S extends StrictRJSFSchema = RJSFSchema,
|
||||||
|
F extends FormContextType = any
|
||||||
|
>({
|
||||||
|
id,
|
||||||
|
options = {},
|
||||||
|
value,
|
||||||
|
required,
|
||||||
|
disabled,
|
||||||
|
readonly,
|
||||||
|
autofocus = false,
|
||||||
|
onChange,
|
||||||
|
onBlur,
|
||||||
|
onFocus,
|
||||||
|
label,
|
||||||
|
schema,
|
||||||
|
uiSchema,
|
||||||
|
placeholder,
|
||||||
|
rawErrors = [],
|
||||||
|
}: WidgetProps<T, S, F>) {
|
||||||
|
const handleChange = useCallback(
|
||||||
|
({ target: { value } }: React.ChangeEvent<HTMLTextAreaElement>) =>
|
||||||
|
onChange(value === '' ? options.emptyValue : value),
|
||||||
|
[onChange, options.emptyValue]
|
||||||
);
|
);
|
||||||
|
|
||||||
let rows: string | number = 5;
|
const handleBlur = useCallback(
|
||||||
if (typeof options.rows === 'string' || typeof options.rows === 'number') {
|
({ target: { value } }: FocusEvent<HTMLTextAreaElement>) =>
|
||||||
rows = options.rows;
|
onBlur(id, value),
|
||||||
|
[onBlur, id]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleFocus = useCallback(
|
||||||
|
({ target: { value } }: FocusEvent<HTMLTextAreaElement>) =>
|
||||||
|
onFocus(id, value),
|
||||||
|
[id, onFocus]
|
||||||
|
);
|
||||||
|
|
||||||
|
let labelToUse = label;
|
||||||
|
if (uiSchema && uiSchema['ui:title']) {
|
||||||
|
labelToUse = uiSchema['ui:title'];
|
||||||
|
} else if (schema && schema.title) {
|
||||||
|
labelToUse = schema.title;
|
||||||
|
}
|
||||||
|
if (required) {
|
||||||
|
labelToUse = `${labelToUse}*`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return <BaseInputTemplate {...props} multiline rows={rows} />;
|
let invalid = false;
|
||||||
|
let errorMessageForField = null;
|
||||||
|
if (rawErrors && rawErrors.length > 0) {
|
||||||
|
invalid = true;
|
||||||
|
errorMessageForField = `${labelToUse.replace(/\*$/, '')} ${rawErrors[0]}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<TextArea
|
||||||
|
id={id}
|
||||||
|
name={id}
|
||||||
|
className="form-control"
|
||||||
|
value={value || ''}
|
||||||
|
labelText={labelToUse}
|
||||||
|
placeholder={placeholder}
|
||||||
|
required={required}
|
||||||
|
disabled={disabled}
|
||||||
|
readOnly={readonly}
|
||||||
|
autoFocus={autofocus}
|
||||||
|
rows={options.rows}
|
||||||
|
onBlur={handleBlur}
|
||||||
|
onFocus={handleFocus}
|
||||||
|
onChange={handleChange}
|
||||||
|
invalid={invalid}
|
||||||
|
invalidText={errorMessageForField}
|
||||||
|
/>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextareaWidget.defaultProps = {
|
||||||
|
autofocus: false,
|
||||||
|
options: {},
|
||||||
|
};
|
||||||
|
|
||||||
export default TextareaWidget;
|
export default TextareaWidget;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user