Use the new Select component everywhere

This commit is contained in:
Josh Perez 2021-06-03 18:12:23 -04:00 committed by GitHub
parent cdf8b0b28d
commit 016ef8af79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 67 deletions

View file

@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC // Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import React from 'react'; import React, { ChangeEvent } from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
export type Option = Readonly<{ export type Option = Readonly<{
@ -19,7 +19,7 @@ export type PropsType = Readonly<{
export function Select(props: PropsType): JSX.Element { export function Select(props: PropsType): JSX.Element {
const { moduleClassName, value, options, onChange } = props; const { moduleClassName, value, options, onChange } = props;
const onSelectChange = (event: React.ChangeEvent<HTMLSelectElement>) => { const onSelectChange = (event: ChangeEvent<HTMLSelectElement>) => {
onChange(event.target.value); onChange(event.target.value);
}; };

View file

@ -9,6 +9,7 @@ import { LocalizerType } from '../../../types/Util';
import { PanelRow } from './PanelRow'; import { PanelRow } from './PanelRow';
import { PanelSection } from './PanelSection'; import { PanelSection } from './PanelSection';
import { AccessControlClass } from '../../../textsecure.d'; import { AccessControlClass } from '../../../textsecure.d';
import { Select } from '../../Select';
export type PropsType = { export type PropsType = {
accessEnum: typeof AccessControlClass.AccessRequired; accessEnum: typeof AccessControlClass.AccessRequired;
@ -36,8 +37,8 @@ export const GroupLinkManagement: React.ComponentType<PropsType> = ({
} }
const createEventHandler = (handleEvent: (x: boolean) => void) => { const createEventHandler = (handleEvent: (x: boolean) => void) => {
return (event: React.ChangeEvent<HTMLSelectElement>) => { return (value: string) => {
handleEvent(event.target.value === 'true'); handleEvent(value === 'true');
}; };
}; };
@ -57,19 +58,20 @@ export const GroupLinkManagement: React.ComponentType<PropsType> = ({
label={i18n('ConversationDetails--group-link')} label={i18n('ConversationDetails--group-link')}
right={ right={
isAdmin ? ( isAdmin ? (
<div className="module-conversation-details-select"> <Select
<select
onChange={createEventHandler(changeHasGroupLink)} onChange={createEventHandler(changeHasGroupLink)}
options={[
{
text: i18n('on'),
value: i18n('on'),
},
{
text: i18n('off'),
value: i18n('off'),
},
]}
value={String(Boolean(hasGroupLink))} value={String(Boolean(hasGroupLink))}
> />
<option value="true" aria-label={i18n('on')}>
{i18n('on')}
</option>
<option value="false" aria-label={i18n('off')}>
{i18n('off')}
</option>
</select>
</div>
) : null ) : null
} }
/> />
@ -112,21 +114,22 @@ export const GroupLinkManagement: React.ComponentType<PropsType> = ({
info={i18n('GroupLinkManagement--approve-info')} info={i18n('GroupLinkManagement--approve-info')}
label={i18n('GroupLinkManagement--approve-label')} label={i18n('GroupLinkManagement--approve-label')}
right={ right={
<div className="module-conversation-details-select"> <Select
<select
onChange={createEventHandler( onChange={createEventHandler(
setAccessControlAddFromInviteLinkSetting setAccessControlAddFromInviteLinkSetting
)} )}
options={[
{
text: i18n('on'),
value: i18n('on'),
},
{
text: i18n('off'),
value: i18n('off'),
},
]}
value={String(membersNeedAdminApproval)} value={String(membersNeedAdminApproval)}
> />
<option value="true" aria-label={i18n('on')}>
{i18n('on')}
</option>
<option value="false" aria-label={i18n('off')}>
{i18n('off')}
</option>
</select>
</div>
} }
/> />
</PanelSection> </PanelSection>

View file

@ -10,6 +10,7 @@ import { AccessControlClass } from '../../../textsecure.d';
import { PanelRow } from './PanelRow'; import { PanelRow } from './PanelRow';
import { PanelSection } from './PanelSection'; import { PanelSection } from './PanelSection';
import { Select } from '../../Select';
export type PropsType = { export type PropsType = {
accessEnum: typeof AccessControlClass.AccessRequired; accessEnum: typeof AccessControlClass.AccessRequired;
@ -30,15 +31,11 @@ export const GroupV2Permissions: React.ComponentType<PropsType> = ({
throw new Error('GroupV2Permissions rendered without a conversation'); throw new Error('GroupV2Permissions rendered without a conversation');
} }
const updateAccessControlAttributes = ( const updateAccessControlAttributes = (value: string) => {
event: React.ChangeEvent<HTMLSelectElement> setAccessControlAttributesSetting(Number(value));
) => {
setAccessControlAttributesSetting(Number(event.target.value));
}; };
const updateAccessControlMembers = ( const updateAccessControlMembers = (value: string) => {
event: React.ChangeEvent<HTMLSelectElement> setAccessControlMembersSetting(Number(value));
) => {
setAccessControlMembersSetting(Number(event.target.value));
}; };
const accessControlOptions = getAccessControlOptions(accessEnum, i18n); const accessControlOptions = getAccessControlOptions(accessEnum, i18n);
@ -48,36 +45,22 @@ export const GroupV2Permissions: React.ComponentType<PropsType> = ({
label={i18n('ConversationDetails--add-members-label')} label={i18n('ConversationDetails--add-members-label')}
info={i18n('ConversationDetails--add-members-info')} info={i18n('ConversationDetails--add-members-info')}
right={ right={
<div className="module-conversation-details-select"> <Select
<select
onChange={updateAccessControlMembers} onChange={updateAccessControlMembers}
value={conversation.accessControlMembers} options={accessControlOptions}
> value={String(conversation.accessControlMembers)}
{accessControlOptions.map(({ name, value }) => ( />
<option aria-label={name} key={name} value={value}>
{name}
</option>
))}
</select>
</div>
} }
/> />
<PanelRow <PanelRow
label={i18n('ConversationDetails--group-info-label')} label={i18n('ConversationDetails--group-info-label')}
info={i18n('ConversationDetails--group-info-info')} info={i18n('ConversationDetails--group-info-info')}
right={ right={
<div className="module-conversation-details-select"> <Select
<select
onChange={updateAccessControlAttributes} onChange={updateAccessControlAttributes}
value={conversation.accessControlAttributes} options={accessControlOptions}
> value={String(conversation.accessControlAttributes)}
{accessControlOptions.map(({ name, value }) => ( />
<option aria-label={name} key={name} value={value}>
{name}
</option>
))}
</select>
</div>
} }
/> />
</PanelSection> </PanelSection>

View file

@ -5,7 +5,7 @@ import { LocalizerType } from '../types/Util';
import { AccessControlClass } from '../textsecure.d'; import { AccessControlClass } from '../textsecure.d';
type AccessControlOption = { type AccessControlOption = {
name: string; text: string;
value: number; value: number;
}; };
@ -15,11 +15,11 @@ export function getAccessControlOptions(
): Array<AccessControlOption> { ): Array<AccessControlOption> {
return [ return [
{ {
name: i18n('GroupV2--all-members'), text: i18n('GroupV2--all-members'),
value: accessEnum.MEMBER, value: accessEnum.MEMBER,
}, },
{ {
name: i18n('GroupV2--only-admins'), text: i18n('GroupV2--only-admins'),
value: accessEnum.ADMINISTRATOR, value: accessEnum.ADMINISTRATOR,
}, },
]; ];