Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Field: make id optional, generate one if not provided #4298

Merged
merged 2 commits into from
Mar 30, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ export default class ManageEventIndexDialog extends React.Component {
totalRooms: formatCountLong(this.state.roomCount),
})} <br />
<Field
id={"crawlerSleepTimeMs"}
label={_t('Message downloading sleep time(ms)')}
type='number'
value={this.state.crawlerSleepTime}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,6 @@ export default class CreateSecretStorageDialog extends React.PureComponent {
<div>{_t("Enter your account password to confirm the upgrade:")}</div>
<div><Field
type="password"
id="mx_CreateSecretStorage_accountPassword"
label={_t("Password")}
value={this.state.accountPassword}
onChange={this._onAccountPasswordChange}
Expand Down Expand Up @@ -497,7 +496,6 @@ export default class CreateSecretStorageDialog extends React.PureComponent {
<div className="mx_CreateSecretStorageDialog_passPhraseContainer">
<Field
type="password"
id="mx_CreateSecretStorageDialog_passPhraseField"
className="mx_CreateSecretStorageDialog_passPhraseField"
onChange={this._onPassPhraseChange}
value={this.state.passPhrase}
Expand Down Expand Up @@ -574,7 +572,6 @@ export default class CreateSecretStorageDialog extends React.PureComponent {
<div className="mx_CreateSecretStorageDialog_passPhraseContainer">
<Field
type="password"
id="mx_CreateSecretStorageDialog_passPhraseField"
onChange={this._onPassPhraseConfirmChange}
value={this.state.passPhraseConfirm}
className="mx_CreateSecretStorageDialog_passPhraseField"
Expand Down
3 changes: 0 additions & 3 deletions src/components/structures/auth/ForgotPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ export default createReactClass({
<form onSubmit={this.onSubmitForm}>
<div className="mx_AuthBody_fieldRow">
<Field
id="mx_ForgotPassword_email"
name="reset_email" // define a name so browser's password autofill gets less confused
type="text"
label={_t('Email')}
Expand All @@ -307,15 +306,13 @@ export default createReactClass({
</div>
<div className="mx_AuthBody_fieldRow">
<Field
id="mx_ForgotPassword_password"
name="reset_password"
type="password"
label={_t('Password')}
value={this.state.password}
onChange={this.onInputChanged.bind(this, "password")}
/>
<Field
id="mx_ForgotPassword_passwordConfirm"
name="reset_password_confirm"
type="password"
label={_t('Confirm')}
Expand Down
1 change: 0 additions & 1 deletion src/components/structures/auth/SoftLogout.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ export default class SoftLogout extends React.Component {
<p>{introText}</p>
{error}
<Field
id="softlogout_password"
type="password"
label={_t("Password")}
onChange={this.onPasswordChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ export const PasswordAuthEntry = createReactClass({
<p>{ _t("Confirm your identity by entering your account password below.") }</p>
<form onSubmit={this._onSubmit} className="mx_InteractiveAuthEntryComponents_passwordSection">
<Field
id="mx_InteractiveAuthEntryComponents_password"
className={passwordBoxClass}
type="password"
name="passwordField"
Expand Down
3 changes: 2 additions & 1 deletion src/components/views/auth/ModularServerConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export default class ModularServerConfig extends ServerConfig {
)}
<form onSubmit={this.onSubmit} autoComplete="off" action={null}>
<div className="mx_ServerConfig_fields">
<Field id="mx_ServerConfig_hsUrl"
<Field
id="mx_ServerConfig_hsUrl"
label={_t("Server Name")}
placeholder={this.props.serverConfig.hsUrl}
value={this.state.hsUrl}
Expand Down
5 changes: 0 additions & 5 deletions src/components/views/auth/PasswordLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ export default class PasswordLogin extends React.Component {
classes.error = this.props.loginIncorrect && !this.state.username;
return <Field
className={classNames(classes)}
id="mx_PasswordLogin_email"
name="username" // make it a little easier for browser's remember-password
key="email_input"
type="text"
Expand All @@ -209,7 +208,6 @@ export default class PasswordLogin extends React.Component {
classes.error = this.props.loginIncorrect && !this.state.username;
return <Field
className={classNames(classes)}
id="mx_PasswordLogin_username"
name="username" // make it a little easier for browser's remember-password
key="username_input"
type="text"
Expand All @@ -233,7 +231,6 @@ export default class PasswordLogin extends React.Component {

return <Field
className={classNames(classes)}
id="mx_PasswordLogin_phoneNumber"
name="phoneNumber"
key="phone_input"
type="text"
Expand Down Expand Up @@ -290,7 +287,6 @@ export default class PasswordLogin extends React.Component {
<div className="mx_Login_type_container">
<label className="mx_Login_type_label">{ _t('Sign in with') }</label>
<Field
id="mx_PasswordLogin_type"
element="select"
value={this.state.loginType}
onChange={this.onLoginTypeChange}
Expand Down Expand Up @@ -328,7 +324,6 @@ export default class PasswordLogin extends React.Component {
{loginField}
<Field
className={pwFieldClass}
id="mx_PasswordLogin_password"
type="password"
name="password"
label={_t('Password')}
Expand Down
2 changes: 0 additions & 2 deletions src/components/views/auth/RegistrationForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ export default createReactClass({
_t("Email") :
_t("Email (optional)");
return <Field
id="mx_RegistrationForm_email"
ref={field => this[FIELD_EMAIL] = field}
type="text"
label={emailPlaceholder}
Expand Down Expand Up @@ -524,7 +523,6 @@ export default createReactClass({
onOptionChange={this.onPhoneCountryChange}
/>;
return <Field
id="mx_RegistrationForm_phoneNumber"
ref={field => this[FIELD_PHONE_NUMBER] = field}
type="text"
label={phoneLabel}
Expand Down
5 changes: 3 additions & 2 deletions src/components/views/auth/ServerConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ export default class ServerConfig extends React.PureComponent {
{sub}
</a>,
})}
<Field id="mx_ServerConfig_hsUrl"
<Field
id="mx_ServerConfig_hsUrl"
label={_t("Homeserver URL")}
placeholder={this.props.serverConfig.hsUrl}
value={this.state.hsUrl}
Expand All @@ -246,7 +247,7 @@ export default class ServerConfig extends React.PureComponent {
{sub}
</a>,
})}
<Field id="mx_ServerConfig_isUrl"
<Field
label={_t("Identity Server URL")}
placeholder={this.props.serverConfig.isUrl}
value={this.state.isUrl || ''}
Expand Down
2 changes: 0 additions & 2 deletions src/components/views/dialogs/BugReportDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ export default class BugReportDialog extends React.Component {
) }
</b></p>
<Field
id="mx_BugReportDialog_issueUrl"
type="text"
className="mx_BugReportDialog_field_input"
label={_t("GitHub issue")}
Expand All @@ -175,7 +174,6 @@ export default class BugReportDialog extends React.Component {
placeholder="https://github.com/vector-im/riot-web/issues/..."
/>
<Field
id="mx_BugReportDialog_notes"
className="mx_BugReportDialog_field_input"
element="textarea"
label={_t("Notes")}
Expand Down
6 changes: 3 additions & 3 deletions src/components/views/dialogs/CreateRoomDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default createReactClass({
const domain = MatrixClientPeg.get().getDomain();
aliasField = (
<div className="mx_CreateRoomDialog_aliasContainer">
<RoomAliasField id="alias" ref={ref => this._aliasFieldRef = ref} onChange={this.onAliasChange} domain={domain} value={this.state.alias} />
<RoomAliasField ref={ref => this._aliasFieldRef = ref} onChange={this.onAliasChange} domain={domain} value={this.state.alias} />
</div>
);
} else {
Expand All @@ -188,8 +188,8 @@ export default createReactClass({
>
<form onSubmit={this.onOk} onKeyDown={this._onKeyDown}>
<div className="mx_Dialog_content">
<Field id="name" ref={ref => this._nameFieldRef = ref} label={ _t('Name') } onChange={this.onNameChange} onValidate={this.onNameValidate} value={this.state.name} className="mx_CreateRoomDialog_name" />
<Field id="topic" label={ _t('Topic (optional)') } onChange={this.onTopicChange} value={this.state.topic} />
<Field ref={ref => this._nameFieldRef = ref} label={ _t('Name') } onChange={this.onNameChange} onValidate={this.onNameValidate} value={this.state.name} className="mx_CreateRoomDialog_name" />
<Field label={ _t('Topic (optional)') } onChange={this.onTopicChange} value={this.state.topic} />
<LabelledToggleSwitch label={ _t("Make this room public")} onChange={this.onPublicChange} value={this.state.isPublic} />
{ privateLabel }
{ publicLabel }
Expand Down
1 change: 0 additions & 1 deletion src/components/views/dialogs/DeactivateAccountDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ export default class DeactivateAccountDialog extends React.Component {

<p>{ _t("To continue, please enter your password:") }</p>
<Field
id="mx_DeactivateAccountDialog_password"
type="password"
label={_t('Password')}
onChange={this._onPasswordFieldChange}
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/dialogs/DevtoolsDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class FilteredList extends React.PureComponent {
render() {
const TruncatedList = sdk.getComponent("elements.TruncatedList");
return <div>
<Field id="DevtoolsDialog_FilteredList_filter" label={_t('Filter results')} autoFocus={true} size={64}
<Field label={_t('Filter results')} autoFocus={true} size={64}
type="text" autoComplete="off" value={this.props.query} onChange={this.onQuery}
className="mx_TextInputDialog_input mx_DevTools_RoomStateExplorer_query"
// force re-render so that autoFocus is applied when this component is re-used
Expand Down
1 change: 0 additions & 1 deletion src/components/views/dialogs/ReportEventDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export default class ReportEventDialog extends PureComponent {
</p>
{adminMessage}
<Field
id="mx_ReportEventDialog_reason"
className="mx_ReportEventDialog_reason"
element="textarea"
label={_t("Reason")}
Expand Down
1 change: 0 additions & 1 deletion src/components/views/dialogs/TextInputDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export default createReactClass({
</div>
<div>
<Field
id="mx_TextInputDialog_field"
className="mx_TextInputDialog_input"
ref={this._field}
type="text"
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/elements/EditableItemList.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class EditableItemList extends React.Component {
return (
<form onSubmit={this._onItemAdded} autoComplete="off"
noValidate={true} className="mx_EditableItemList_newItem">
<Field id={`mx_EditableItemList_new_${this.props.id}`} label={this.props.placeholder} type="text"
<Field label={this.props.placeholder} type="text"
autoComplete="off" value={this.props.newItem || ""} onChange={this._onNewItemChanged}
list={this.props.suggestionsListId} />
<AccessibleButton onClick={this._onItemAdded} kind="primary" type="submit">
Expand Down
17 changes: 13 additions & 4 deletions src/components/views/elements/Field.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ import { debounce } from 'lodash';
// Invoke validation from user input (when typing, etc.) at most once every N ms.
const VALIDATION_THROTTLE_MS = 200;

const BASE_ID = "mx_Field";
let count = 1;
function getId() {
return `${BASE_ID}_${count++}`;
}

export default class Field extends React.PureComponent {
static propTypes = {
// The field's ID, which binds the input and label together.
// The field's ID, which binds the input and label together. Immutable.
id: PropTypes.string.isRequired,
// The element to create. Defaults to "input".
// To define options for a select, use <Field><option ... /></Field>
Expand Down Expand Up @@ -63,13 +69,15 @@ export default class Field extends React.PureComponent {
// All other props pass through to the <input>.
};

constructor() {
super();
constructor(props) {
super(props);
this.state = {
valid: undefined,
feedback: undefined,
focused: false,
};

this.id = this.props.id || getId();
}

onFocus = (ev) => {
Expand Down Expand Up @@ -167,6 +175,7 @@ export default class Field extends React.PureComponent {
inputProps.type = inputProps.type || "text";
inputProps.ref = input => this.input = input;
inputProps.placeholder = inputProps.placeholder || inputProps.label;
inputProps.id = this.id; // this overwrites the id from props

inputProps.onFocus = this.onFocus;
inputProps.onChange = this.onChange;
Expand Down Expand Up @@ -211,7 +220,7 @@ export default class Field extends React.PureComponent {
return <div className={fieldClasses}>
{prefixContainer}
{fieldInput}
<label htmlFor={this.props.id}>{this.props.label}</label>
<label htmlFor={this.id}>{this.props.label}</label>
{postfixContainer}
{fieldTooltip}
</div>;
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/elements/PowerSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default createReactClass({
const label = typeof this.props.label === "undefined" ? _t("Power level") : this.props.label;
if (this.state.custom) {
picker = (
<Field id={`powerSelector_custom_${this.props.powerLevelKey}`} type="number"
<Field type="number"
label={label} max={this.props.maxValue}
onBlur={this.onCustomBlur} onKeyDown={this.onCustomKeyDown} onChange={this.onCustomChange}
value={String(this.state.customValue)} disabled={this.props.disabled} />
Expand All @@ -151,7 +151,7 @@ export default createReactClass({
});

picker = (
<Field id={`powerSelector_notCustom_${this.props.powerLevelKey}`} element="select"
<Field element="select"
label={label} onChange={this.onSelectChange}
value={String(this.state.selectValue)} disabled={this.props.disabled}>
{options}
Expand Down
2 changes: 0 additions & 2 deletions src/components/views/elements/RoomAliasField.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {MatrixClientPeg} from '../../../MatrixClientPeg';
// Controlled form component wrapping Field for inputting a room alias scoped to a given domain
export default class RoomAliasField extends React.PureComponent {
static propTypes = {
id: PropTypes.string.isRequired,
domain: PropTypes.string.isRequired,
onChange: PropTypes.func,
value: PropTypes.string.isRequired,
Expand All @@ -50,7 +49,6 @@ export default class RoomAliasField extends React.PureComponent {
className="mx_RoomAliasField"
prefix={poundSign}
postfix={domain}
id={this.props.id}
ref={ref => this._fieldRef = ref}
onValidate={this._onValidate}
placeholder={_t("e.g. my-room")}
Expand Down
1 change: 0 additions & 1 deletion src/components/views/room_settings/AliasSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class EditableAliasesList extends EditableItemList {
className="mx_EditableItemList_newItem"
>
<RoomAliasField
id={`mx_EditableItemList_new_${this.props.id}`}
ref={this._aliasField}
onChange={onChange}
value={this.props.newItem || ""}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default class RoomProfileSettings extends React.Component {
onChange={this._onAvatarChanged} accept="image/*" />
<div className="mx_ProfileSettings_profile">
<div className="mx_ProfileSettings_controls">
<Field id="profileDisplayName" label={_t("Room Name")}
<Field label={_t("Room Name")}
type="text" value={this.state.displayName} autoComplete="off"
onChange={this._onDisplayNameChanged} disabled={!this.state.canSetName} />
<Field id="profileTopic" label={_t("Room Topic")} disabled={!this.state.canSetTopic}
Expand Down
4 changes: 1 addition & 3 deletions src/components/views/settings/ChangePassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export default createReactClass({
if (!this.state.cachedPassword) {
currentPassword = (
<div className={rowClassName}>
<Field id="mx_ChangePassword_oldPassword"
<Field
type="password"
label={_t('Current password')}
value={this.state.oldPassword}
Expand All @@ -254,7 +254,6 @@ export default createReactClass({
{ currentPassword }
<div className={rowClassName}>
<Field
id="mx_ChangePassword_newPassword"
type="password"
label={passwordLabel}
value={this.state.newPassword}
Expand All @@ -265,7 +264,6 @@ export default createReactClass({
</div>
<div className={rowClassName}>
<Field
id="mx_ChangePassword_newPasswordConfirm"
type="password"
label={_t("Confirm password")}
value={this.state.newPasswordConfirm}
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/settings/ProfileSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class ProfileSettings extends React.Component {
{this.state.userId}
{hostingSignup}
</p>
<Field id="profileDisplayName" label={_t("Display Name")}
<Field label={_t("Display Name")}
type="text" value={this.state.displayName} autoComplete="off"
onChange={this._onDisplayNameChanged} />
</div>
Expand Down
Loading