-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(console): support model argument schema (#3122)
- Loading branch information
Showing
6 changed files
with
362 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
console/packages/starwhale-ui/src/RJSFForm/widgets/CheckboxWidget.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { ChangeEvent, FocusEvent, useCallback } from 'react' | ||
import { | ||
ariaDescribedByIds, | ||
descriptionId, | ||
getTemplate, | ||
labelValue, | ||
schemaRequiresTrueValue, | ||
FormContextType, | ||
RJSFSchema, | ||
StrictRJSFSchema, | ||
WidgetProps, | ||
} from '@rjsf/utils' | ||
|
||
/** The `CheckBoxWidget` is a widget for rendering boolean properties. | ||
* It is typically used to represent a boolean. | ||
* | ||
* @param props - The `WidgetProps` for this component | ||
*/ | ||
function CheckboxWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>({ | ||
schema, | ||
uiSchema, | ||
options, | ||
id, | ||
value, | ||
disabled, | ||
readonly, | ||
label, | ||
hideLabel, | ||
autofocus = false, | ||
onBlur, | ||
onFocus, | ||
onChange, | ||
registry, | ||
}: WidgetProps<T, S, F>) { | ||
const DescriptionFieldTemplate = getTemplate<'DescriptionFieldTemplate', T, S, F>( | ||
'DescriptionFieldTemplate', | ||
registry, | ||
options | ||
) | ||
// Because an unchecked checkbox will cause html5 validation to fail, only add | ||
// the "required" attribute if the field value must be "true", due to the | ||
// "const" or "enum" keywords | ||
const required = schemaRequiresTrueValue<S>(schema) | ||
|
||
const handleChange = useCallback( | ||
(event: ChangeEvent<HTMLInputElement>) => onChange(event.target.checked), | ||
[onChange] | ||
) | ||
|
||
const handleBlur = useCallback( | ||
(event: FocusEvent<HTMLInputElement>) => onBlur(id, event.target.checked), | ||
[onBlur, id] | ||
) | ||
|
||
const handleFocus = useCallback( | ||
(event: FocusEvent<HTMLInputElement>) => onFocus(id, event.target.checked), | ||
[onFocus, id] | ||
) | ||
const description = options.description ?? schema.description | ||
|
||
return ( | ||
<div className={`checkbox flex ${disabled || readonly ? 'disabled' : ''}`}> | ||
{!hideLabel && !!description && ( | ||
<DescriptionFieldTemplate | ||
id={descriptionId<T>(id)} | ||
description={description} | ||
schema={schema} | ||
uiSchema={uiSchema} | ||
registry={registry} | ||
/> | ||
)} | ||
{labelValue( | ||
// eslint-disable-next-line | ||
<label className='control-label' htmlFor={id}> | ||
{label} | ||
</label>, | ||
hideLabel | ||
)} | ||
<input | ||
type='checkbox' | ||
id={id} | ||
name={id} | ||
checked={typeof value === 'undefined' ? false : value} | ||
required={required} | ||
disabled={disabled || readonly} | ||
// eslint-disable-next-line | ||
autoFocus={autofocus} | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
onFocus={handleFocus} | ||
aria-describedby={ariaDescribedByIds<T>(id)} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
export default CheckboxWidget |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.