Skip to content

Commit

Permalink
Fix duplicate field IDs on some setting pages
Browse files Browse the repository at this point in the history
  • Loading branch information
maksis committed Jun 15, 2024
1 parent 1e8062b commit 59fda6f
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/components/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export interface FormProps<ValueType extends Partial<UI.FormValueMap> = UI.FormV
title?: string;
className?: string;
optionTitleFormatter?: UI.OptionTitleParser;
id?: string;
}

interface State<ValueType> {
Expand Down Expand Up @@ -347,6 +348,7 @@ class Form<
onFieldSetting,
location,
optionTitleFormatter,
id,
} = this.props;
const { formValue, error } = this.state;

Expand All @@ -372,6 +374,7 @@ class Form<
{!!title && <div className="ui form header">{title}</div>}
<TcombForm
ref={(c) => (this.form = c)}
id={id}
type={type}
options={options}
value={formValue}
Expand Down
4 changes: 4 additions & 0 deletions src/components/form/tcomb/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,10 @@ export class Form extends React.Component {
}

getSeed() {
if (this.props.id) {
return this.props.id;
}

const rii = this._reactInternalInstance;
if (rii) {
if (rii._hostContainerInfo) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/form/tcomb/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ export declare namespace form {
onChange?: (raw: any, path: Path, kind?: ChangeKind) => void;

context?: any;

id?: string;
}

export class Component extends React.Component {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/Settings/components/LocalSettingForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as UI from 'types/ui';

import { translateForm } from 'utils/FormUtils';
import { useLocation } from 'react-router';
import { SettingSaveContext } from '../effects/useSettingSaveContext';
import { SettingSaveContext, getSettingFormId } from '../effects/useSettingSaveContext';

export interface LocalSettingFormProps
extends Omit<FormProps, 'onSave' | 'fieldDefinitions' | 'value' | 'location'> {
Expand Down Expand Up @@ -50,6 +50,7 @@ const LocalSettingForm: React.FC<LocalSettingFormProps> = ({
<div className="local setting-form">
<Form
ref={(f) => saveContext.addFormRef(keys, f)}
id={getSettingFormId(keys)}
location={location}
onSave={onSave}
fieldDefinitions={definitions}
Expand Down
3 changes: 2 additions & 1 deletion src/routes/Settings/components/RemoteSettingForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Form, {
import * as API from 'types/api';
import * as UI from 'types/ui';
import { useLocation } from 'react-router';
import { SettingSaveContext } from '../effects/useSettingSaveContext';
import { SettingSaveContext, getSettingFormId } from '../effects/useSettingSaveContext';

export interface RemoteSettingFormProps
extends Omit<FormProps, 'onSave' | 'value' | 'fieldDefinitions' | 'location'> {
Expand Down Expand Up @@ -67,6 +67,7 @@ const RemoteSettingForm: React.FC<Props> = ({
<div className="remote setting-form">
<Form
{...other}
id={getSettingFormId(keys)}
ref={(f) => saveContext.addFormRef(keys, f)}
onSave={onSave}
fieldDefinitions={fieldDefinitions}
Expand Down
8 changes: 4 additions & 4 deletions src/routes/Settings/effects/useSettingSaveContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export interface SettingSaveContextProps {
saveContext: SettingSaveContext;
}

const getFormId = (keys: string[]) => {
return keys.join('_');
export const getSettingFormId = (keys: string[]) => {
return keys.join('|');
};

interface UseSaveContextProps {
Expand Down Expand Up @@ -115,7 +115,7 @@ export const useSettingSaveContext = ({
c.hasOwnProperty('save'),
'Invalid setting form component supplied for SaveDecorator (save property missing)',
);
forms.set(getFormId(keys), c);
forms.set(getSettingFormId(keys), c);
} else {
// Switching to another page
// Single forms shouldn't be unmounted otherwise, but using identifiers would still be safer...
Expand All @@ -124,7 +124,7 @@ export const useSettingSaveContext = ({
};

const getFormRef = (keys: string[]) => {
return forms.get(getFormId(keys))!;
return forms.get(getSettingFormId(keys))!;
};

const saveContext: SettingSaveContext = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class WebUserDialog extends Component<Props> {
fieldDefinitions={this.entry}
onFieldSetting={this.onFieldSetting}
onSave={this.onSave}
sourceValue={this.props.user as Entry}
sourceValue={user as Entry}
location={this.props.location}
/>
</Modal>
Expand Down

0 comments on commit 59fda6f

Please sign in to comment.