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

Improve formSelect to handle hidden prop #1

Merged
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
61 changes: 30 additions & 31 deletions www/src/Addons/Analog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const Analog = ({ values, errors, handleChange, handleCheckbox }) => {
const availableAnalogPins = ANALOG_PINS.filter(
(pin) => !usedPins?.includes(pin),
);

return (
<Section title={t('AddonsConfig:analog-header-text')}>
<div id="AnalogInputOptions" hidden={!values.AnalogInputEnabled}>
Expand Down Expand Up @@ -289,20 +290,9 @@ const Analog = ({ values, errors, handleChange, handleCheckbox }) => {
min={0}
max={100}
/>
<FormCheck
label={t('AddonsConfig:analog-force-circularity')}
type="switch"
id="Forced_circularity"
className="col-sm-3 ms-2"
isInvalid={false}
checked={Boolean(values.forced_circularity)}
onChange={(e) => {
handleCheckbox('forced_circularity', values);
handleChange(e);
}}
/>

<FormSelect
hidden={!Boolean(values.forced_circularity)}
hidden={!values.forced_circularity}
label={t('AddonsConfig:analog-error-label')}
name="analog_error"
className="form-control-sm"
Expand All @@ -313,14 +303,37 @@ const Analog = ({ values, errors, handleChange, handleCheckbox }) => {
onChange={handleChange}
>
{ANALOG_ERROR_RATES.map((o, i) => (
<option
key={`analog_error-option-${i}`}
value={o.value}
>
<option key={`analog_error-option-${i}`} value={o.value}>
{o.label}
</option>
))}
</FormSelect>
<FormControl
hidden={!values.analog_smoothing}
type="number"
label={t('AddonsConfig:smoothing-factor')}
name="smoothing_factor"
className="form-control-sm"
groupClassName="col-sm-3 mb-3"
value={values.smoothing_factor}
error={errors.smoothing_factor}
isInvalid={errors.smoothing_factor}
onChange={handleChange}
min={0}
max={100}
/>
<FormCheck
label={t('AddonsConfig:analog-force-circularity')}
type="switch"
id="Forced_circularity"
className="col-sm-3 ms-2"
isInvalid={false}
checked={Boolean(values.forced_circularity)}
onChange={(e) => {
handleCheckbox('forced_circularity', values);
handleChange(e);
}}
/>
<FormCheck
label={t('AddonsConfig:analog-auto-calibrate')}
type="switch"
Expand All @@ -345,20 +358,6 @@ const Analog = ({ values, errors, handleChange, handleCheckbox }) => {
handleChange(e);
}}
/>
<FormControl
hidden={!values.analog_smoothing}
type="number"
label={t('AddonsConfig:smoothing-factor')}
name="smoothing_factor"
className="form-control-sm"
groupClassName="col-sm-3 mb-3"
value={values.smoothing_factor}
error={errors.smoothing_factor}
isInvalid={errors.smoothing_factor}
onChange={handleChange}
min={0}
max={100}
/>
</Row>
</div>
<FormCheck
Expand Down
59 changes: 29 additions & 30 deletions www/src/Addons/FocusMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,36 +93,35 @@ const FocusMode = ({
/>
</div>
<Row>
{BUTTON_MASKS_OPTIONS.map((mask) =>
values.focusModeButtonLockMask & mask.value ? (
<FormSelect
key={`focusModeButtonLockMask-${mask.label}`}
name="focusModeButtonLockMask"
className="form-select-sm"
groupClassName="col-sm-3 mb-3"
value={values.focusModeButtonLockMask & mask.value}
error={errors.focusModeButtonLockMask}
isInvalid={errors.focusModeButtonLockMask}
onChange={(e) => {
setFieldValue(
'focusModeButtonLockMask',
(values.focusModeButtonLockMask ^ mask.value) |
e.target.value,
);
}}
>
{BUTTON_MASKS_OPTIONS.map((o, i) => (
<option
key={`focusModeButtonLockMask-option-${i}`}
value={o.value}
>
{o.label}
</option>
))}
</FormSelect>
) : (
<></>
),
{BUTTON_MASKS_OPTIONS.map(
(mask) =>
Boolean(values.focusModeButtonLockMask & mask.value) && (
<FormSelect
key={`focusModeButtonLockMask-${mask.label}`}
name="focusModeButtonLockMask"
className="form-select-sm"
groupClassName="col-sm-3 mb-3"
value={values.focusModeButtonLockMask & mask.value}
error={errors.focusModeButtonLockMask}
isInvalid={errors.focusModeButtonLockMask}
onChange={(e) => {
setFieldValue(
'focusModeButtonLockMask',
(values.focusModeButtonLockMask ^ mask.value) |
e.target.value,
);
}}
>
{BUTTON_MASKS_OPTIONS.map((o, i) => (
<option
key={`focusModeButtonLockMask-option-${i}`}
value={o.value}
>
{o.label}
</option>
))}
</FormSelect>
),
)}
<FormSelect
name="focusModeButtonLockMask"
Expand Down
19 changes: 0 additions & 19 deletions www/src/Components/FormSelect.jsx

This file was deleted.

27 changes: 27 additions & 0 deletions www/src/Components/FormSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { Form, FormSelectProps } from 'react-bootstrap';

type FormSelectTypes = {
label?: string;
error?: string;
groupClassName?: string;
hidden?: boolean;
} & FormSelectProps;

const FormSelect = ({
label,
error,
groupClassName = '',
hidden = false,
...props
}: FormSelectTypes) => {
return (
<Form.Group className={groupClassName} hidden={hidden}>
{label && <Form.Label>{label}</Form.Label>}
<Form.Select {...props} />
<Form.Control.Feedback type="invalid">{error}</Form.Control.Feedback>
</Form.Group>
);
};

export default FormSelect;