Commit a0073857 by tungnq

IMPORTANT: Đã hoàn thiện componet email chip input

- Nhấn vào button arrow để xem tất cả
- Nhấn vào TouchOpacity toàn dòng thì sẽ hiển thị text input cho người dùng nhập
parent 3c5a84e8
...@@ -48,6 +48,7 @@ const images = { ...@@ -48,6 +48,7 @@ const images = {
//Icon other //Icon other
icBack: require('./icon/icon_png/back.png'), icBack: require('./icon/icon_png/back.png'),
icBack2: require('./icon/back.png'),
icNext: require('./icon/icon_png/arrow_next.png'), icNext: require('./icon/icon_png/arrow_next.png'),
icCancel: require('./images/iconCancel.png'), icCancel: require('./images/iconCancel.png'),
icMale: require('./icon/icon_png/male.png'), icMale: require('./icon/icon_png/male.png'),
......
// FEATURE: Component EmailChipInput để nhập và hiển thị danh sách email dạng chip
import React, {useRef, useState} from 'react'; import React, {useRef, useState} from 'react';
import {View, TextInput, TouchableOpacity, Image} from 'react-native'; import {View, TextInput, TouchableOpacity, Image} from 'react-native';
import Chip from './chip'; import Chip from './chip';
import {BACKSPACE, DELIMITERS} from '../../config/constants'; import {BACKSPACE, DELIMITERS} from '../../config/constants';
import {isValidEmail} from '../../config/Functions'; import {isValidEmail} from '../../config/Functions';
import styles from './styles';
import {Text} from 'react-native'; import {Text} from 'react-native';
import R from '../../assets/R'; import R from '../../assets/R';
...@@ -75,17 +73,21 @@ export const EmailChipInput = ({ ...@@ -75,17 +73,21 @@ export const EmailChipInput = ({
autoFocus = false, autoFocus = false,
blurOnSubmit = true, blurOnSubmit = true,
keyboardType = 'email-address', keyboardType = 'email-address',
widthContainerInput= '100%', widthContainerInput = '100%',
}) => { }) => {
// STATE: Refs và state management
const ref = useRef(null); const ref = useRef(null);
const [emails, setEmails] = useState(entries); const [emails, setEmails] = useState(entries);
const [value, setValue] = useState(''); const [value, setValue] = useState('');
const [isExpanded, setIsExpanded] = useState(false); // Quản lý chế độ hiển thị chip const [isExpanded, setIsExpanded] = useState(false);
const [showInput, setShowInput] = useState(false);
const onBlur = () => {
if (!value) {
setShowInput(false);
}
};
// FUNCTIONALITY: Xử lý nhấn vào chip để edit
const handleOnPressChip = index => { const handleOnPressChip = index => {
// NOTE: Đưa email từ chip về input để chỉnh sửa
setValue(emails[index]); setValue(emails[index]);
setEmails(emails.filter((_, i) => i !== index)); setEmails(emails.filter((_, i) => i !== index));
...@@ -129,7 +131,7 @@ export const EmailChipInput = ({ ...@@ -129,7 +131,7 @@ export const EmailChipInput = ({
}; };
// FUNCTIONALITY: Xử lý blur/submit để finalize email // FUNCTIONALITY: Xử lý blur/submit để finalize email
const onBlur = () => { const handleBlur = () => {
// VALIDATION: Nếu email không hợp lệ hoặc rỗng, chỉ submit danh sách hiện tại // VALIDATION: Nếu email không hợp lệ hoặc rỗng, chỉ submit danh sách hiện tại
if (!isValidEmail(value)) { if (!isValidEmail(value)) {
return onSubmit(emails); return onSubmit(emails);
...@@ -180,12 +182,13 @@ export const EmailChipInput = ({ ...@@ -180,12 +182,13 @@ export const EmailChipInput = ({
chipTextStyle={chipTextStyle} chipTextStyle={chipTextStyle}
marginHorizontal={5} marginHorizontal={5}
/> />
<Text style={{ <Text
style={{
fontSize: 12, fontSize: 12,
color: R.colors.gray, color: R.colors.gray,
fontFamily: R.fonts.fontMedium, fontFamily: R.fonts.fontMedium,
marginLeft: 5, marginLeft: 5,
alignSelf: 'center' alignSelf: 'center',
}}> }}>
+{emails.length - 1} more +{emails.length - 1} more
</Text> </Text>
...@@ -201,45 +204,62 @@ export const EmailChipInput = ({ ...@@ -201,45 +204,62 @@ export const EmailChipInput = ({
flexDirection: 'row', flexDirection: 'row',
flexWrap: emails.length > 0 ? 'wrap' : 'nowrap', flexWrap: emails.length > 0 ? 'wrap' : 'nowrap',
alignItems: 'center', alignItems: 'center',
} },
]}> ]}>
{/* FEATURE: Render danh sách email chips */} {/* FEATURE: Render danh sách email chips */}
<TouchableOpacity
onPress={() => setShowInput(true)}
style={{
flexDirection: 'row',
flexWrap: emails.length > 0 ? 'wrap' : 'nowrap',
alignItems: 'center',
}}>
<Text <Text
style={{ style={{
fontSize: 12, fontSize: 12,
color: R.colors.black, color: R.colors.black,
fontFamily:R.fonts.fontMedium, fontFamily: R.fonts.fontMedium,
fontWeight:'600', fontWeight: '600',
}} }}>
>{title}</Text> {title}
</Text>
{renderChips()} {renderChips()}
</TouchableOpacity>
{/* FEATURE: Nút toggle thu gọn/phóng to */} {/* FEATURE: Nút toggle thu gọn/phóng to */}
{emails.length > 1 && ( {emails.length > 1 && (
<View style={{ position: 'absolute', right: 0, top: 5}}> <View style={{position: 'absolute', right: -3, top: 0}}>
<TouchableOpacity <TouchableOpacity
onPress={toggleExpanded} onPress={toggleExpanded}
> style={{
backgroundColor: R.colors.blue4,
borderRadius: 15,
padding: 3,
}}>
<Image <Image
source={isExpanded ? R.images.icDrop : R.images.icDrop} source={isExpanded ? R.images.icBack2 : R.images.icDrop}
style={{ style={{
width: 30, width: 25,
height: 20, height: 25,
tintColor: R.colors.black tintColor: R.colors.black,
}} }}
/> />
</TouchableOpacity> </TouchableOpacity>
</View> </View>
)} )}
{/* FEATURE: Input để nhập email mới */} {showInput && (
<View style={[{width: widthContainerInput}, inputContainerStyle]}> <View
style={[
{width: '100%', height: 35, widthContainerInput},
inputContainerStyle,
]}>
<TextInput <TextInput
value={value} value={value}
ref={ref} ref={ref}
onChangeText={onTextChange} onChangeText={onTextChange}
onKeyPress={onKeyPress} onKeyPress={onKeyPress}
onSubmitEditing={onBlur} onSubmitEditing={handleBlur}
onBlur={onBlur} onBlur={onBlur}
blurOnSubmit={blurOnSubmit} blurOnSubmit={blurOnSubmit}
keyboardType={keyboardType} keyboardType={keyboardType}
...@@ -250,17 +270,23 @@ export const EmailChipInput = ({ ...@@ -250,17 +270,23 @@ export const EmailChipInput = ({
autoCapitalize={autoCapitalize} autoCapitalize={autoCapitalize}
keyboardAppearance={keyboardAppearance} keyboardAppearance={keyboardAppearance}
placeholderTextColor={placeholderTextColor} placeholderTextColor={placeholderTextColor}
style={[{ style={[
{
fontSize: 12, fontSize: 12,
color: R.colors.black, color: R.colors.black,
fontFamily:R.fonts.fontMedium, fontFamily: R.fonts.fontMedium,
fontWeight:'600', fontWeight: '600',
borderBottomWidth:1, padding: 0,
borderColor:R.colors.grayBorderInputTextHeader height: '100%',
}, inputStyle]} paddingHorizontal: 0,
textAlignVertical: 'center',
},
inputStyle,
]}
{...TextInputProps} {...TextInputProps}
/> />
</View> </View>
)}
</View> </View>
</View> </View>
); );
......
...@@ -39,8 +39,13 @@ const renderHeader = () => { ...@@ -39,8 +39,13 @@ const renderHeader = () => {
onSubmit={handleChange} onSubmit={handleChange}
chipContainerStyle={{ backgroundColor: R.colors.blue4}} chipContainerStyle={{ backgroundColor: R.colors.blue4}}
chipTextStyle={{ color:R.colors.black }} chipTextStyle={{ color:R.colors.black }}
containerStyle={{
borderBottomWidth:1,
borderColor: R.colors.blue4,
borderRadius: 5,
}}
chipImage={ chipImage={
<Image source={R.images.icCancel} style={styles.iconClose} tintColor={R.colors.blue}/> <Image source={R.images.icCancel} style={styles.iconClose} tintColor={R.colors.black}/>
} }
/> />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment