Skip to content

Commit

Permalink
Use the new Select component everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-signal committed Jun 3, 2021
1 parent cdf8b0b commit 016ef8a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 67 deletions.
4 changes: 2 additions & 2 deletions ts/components/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import React from 'react';
import React, { ChangeEvent } from 'react';
import classNames from 'classnames';

export type Option = Readonly<{
Expand All @@ -19,7 +19,7 @@ export type PropsType = Readonly<{
export function Select(props: PropsType): JSX.Element {
const { moduleClassName, value, options, onChange } = props;

const onSelectChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
const onSelectChange = (event: ChangeEvent<HTMLSelectElement>) => {
onChange(event.target.value);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { LocalizerType } from '../../../types/Util';
import { PanelRow } from './PanelRow';
import { PanelSection } from './PanelSection';
import { AccessControlClass } from '../../../textsecure.d';
import { Select } from '../../Select';

export type PropsType = {
accessEnum: typeof AccessControlClass.AccessRequired;
Expand Down Expand Up @@ -36,8 +37,8 @@ export const GroupLinkManagement: React.ComponentType<PropsType> = ({
}

const createEventHandler = (handleEvent: (x: boolean) => void) => {
return (event: React.ChangeEvent<HTMLSelectElement>) => {
handleEvent(event.target.value === 'true');
return (value: string) => {
handleEvent(value === 'true');
};
};

Expand All @@ -57,19 +58,20 @@ export const GroupLinkManagement: React.ComponentType<PropsType> = ({
label={i18n('ConversationDetails--group-link')}
right={
isAdmin ? (
<div className="module-conversation-details-select">
<select
onChange={createEventHandler(changeHasGroupLink)}
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>
<Select
onChange={createEventHandler(changeHasGroupLink)}
options={[
{
text: i18n('on'),
value: i18n('on'),
},
{
text: i18n('off'),
value: i18n('off'),
},
]}
value={String(Boolean(hasGroupLink))}
/>
) : null
}
/>
Expand Down Expand Up @@ -112,21 +114,22 @@ export const GroupLinkManagement: React.ComponentType<PropsType> = ({
info={i18n('GroupLinkManagement--approve-info')}
label={i18n('GroupLinkManagement--approve-label')}
right={
<div className="module-conversation-details-select">
<select
onChange={createEventHandler(
setAccessControlAddFromInviteLinkSetting
)}
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>
<Select
onChange={createEventHandler(
setAccessControlAddFromInviteLinkSetting
)}
options={[
{
text: i18n('on'),
value: i18n('on'),
},
{
text: i18n('off'),
value: i18n('off'),
},
]}
value={String(membersNeedAdminApproval)}
/>
}
/>
</PanelSection>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AccessControlClass } from '../../../textsecure.d';

import { PanelRow } from './PanelRow';
import { PanelSection } from './PanelSection';
import { Select } from '../../Select';

export type PropsType = {
accessEnum: typeof AccessControlClass.AccessRequired;
Expand All @@ -30,15 +31,11 @@ export const GroupV2Permissions: React.ComponentType<PropsType> = ({
throw new Error('GroupV2Permissions rendered without a conversation');
}

const updateAccessControlAttributes = (
event: React.ChangeEvent<HTMLSelectElement>
) => {
setAccessControlAttributesSetting(Number(event.target.value));
const updateAccessControlAttributes = (value: string) => {
setAccessControlAttributesSetting(Number(value));
};
const updateAccessControlMembers = (
event: React.ChangeEvent<HTMLSelectElement>
) => {
setAccessControlMembersSetting(Number(event.target.value));
const updateAccessControlMembers = (value: string) => {
setAccessControlMembersSetting(Number(value));
};
const accessControlOptions = getAccessControlOptions(accessEnum, i18n);

Expand All @@ -48,36 +45,22 @@ export const GroupV2Permissions: React.ComponentType<PropsType> = ({
label={i18n('ConversationDetails--add-members-label')}
info={i18n('ConversationDetails--add-members-info')}
right={
<div className="module-conversation-details-select">
<select
onChange={updateAccessControlMembers}
value={conversation.accessControlMembers}
>
{accessControlOptions.map(({ name, value }) => (
<option aria-label={name} key={name} value={value}>
{name}
</option>
))}
</select>
</div>
<Select
onChange={updateAccessControlMembers}
options={accessControlOptions}
value={String(conversation.accessControlMembers)}
/>
}
/>
<PanelRow
label={i18n('ConversationDetails--group-info-label')}
info={i18n('ConversationDetails--group-info-info')}
right={
<div className="module-conversation-details-select">
<select
onChange={updateAccessControlAttributes}
value={conversation.accessControlAttributes}
>
{accessControlOptions.map(({ name, value }) => (
<option aria-label={name} key={name} value={value}>
{name}
</option>
))}
</select>
</div>
<Select
onChange={updateAccessControlAttributes}
options={accessControlOptions}
value={String(conversation.accessControlAttributes)}
/>
}
/>
</PanelSection>
Expand Down
6 changes: 3 additions & 3 deletions ts/util/getAccessControlOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { LocalizerType } from '../types/Util';
import { AccessControlClass } from '../textsecure.d';

type AccessControlOption = {
name: string;
text: string;
value: number;
};

Expand All @@ -15,11 +15,11 @@ export function getAccessControlOptions(
): Array<AccessControlOption> {
return [
{
name: i18n('GroupV2--all-members'),
text: i18n('GroupV2--all-members'),
value: accessEnum.MEMBER,
},
{
name: i18n('GroupV2--only-admins'),
text: i18n('GroupV2--only-admins'),
value: accessEnum.ADMINISTRATOR,
},
];
Expand Down

0 comments on commit 016ef8a

Please sign in to comment.