@chem-po/react-web 0.0.15 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,27 +1,18 @@
1
1
  import { Flex, Text } from '@chakra-ui/react'
2
2
 
3
3
  import { formatField } from '@chem-po/core'
4
- import { Field } from '@chem-po/react'
4
+ import { Field, FormatField } from '@chem-po/react'
5
5
  import React, { CSSProperties, useMemo } from 'react'
6
6
  import { FileFieldView } from './file'
7
7
  import { MultipleSelectFieldView } from './multipleSelect'
8
8
  import { SelectFieldView } from './select'
9
+ import { FieldViewProps } from './types'
9
10
 
10
- const DefaultFieldView = ({
11
- field,
12
- value,
13
- noLabel,
14
- style,
15
- }: {
16
- field: Field
17
- value: any
18
- noLabel?: boolean
19
- style?: CSSProperties
20
- }) => {
11
+ const DefaultFieldView = <F extends Field>({ field, value, noLabel, style }: FieldViewProps<F>) => {
21
12
  const { placeholder } = field
22
13
 
23
14
  const formatted = useMemo(() => {
24
- const format = formatField[field._type]
15
+ const format = formatField[field._type] as FormatField<F>
25
16
  if (!format) return value
26
17
  return format(field, value)
27
18
  }, [value, field])
@@ -2,20 +2,27 @@ import { Box, Text } from '@chakra-ui/react'
2
2
 
3
3
  import { Flex, useColorMode } from '@chakra-ui/react'
4
4
  import { MultipleSelectField } from '@chem-po/react'
5
- import React, { CSSProperties } from 'react'
5
+ import React, { useMemo } from 'react'
6
+ import { useInputStyles } from '../input/hooks/useInputStyles'
7
+ import { getRenderSelectedOptionText } from '../input/multipleSelect'
8
+ import { FieldViewProps } from './types'
6
9
 
7
- export const MultipleSelectFieldView = ({
10
+ export const MultipleSelectFieldView = <F extends MultipleSelectField>({
8
11
  field,
9
12
  value,
10
13
  noLabel,
11
14
  style,
12
- }: {
13
- field: MultipleSelectField
14
- value: any[]
15
- noLabel?: boolean
16
- style?: CSSProperties
17
- }) => {
18
- const { placeholder, renderOption: RenderOption } = field
15
+ }: FieldViewProps<F>) => {
16
+ const { placeholder, options } = field
17
+ const { size, text } = useInputStyles(undefined, field.size)
18
+ const selectedOptions = useMemo(
19
+ () => options.filter(o => value?.includes(o.value)),
20
+ [value, options],
21
+ )
22
+ const RenderSelectedOptionText = useMemo(
23
+ () => getRenderSelectedOptionText(field, text),
24
+ [field, text],
25
+ )
19
26
  const { colorMode } = useColorMode()
20
27
  return (
21
28
  <Flex maxW="100%" flexFlow="row wrap" align="center" style={style}>
@@ -24,10 +31,16 @@ export const MultipleSelectFieldView = ({
24
31
  {placeholder}
25
32
  </Text>
26
33
  )}
27
- {value ? (
28
- value.map((v: any) => (
29
- <Box key={field.getOptionKey ? field.getOptionKey(v) : v} p={0.5}>
30
- <RenderOption value={v} colorMode={colorMode} isSelected={true} />
34
+ {selectedOptions.length ? (
35
+ selectedOptions.map(o => (
36
+ <Box key={o.value} p={0.5}>
37
+ <RenderSelectedOptionText
38
+ value={o.value}
39
+ option={o}
40
+ colorMode={colorMode}
41
+ isSelected={true}
42
+ size={size}
43
+ />
31
44
  </Box>
32
45
  ))
33
46
  ) : (
@@ -1,6 +1,8 @@
1
1
  import { Flex, Text, useColorMode } from '@chakra-ui/react'
2
2
  import { SelectField } from '@chem-po/react'
3
- import React, { CSSProperties } from 'react'
3
+ import React from 'react'
4
+ import { useInputStyles } from '../input/hooks/useInputStyles'
5
+ import { FieldViewProps } from './types'
4
6
 
5
7
  const stringifyValue = (value: any) => {
6
8
  if (typeof value === 'string') return value
@@ -18,14 +20,12 @@ export const SelectFieldView = <F extends SelectField>({
18
20
  value,
19
21
  noLabel,
20
22
  style,
21
- }: {
22
- field: F
23
- value: F['defaultValue']
24
- noLabel?: boolean
25
- style?: CSSProperties
26
- }) => {
23
+ size: propSize,
24
+ }: FieldViewProps<F>) => {
27
25
  const { placeholder, renderOption: customRender } = field
28
26
  const { colorMode } = useColorMode()
27
+ const { size } = useInputStyles(undefined, field.size, propSize)
28
+ const selectedOption = field.options.find(o => o.value === value)
29
29
  const RenderOption = customRender ?? DefaultRenderOption
30
30
  return (
31
31
  <Flex align="center" style={style}>
@@ -34,7 +34,17 @@ export const SelectFieldView = <F extends SelectField>({
34
34
  {placeholder}
35
35
  </Text>
36
36
  )}
37
- {value ? <RenderOption value={value} colorMode={colorMode} isSelected={true} /> : 'None'}
37
+ {value && selectedOption ? (
38
+ <RenderOption
39
+ value={value}
40
+ option={selectedOption}
41
+ colorMode={colorMode}
42
+ isSelected={true}
43
+ size={size}
44
+ />
45
+ ) : (
46
+ <Text>None</Text>
47
+ )}
38
48
  </Flex>
39
49
  )
40
50
  }
@@ -0,0 +1,11 @@
1
+ import { InputSize } from '@chem-po/core'
2
+ import { Field } from '@chem-po/react'
3
+ import { CSSProperties } from 'react'
4
+
5
+ export interface FieldViewProps<F extends Field> {
6
+ field: F
7
+ value: F['defaultValue']
8
+ noLabel?: boolean
9
+ size?: InputSize
10
+ style?: CSSProperties
11
+ }
@@ -1,39 +0,0 @@
1
- import { InputSize } from '@chem-po/react'
2
- import { CSSProperties, useMemo } from 'react'
3
-
4
- const inputPaddingY: Record<InputSize, number> = {
5
- xs: 0.25,
6
- sm: 0.35,
7
- md: 0.5,
8
- lg: 0.5,
9
- xl: 0.5,
10
- }
11
-
12
- const inputPaddingX: Record<InputSize, number> = {
13
- xs: 0.5,
14
- sm: 0.75,
15
- md: 0.75,
16
- lg: 0.75,
17
- xl: 0.75,
18
- }
19
- const getInputPadding = (size: InputSize) => {
20
- const paddingY = inputPaddingY[size]
21
- const paddingX = inputPaddingX[size]
22
- return `${paddingY}rem ${paddingX}rem`
23
- }
24
- const fontSize: Record<InputSize, string> = {
25
- xs: '0.75rem',
26
- sm: '0.875rem',
27
- md: '1rem',
28
- lg: '1.125rem',
29
- xl: '1.25rem',
30
- }
31
-
32
- export const useInputStyle = (size?: InputSize) =>
33
- useMemo<Partial<CSSProperties>>(
34
- () => ({
35
- padding: getInputPadding(size ?? 'md'),
36
- fontSize: fontSize[size ?? 'md'],
37
- }),
38
- [size],
39
- )