2020-10-26 10:52:33 +01:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
2021-03-30 13:37:47 +02:00
|
|
|
import React, {useState, useRef, useEffect} from 'react'
|
2021-01-04 21:08:12 +01:00
|
|
|
import {injectIntl, IntlShape} from 'react-intl'
|
2020-10-26 10:52:33 +01:00
|
|
|
|
2020-11-03 19:35:24 +01:00
|
|
|
import {PropertyType} from '../blocks/board'
|
2020-10-26 10:52:33 +01:00
|
|
|
import {Utils} from '../utils'
|
|
|
|
import Menu from '../widgets/menu'
|
2020-10-26 14:43:29 +01:00
|
|
|
import './propertyMenu.scss'
|
|
|
|
|
2020-10-26 10:52:33 +01:00
|
|
|
type Props = {
|
2020-11-03 19:35:24 +01:00
|
|
|
propertyId: string
|
|
|
|
propertyName: string
|
|
|
|
propertyType: PropertyType
|
|
|
|
onNameChanged: (newName: string) => void
|
2020-11-03 20:15:16 +01:00
|
|
|
onTypeChanged: (newType: PropertyType) => void
|
2020-11-03 19:35:24 +01:00
|
|
|
onDelete: (id: string) => void
|
2021-01-04 21:08:12 +01:00
|
|
|
intl: IntlShape
|
2020-10-26 10:52:33 +01:00
|
|
|
}
|
|
|
|
|
2021-03-30 13:37:47 +02:00
|
|
|
function typeDisplayName(intl: IntlShape, type: PropertyType): string {
|
|
|
|
switch (type) {
|
|
|
|
case 'text': return intl.formatMessage({id: 'PropertyType.Text', defaultMessage: 'Text'})
|
|
|
|
case 'number': return intl.formatMessage({id: 'PropertyType.Number', defaultMessage: 'Number'})
|
|
|
|
case 'select': return intl.formatMessage({id: 'PropertyType.Select', defaultMessage: 'Select'})
|
|
|
|
case 'multiSelect': return intl.formatMessage({id: 'PropertyType.MultiSelect', defaultMessage: 'Multi Select'})
|
|
|
|
case 'person': return intl.formatMessage({id: 'PropertyType.Person', defaultMessage: 'Person'})
|
|
|
|
case 'file': return intl.formatMessage({id: 'PropertyType.File', defaultMessage: 'File or Media'})
|
|
|
|
case 'checkbox': return intl.formatMessage({id: 'PropertyType.Checkbox', defaultMessage: 'Checkbox'})
|
|
|
|
case 'url': return intl.formatMessage({id: 'PropertyType.URL', defaultMessage: 'URL'})
|
|
|
|
case 'email': return intl.formatMessage({id: 'PropertyType.Email', defaultMessage: 'Email'})
|
|
|
|
case 'phone': return intl.formatMessage({id: 'PropertyType.Phone', defaultMessage: 'Phone'})
|
|
|
|
case 'createdTime': return intl.formatMessage({id: 'PropertyType.CreatedTime', defaultMessage: 'Created Time'})
|
|
|
|
case 'createdBy': return intl.formatMessage({id: 'PropertyType.CreatedBy', defaultMessage: 'Created By'})
|
|
|
|
case 'updatedTime': return intl.formatMessage({id: 'PropertyType.UpdatedTime', defaultMessage: 'Updated Time'})
|
|
|
|
case 'updatedBy': return intl.formatMessage({id: 'PropertyType.UpdatedBy', defaultMessage: 'Updated By'})
|
|
|
|
default: {
|
|
|
|
Utils.assertFailure(`typeDisplayName, unhandled type: ${type}`)
|
|
|
|
return type
|
2020-10-26 10:52:33 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-30 13:37:47 +02:00
|
|
|
}
|
|
|
|
function typeMenuTitle(intl: IntlShape, type: PropertyType): string {
|
|
|
|
return `${intl.formatMessage({id: 'PropertyMenu.typeTitle', defaultMessage: 'Type'})}: ${typeDisplayName(intl, type)}`
|
|
|
|
}
|
2020-10-26 10:52:33 +01:00
|
|
|
|
2021-03-30 13:37:47 +02:00
|
|
|
const PropertyMenu = React.memo((props: Props) => {
|
|
|
|
const {intl} = props
|
|
|
|
const nameTextbox = useRef<HTMLInputElement>(null)
|
|
|
|
const [name, setName] = useState(props.propertyName)
|
2020-10-26 10:52:33 +01:00
|
|
|
|
2021-03-31 22:44:02 +02:00
|
|
|
const deleteText = intl.formatMessage({
|
|
|
|
id: 'PropertyType.Delete',
|
|
|
|
defaultMessage: 'Delete',
|
|
|
|
})
|
|
|
|
|
2021-03-30 13:37:47 +02:00
|
|
|
useEffect(() => {
|
|
|
|
nameTextbox.current?.focus()
|
|
|
|
nameTextbox.current?.setSelectionRange(0, name.length)
|
|
|
|
}, [])
|
2021-01-04 21:08:12 +01:00
|
|
|
|
2021-03-30 13:37:47 +02:00
|
|
|
return (
|
|
|
|
<Menu>
|
|
|
|
<input
|
|
|
|
ref={nameTextbox}
|
|
|
|
type='text'
|
|
|
|
className='PropertyMenu menu-textbox'
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
onChange={(e) => setName(e.target.value)}
|
|
|
|
value={name}
|
|
|
|
onBlur={() => props.onNameChanged(name)}
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
if (e.keyCode === 13 || e.keyCode === 27) {
|
|
|
|
props.onNameChanged(name)
|
|
|
|
e.stopPropagation()
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Menu.SubMenu
|
|
|
|
id='type'
|
|
|
|
name={typeMenuTitle(intl, props.propertyType)}
|
|
|
|
>
|
|
|
|
<Menu.Label>
|
|
|
|
<b>
|
|
|
|
{props.intl.formatMessage({id: 'PropertyMenu.changeType', defaultMessage: 'Change property type'})}
|
|
|
|
</b>
|
|
|
|
</Menu.Label>
|
2021-01-04 21:08:12 +01:00
|
|
|
|
2021-03-30 13:37:47 +02:00
|
|
|
<Menu.Separator/>
|
2021-01-04 21:08:12 +01:00
|
|
|
|
2020-10-26 10:52:33 +01:00
|
|
|
<Menu.Text
|
2021-03-30 13:37:47 +02:00
|
|
|
id='text'
|
2021-03-31 22:44:02 +02:00
|
|
|
name={typeDisplayName(intl, 'text')}
|
2021-03-30 13:37:47 +02:00
|
|
|
onClick={() => props.onTypeChanged('text')}
|
2020-10-26 10:52:33 +01:00
|
|
|
/>
|
2021-03-30 13:37:47 +02:00
|
|
|
<Menu.Text
|
|
|
|
id='number'
|
2021-03-31 22:44:02 +02:00
|
|
|
name={typeDisplayName(intl, 'number')}
|
2021-03-30 13:37:47 +02:00
|
|
|
onClick={() => props.onTypeChanged('number')}
|
|
|
|
/>
|
|
|
|
<Menu.Text
|
|
|
|
id='email'
|
2021-03-31 22:44:02 +02:00
|
|
|
name={typeDisplayName(intl, 'email')}
|
2021-03-30 13:37:47 +02:00
|
|
|
onClick={() => props.onTypeChanged('email')}
|
|
|
|
/>
|
|
|
|
<Menu.Text
|
|
|
|
id='select'
|
2021-03-31 22:44:02 +02:00
|
|
|
name={typeDisplayName(intl, 'select')}
|
2021-03-30 13:37:47 +02:00
|
|
|
onClick={() => props.onTypeChanged('select')}
|
|
|
|
/>
|
|
|
|
<Menu.Text
|
|
|
|
id='createdTime'
|
2021-03-31 22:44:02 +02:00
|
|
|
name={typeDisplayName(intl, 'createdTime')}
|
2021-03-30 13:37:47 +02:00
|
|
|
onClick={() => props.onTypeChanged('createdTime')}
|
|
|
|
/>
|
|
|
|
<Menu.Text
|
|
|
|
id='updatedTime'
|
2021-03-31 22:44:02 +02:00
|
|
|
name={typeDisplayName(intl, 'updatedTime')}
|
2021-03-30 13:37:47 +02:00
|
|
|
onClick={() => props.onTypeChanged('updatedTime')}
|
|
|
|
/>
|
|
|
|
</Menu.SubMenu>
|
|
|
|
<Menu.Text
|
|
|
|
id='delete'
|
2021-03-31 22:44:02 +02:00
|
|
|
name={deleteText}
|
2021-03-30 13:37:47 +02:00
|
|
|
onClick={() => props.onDelete(props.propertyId)}
|
|
|
|
/>
|
|
|
|
</Menu>
|
|
|
|
)
|
|
|
|
})
|
2021-01-04 21:08:12 +01:00
|
|
|
|
|
|
|
export default injectIntl(PropertyMenu)
|