-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathRoomNameInput.js
129 lines (109 loc) · 3.86 KB
/
RoomNameInput.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import CONST from '../CONST';
import ONYXKEYS from '../ONYXKEYS';
import compose from '../libs/compose';
import withLocalize, {withLocalizePropTypes} from './withLocalize';
import withFullPolicy, {fullPolicyDefaultProps, fullPolicyPropTypes} from '../pages/workspace/withFullPolicy';
import TextInput from './TextInput';
const propTypes = {
/** Callback to execute when the text input is modified correctly */
onChangeText: PropTypes.func,
/** Room name to show in input field. This should include the '#' already prefixed to the name */
value: PropTypes.string,
/** Whether we should show the input as disabled */
disabled: PropTypes.bool,
/** Error text to show */
errorText: PropTypes.string,
...withLocalizePropTypes,
...fullPolicyPropTypes,
/* Onyx Props */
/** All reports shared with the user */
reports: PropTypes.shape({
/** The report name */
reportName: PropTypes.string,
/** ID of the report */
reportID: PropTypes.number,
}).isRequired,
/** The policies which the user has access to and which the report could be tied to */
policies: PropTypes.shape({
/** The policy name */
name: PropTypes.string,
/** ID of the policy */
id: PropTypes.string,
}).isRequired,
/** A ref forwarded to the TextInput */
forwardedRef: PropTypes.func,
};
const defaultProps = {
onChangeText: () => {},
value: '',
disabled: false,
errorText: '',
...fullPolicyDefaultProps,
forwardedRef: () => {},
};
class RoomNameInput extends Component {
constructor(props) {
super(props);
this.setModifiedRoomName = this.setModifiedRoomName.bind(this);
}
/**
* Calls the onChangeText callback with a modified room name
* @param {Event} event
*/
setModifiedRoomName(event) {
const roomName = event.nativeEvent.text;
const modifiedRoomName = this.modifyRoomName(roomName);
this.props.onChangeText(modifiedRoomName);
}
/**
* Modifies the room name to follow our conventions:
* - Max length 80 characters
* - Cannot not include space or special characters, and we automatically apply an underscore for spaces
* - Must be lowercase
* @param {String} roomName
* @returns {String}
*/
modifyRoomName(roomName) {
const modifiedRoomNameWithoutHash = roomName
.replace(/ /g, '_')
.replace(/[^a-zA-Z\d_]/g, '')
.substr(0, CONST.REPORT.MAX_ROOM_NAME_LENGTH)
.toLowerCase();
return `${CONST.POLICY.ROOM_PREFIX}${modifiedRoomNameWithoutHash}`;
}
render() {
return (
<TextInput
ref={this.props.forwardedRef}
disabled={this.props.disabled}
label={this.props.translate('newRoomPage.roomName')}
prefixCharacter={CONST.POLICY.ROOM_PREFIX}
placeholder={this.props.translate('newRoomPage.social')}
onChange={this.setModifiedRoomName}
value={this.props.value.substring(1)} // Since the room name always starts with a prefix, we omit the first character to avoid displaying it twice.
errorText={this.props.errorText}
autoCapitalize="none"
/>
);
}
}
RoomNameInput.propTypes = propTypes;
RoomNameInput.defaultProps = defaultProps;
export default compose(
withLocalize,
withFullPolicy,
withOnyx({
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
}),
)(React.forwardRef((props, ref) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<RoomNameInput {...props} forwardedRef={ref} />
)));