Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to render enums as select dropdown #4594

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions anet-dictionary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -643,13 +643,21 @@ fields:
type: enumset
label: Choose one or more of the options
helpText: Help text for choosing multiple values
asA: select
htmlSize: 4
choices:
opt1:
label: Option 1
opt2:
label: Option 2
opt3:
label: Option 3
opt4:
label: Option 4
opt5:
label: Option 5
opt6:
label: Option 6
inputFieldName:
type: text
label: Text field
Expand Down
132 changes: 119 additions & 13 deletions client/src/components/FieldHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Form,
FormControl,
FormGroup,
FormSelect,
InputGroup,
ListGroup,
Row,
Expand Down Expand Up @@ -520,30 +521,135 @@ ButtonToggleGroupField.propTypes = {
export const RadioButtonToggleGroupField = ({
field, // { name, value, onChange, onBlur }
form, // also values, setXXXX, handleXXXX, dirty, isValid, status, etc.
asA,
enableClear,
...props
}) => (
<ButtonToggleGroupField field={field} form={form} type="radio" {...props} />
)
}) =>
(asA === "select" && (
<SelectField field={field} form={form} {...props} />
)) || (
<ButtonToggleGroupField
field={field}
form={form}
type="radio"
enableClear={enableClear}
{...props}
/>
)
RadioButtonToggleGroupField.propTypes = {
field: PropTypes.object,
form: PropTypes.object
form: PropTypes.object,
asA: PropTypes.string,
enableClear: PropTypes.bool
}

export const CheckboxButtonToggleGroupField = ({
field, // { name, value, onChange, onBlur }
form, // also values, setXXXX, handleXXXX, dirty, isValid, status, etc.
asA,
enableClear,
...props
}) => (
<ButtonToggleGroupField
field={field}
form={form}
type="checkbox"
{...props}
/>
)
}) =>
(asA === "select" && (
<SelectField field={field} form={form} multiple {...props} />
)) || (
<ButtonToggleGroupField
field={field}
form={form}
type="checkbox"
enableClear={enableClear}
{...props}
/>
)
CheckboxButtonToggleGroupField.propTypes = {
field: PropTypes.object,
form: PropTypes.object
form: PropTypes.object,
asA: PropTypes.string,
enableClear: PropTypes.bool
}

export const SelectField = ({
field, // { name, value, onChange, onBlur }
form, // contains, touched, errors, values, setXXXX, handleXXXX, dirty, isValid, status, etc.
label,
buttons,
multiple,
onChange,
className,
children,
extraColElem,
addon,
vertical,
extraAddon,
...otherProps
}) => {
const { validationState } = getFormGroupValidationState(field, form)
if (validationState) {
className = classNames(className, "is-invalid")
}
const widgetElem = useMemo(
() => (
<FormSelect
{...field}
value={field.value ?? ""}
multiple={multiple}
isInvalid={validationState}
className={className}
onChange={e => {
let newValue
if (!multiple) {
// First (empty) option must be the empty string (""); replace with null when selected
newValue = e.target.value || null
} else {
newValue = []
for (const opt of e.target.options) {
if (opt.selected) {
newValue.push(opt.value)
}
}
}
onChange(newValue)
}}
{...otherProps}
>
{!multiple && <option />}
{buttons.map(option => (
<option key={`${field.name}_${option.value}`} value={option.value}>
{option.label}
</option>
))}
</FormSelect>
),
[field, className, otherProps, buttons, multiple, onChange, validationState]
)
return (
<Field
field={field}
form={form}
label={label}
widgetElem={widgetElem}
extraColElem={extraColElem}
addon={addon}
vertical={vertical}
extraAddon={extraAddon}
>
{children}
</Field>
)
}
SelectField.propTypes = {
field: PropTypes.object,
form: PropTypes.object,
label: PropTypes.string,
buttons: PropTypes.array.isRequired,
multiple: PropTypes.bool,
onChange: PropTypes.func.isRequired,
className: PropTypes.string,
children: PropTypes.any,
extraColElem: PropTypes.object,
addon: PropTypes.object,
vertical: PropTypes.bool,
extraAddon: PropTypes.object
}

export default Field
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/anet-schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ $defs:
type: string
asA:
type: string
htmlSize:
type: number
filters:
type: array
uniqueItems: true
Expand Down
Loading