Skip to content

EXAMPLE: emoji picker #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
"react-native-svg": "15.8.0",
"react-native-web": "~0.19.6",
"reactotron-react-native": "^5.1.7",
"rn-emoji-keyboard": "^1.7.0",
"sass": "^1.77.2",
"setimmediate": "^1.0.5",
"use-debounce": "^9.0.4"
Expand Down
3 changes: 3 additions & 0 deletions src/components/molecules/Field/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { PropsWithChildren } from 'react'

import { FieldCheckbox } from './FieldCheckbox'
import { FieldCheckboxGroup } from './FieldCheckboxGroup'
import { FieldEmojiPicker } from './FieldEmojiPicker'
import { FieldInput } from './FieldInput'
import { FieldRadioGroup } from './FieldRadioGroup'
import { FieldSelect } from './FieldSelect'
Expand All @@ -12,6 +13,7 @@ type FieldComposition = React.FC<PropsWithChildren> & {
Checkbox: typeof FieldCheckbox
RadioGroup: typeof FieldRadioGroup
Select: typeof FieldSelect
EmojiPicker: typeof FieldEmojiPicker
}

const Field: FieldComposition = ({ children }) => {
Expand All @@ -23,6 +25,7 @@ Field.CheckboxGroup = FieldCheckboxGroup
Field.Checkbox = FieldCheckbox
Field.RadioGroup = FieldRadioGroup
Field.Select = FieldSelect
Field.EmojiPicker = FieldEmojiPicker

export { Field }
export * from './types'
78 changes: 78 additions & 0 deletions src/components/molecules/Field/FieldEmojiPicker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { forwardRef, useCallback, useImperativeHandle, useRef, useMemo } from 'react'
import { NativeSyntheticEvent, TextInputFocusEventData } from 'react-native'

import type { FieldEmojiPickerProps } from './types'

import {
FormErrorMessage,
FormLabel,
Box,
EmojiPicker,
EmojiPickerRef,
} from '@/design-system/components'
import { getLayoutProps } from '@/design-system/utils/getLayoutProps'

export const FieldEmojiPicker = forwardRef<EmojiPickerRef, FieldEmojiPickerProps>(
(
{
errorMessage,
isInvalid,
isRequired,
label,
labelStyle,
onBlur,
onFocus,
testID,
onChangeEmoji,
...props
},
ref
) => {
const _emojiPickerRef = useRef<EmojiPickerRef>(null)

const { layoutProps, restProps: emojiPickerProps } = useMemo(
() => getLayoutProps(props),
[props]
)

const handleFocus = useCallback(() => {
_emojiPickerRef?.current?.focus()
}, [onFocus])

Check warning on line 40 in src/components/molecules/Field/FieldEmojiPicker.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

React Hook useCallback has an unnecessary dependency: 'onFocus'. Either exclude it or remove the dependency array

const handleBlur = useCallback(
(e?: NativeSyntheticEvent<TextInputFocusEventData>) => {
onBlur && e && onBlur(e)
_emojiPickerRef.current?.blur()
},
[onBlur]
)

useImperativeHandle(
ref,
() => ({
focus: handleFocus,
blur: handleBlur,
..._emojiPickerRef.current,
}),
[handleBlur, handleFocus]
)

return (
<Box width="100%" gap={1} mb={2} {...layoutProps}>
<FormLabel
{...{ isRequired, label, labelStyle }}
testID={testID + ':label'}
onLabelPress={handleFocus}
/>
<EmojiPicker
isInvalid={isInvalid || Boolean(errorMessage)}
onChangeEmoji={onChangeEmoji}
{...emojiPickerProps}
ref={_emojiPickerRef}
testID={testID + ':input'}
/>
<FormErrorMessage {...{ errorMessage }} testId={testID + ':error_message'} />
</Box>
)
}
)
10 changes: 10 additions & 0 deletions src/components/molecules/Field/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SelectProps,
StyledProps,
TouchableRef,
EmojiPickerProps,
} from '@/design-system'

// -----------------------
Expand Down Expand Up @@ -92,3 +93,12 @@ export type FieldCheckboxProps = FormLabelProps &
CheckboxProps & {
errorMessage?: string
}

// -----------------------
// ----- EMOJI PICKER ----
// -----------------------

export type FieldEmojiPickerProps = FormLabelProps &
EmojiPickerProps & {
errorMessage?: string
}
44 changes: 44 additions & 0 deletions src/components/organisms/ControlledField/ControlledEmojiPicker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useCallback } from 'react'
import { Controller, ControllerProps, FieldValues, get } from 'react-hook-form'

import type { ControlledEmojiPickerProps } from './types'
import { Field } from '../../molecules'

export const ControlledEmojiPicker = <TFieldValues extends FieldValues = FieldValues>({
control,
name,
errors,
rules,
children,
...props
}: ControlledEmojiPickerProps<TFieldValues>) => {
const errorMessage = get(errors, name)?.message

const renderEmojiPicker = useCallback(
({
field: { onChange, name, ref, value, ...fieldProps },
}: Parameters<ControllerProps['render']>[0]) => {
return (
<Field.EmojiPicker
{...props}
{...fieldProps}
emoji={value}
ref={ref}
errorMessage={errorMessage}
onChangeEmoji={onChange}
/>
)
},
[errorMessage, props]
)

return (
<Controller
name={name}
// @ts-expect-error: For some reason, the type of render is not being inferred correctly
control={control}
rules={rules}
render={renderEmojiPicker}
/>
)
}
3 changes: 3 additions & 0 deletions src/components/organisms/ControlledField/ControlledField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { PropsWithChildren } from 'react'

import { ControlledCheckbox } from './ControlledCheckbox'
import { ControlledCheckboxGroup } from './ControlledCheckboxGroup'
import { ControlledEmojiPicker } from './ControlledEmojiPicker'
import { ControlledInput } from './ControlledInput'
import { ControlledRadioGroup } from './ControlledRadioGroup'
import { ControlledSelect } from './ControlledSelect'
Expand All @@ -12,6 +13,7 @@ type ControlledFieldComposition = React.FC<PropsWithChildren> & {
RadioGroup: typeof ControlledRadioGroup
Checkbox: typeof ControlledCheckbox
Select: typeof ControlledSelect
EmojiPicker: typeof ControlledEmojiPicker
}

const ControlledField: ControlledFieldComposition = ({ children }) => {
Expand All @@ -23,6 +25,7 @@ ControlledField.CheckboxGroup = ControlledCheckboxGroup
ControlledField.Checkbox = ControlledCheckbox
ControlledField.RadioGroup = ControlledRadioGroup
ControlledField.Select = ControlledSelect
ControlledField.EmojiPicker = ControlledEmojiPicker

export { ControlledField }
export * from './types'
11 changes: 11 additions & 0 deletions src/components/organisms/ControlledField/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FieldRadioGroupProps,
FieldSelectProps,
FieldCheckboxProps,
FieldEmojiPickerProps,
} from '@/components/molecules'

// -----------------------
Expand Down Expand Up @@ -68,3 +69,13 @@ export type ControlledCheckboxProps<TFieldValues extends FieldValues = FieldValu
'onChange' | 'isChecked'
> &
ControlledFieldProps<TFieldValues>

// -----------------------
// ----- EMOJI PICKER ----
// -----------------------

export type ControlledEmojiPickerProps<TFieldValues extends FieldValues = FieldValues> = Omit<
FieldEmojiPickerProps,
'ref' | 'onChangeEmoji' | 'emoji'
> &
ControlledFieldProps<TFieldValues>
115 changes: 115 additions & 0 deletions src/design-system/components/EmojiPicker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { forwardRef, useCallback, useImperativeHandle } from 'react'
import { StyleSheet } from 'react-native'
import EmojiKeyboard from 'rn-emoji-keyboard'

import { BoxWithShadow } from './BoxWithShadow'
import { Icon } from './Icon'
import { Row } from './Row'
import { Text } from './Text'
import { Touchable } from './Touchables'
import { EmojiPickerProps, EmojiPickerRef } from './types'

import { useBoolean, useTheme } from '@/hooks'
import { hex2rgba } from '@/utils'

export const EmojiPicker = forwardRef<EmojiPickerRef, EmojiPickerProps>(
({ emoji, onChangeEmoji, placeholder, isDisabled, isInvalid, ...props }, ref) => {
const [isOpen, setIsOpen] = useBoolean(false)
const { colors } = useTheme()

const clearEmoji = useCallback(() => {
onChangeEmoji({
emoji: '',
name: '',
slug: '',
})
}, [onChangeEmoji])

useImperativeHandle(
ref,
() => ({
focus: setIsOpen.on,
blur: setIsOpen.off,
}),
[setIsOpen.on, setIsOpen.off]
)

return (
<>
<BoxWithShadow isInvalid={isInvalid}>
<Touchable
onPress={setIsOpen.on}
width="100%"
minW={48}
disabled={isDisabled}
alignItems="center"
borderColor={
isDisabled ? 'border.disabled' : isInvalid ? 'border.error' : 'border.primary'
}
bg={isDisabled ? 'bg.disabled_subtle' : 'bg.primary'}
borderRadius={8}
borderWidth={1}
flexDirection="row"
overflow="hidden"
{...props}
>
<Row
alignItems="center"
gap={2}
px={2}
py={2}
justifyContent="space-between"
width={'100%'}
>
<Text.MdRegular
style={emoji?.emoji ? styles.input : undefined}
color={emoji?.emoji ? 'text.primary' : 'text.placeholder'}
>
{emoji?.emoji || placeholder}
</Text.MdRegular>
{emoji?.emoji ? (
<Touchable right={0} onPress={clearEmoji}>
<Icon name="close-line" color="text.primary" size={18} />
</Touchable>
) : null}
</Row>
</Touchable>
</BoxWithShadow>
{/* FIXME: Emoji picker is not looking nice on web and bigger screens, we need to do something with that */}
<EmojiKeyboard
open={isOpen}
onClose={setIsOpen.off}
onEmojiSelected={(selectedEmoji) => {

Check warning on line 82 in src/design-system/components/EmojiPicker.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

JSX props should not use arrow functions
onChangeEmoji(selectedEmoji)
setIsOpen.off()
}}
theme={{
backdrop: hex2rgba(colors.bg.primary, 0.2),
header: colors.text.primary,
container: colors.bg.primary,
knob: colors.border.primary,
category: {
icon: colors.text.secondary,
iconActive: colors.text.primary,
container: colors.bg.quaternary,
containerActive: colors.bg.active,
},
search: {
text: colors.text.primary,
placeholder: colors.text.placeholder,
icon: colors.text.primary,
background: colors.bg.primary,
},
}}
enableSearchBar
/>
</>
)
}
)

const styles = StyleSheet.create({
input: {
fontFamily: 'system',
},
})
1 change: 1 addition & 0 deletions src/design-system/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './BoxWithShadow'
export * from './Button'
export * from './Center'
export * from './Column'
export * from './EmojiPicker'
export * from './GradientBox'
export * from './Row'
export * from './Spacer'
Expand Down
25 changes: 25 additions & 0 deletions src/design-system/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,28 @@ export type CheckboxProps = TouchableProps & {
size?: 'sm' | 'md'
pb?: SizingValue
}

// -----------------------
// ----- EMOJI PICKER ----
// -----------------------

export type EmojiPickerRef = { focus: () => void; blur: () => void }

export type EmojiPickerProps = TouchableProps & {
// Logic
onChangeEmoji: (newValue: EmojiType) => void
emoji?: EmojiType

// UI
placeholder?: string
isDisabled?: boolean
isInvalid?: boolean
rightElement?: JSX.Element
leftElement?: JSX.Element
}

type EmojiType = {
emoji: string
name: string
slug: string
}
2 changes: 2 additions & 0 deletions src/hooks/forms/useTestForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const defaultValues: TestFormValues = {
age: '',
education: '',
interests: [],
icon: '',
}

export const useTestForm = () => {
Expand Down Expand Up @@ -52,6 +53,7 @@ export const useTestForm = () => {
music: { required: t('test_form.errors.music') },
interests: { required: t('test_form.errors.interests') },
sex: { required: t('test_form.errors.sex') },
icon: { required: t('test_form.errors.icon') },
}

const {
Expand Down
Loading
Loading