about summary refs log tree commit diff
path: root/src/components/Select/types.ts
blob: 661621a603597a310b26c28858b2d180f59fa5ea (plain) (blame)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import {
  type AccessibilityProps,
  type StyleProp,
  type TextStyle,
  type ViewStyle,
} from 'react-native'

import {type TextStyleProp} from '#/alf'
import {type DialogControlProps} from '#/components/Dialog'
import {type Props as SVGIconProps} from '#/components/icons/common'

export type RootProps = {
  children?: React.ReactNode
  value?: string
  onValueChange?: (value: string) => void
  disabled?: boolean
  /**
   * @platform web
   */
  defaultValue?: string
  /**
   * @platform web
   */
  open?: boolean
  /**
   * @platform web
   */
  defaultOpen?: boolean
  /**
   * @platform web
   */
  onOpenChange?(open: boolean): void
  /**
   * @platform web
   */
  name?: string
  /**
   * @platform web
   */
  autoComplete?: string
  /**
   * @platform web
   */
  required?: boolean
}

export type RadixPassThroughTriggerProps = {
  id: string
  type: 'button'
  disabled: boolean
  ['data-disabled']: boolean
  ['data-state']: string
  ['aria-controls']?: string
  ['aria-haspopup']?: boolean
  ['aria-expanded']?: AccessibilityProps['aria-expanded']
  onClick: () => void
  onPointerDown: () => void
  onKeyDown: () => void
}

export type TriggerProps = {
  children: React.ReactNode | ((props: TriggerChildProps) => React.ReactNode)
  label: string
}

export type TriggerChildProps =
  | {
      isNative: true
      control: DialogControlProps
      state: {
        /**
         * Web only, `false` on native
         */
        hovered: false
        focused: boolean
        pressed: boolean
      }
      /**
       * We don't necessarily know what these will be spread on to, so we
       * should add props one-by-one.
       *
       * On web, these properties are applied to a parent `Pressable`, so this
       * object is empty.
       */
      props: {
        onPress: () => void
        onFocus: () => void
        onBlur: () => void
        onPressIn: () => void
        onPressOut: () => void
        accessibilityLabel: string
      }
    }
  | {
      isNative: false
      state: {
        hovered: boolean
        focused: boolean
        /**
         * Native only, `false` on web
         */
        pressed: false
      }
      props: RadixPassThroughTriggerProps & {
        onPress: () => void
        onFocus: () => void
        onBlur: () => void
        onMouseEnter: () => void
        onMouseLeave: () => void
        accessibilityLabel: string
      }
    }

/*
 * For use within the `Select.Trigger` component.
 * Shows the currently selected value. You can also
 * provide a placeholder to show when no value is selected.
 *
 * If you're passing items of a different shape than {value: string, label: string},
 * you'll need to pass a function to `children` that extracts the label from an item.
 */
export type ValueProps = {
  /**
   * Only needed for native. Extracts the label from an item. Defaults to `item => item.label`
   */
  children?: (value: any) => string
  placeholder?: string
  style?: StyleProp<TextStyle>
}

/*
 * Icon for use within the `Select.Trigger` component.
 * Changes based on platform - chevron down on web, up/down chevrons on native
 *
 * `style` prop is web only
 */
export type IconProps = TextStyleProp

export type ContentProps<T> = {
  /**
   * Items to render. Recommended to be in the form {value: string, label: string} - if not,
   * you need to provide a `valueExtractor` function to extract the value from an item and
   * customise the `Select.ValueText` component.
   */
  items: T[]
  /**
   * Renders an item. You should probably use the `Select.Item` component.
   *
   * @example
   * ```tsx
   * renderItem={({label, value}) => (
   *   <Select.Item value={value} label={label}>
   *     <Select.ItemIndicator />
   *     <Select.ItemText>{label}</Select.ItemText>
   *   </Select.Item>
   * )}
   * ```
   */
  renderItem: (
    item: T,
    index: number,
    selectedValue?: string | null,
  ) => React.ReactElement
  /*
   * Extracts the value from an item. Defaults to `item => item.value`
   */
  valueExtractor?: (item: T) => string
}

/*
 * An item within the select dropdown
 */
export type ItemProps = {
  ref?: React.Ref<HTMLDivElement>
  value: string
  label: string
  children: React.ReactNode
  style?: StyleProp<ViewStyle>
}

export type ItemTextProps = {
  children: React.ReactNode
}

export type ItemIndicatorProps = {
  icon?: React.ComponentType<SVGIconProps>
}